Twig checkbox is checked - symfony-forms

please how can I do something as follows with twig checkbox
<input type="checkbox" name="checkbox">
{% if checkbox is checked %}
#do_it
{% else %}
#do_that
{% endif %}
help me, please

Checkboxes return string value.
{% if node.field_mycheckbox.value == '1' %}
<p>print 1</p>
{% else %}
<p>print 0</p>
{% endif %}

The controller:
$dispo = $mezzo->getAuDisponibile ();
// table column AuDisponibile is boolean
...
...
return $this-render ('anagrafiche/automezzi.html.twig', ['dispo' => $dispo]);
The template:
{% if dispo %}
<input input type='checkbox' name="idisponibile" value="SI" checked>
{% else %}
<input input type='checkbox' name="idisponibile" value="SI">
{% endif %}

You can check if a form field is checked with in example,
{% if form.field.children.KEY.vars.checked %}
Do something here
{% endif %}
Using it inside a loop.
{% for child in form.field %}
<input type="checkbox" name="checkbox" {{ child.vars.checked == true ? "Checked" }}>
{% endfor %}
or
<input type="checkbox" name="checkbox" {{ form.field.children.KEY.vars.checked ? "Checked" }}>
KEY being the array key.

Related

Comparison string in jinja

how to compare the data from database to compare with value in html with jinja?
I try gender value from db is Male
{% set gender=user.gender %}
{% if gender == 'Male' %}
{% set check = "checked" %}
{% else %}
{% set check = "unchecked" %}
{% endif %}
but the output is Male unchecked and Female also unchecked
I cannot reproduce the issue so it probably means your user variable is not defined as you expect:
import jinja2
t=jinja2.Template(''' {% set gender=user.gender %}
{% if gender == 'Male' %}
{% set check = "checked" %}
{% else %}
{% set check = "unchecked" %}
{% endif %}
''')
for gender in ['Male', 'Female']:
m = t.make_module(vars = {'user': {'gender': gender }})
print(m.check)
prints:
checked
unchecked

Liquid gives the wrong link

I'm creating a pagination in my website with Liquid template engine. But the link does not work as it should.
I want this link: https://support.beefinity.com/support/tickets/filter?page=2&requested_by=0&wf_filter=all
But I get this link: https://support.beefinity.com/support/tickets/filter/page/2?requested_by=0&url_locale=&wf_filter=all
This is the code that I use for the pagination:
{% paginate tickets by 10 %} {% for ticket in tickets %} {% if ticket.group.name == "Beefinity Support"%}
<div class="c-row c-ticket-row" id="ticket-view">
<span class="status-source sources-detailed-{{ticket.source_name | downcase}}" title=" {{ticket.source_name | downcase}} "></span> <span class="label-status-pending label label-small">{{ticket.status}}</span>
<div class="ticket-brief">
<div class="ellipsis">
<a class="c-link" href="{{ticket.portal_url}}" title="{{ticket.subject}}">{{ticket.subject}}</a>
</div>
<div class="help-text">
Made by: <span class="emphasize">{{ticket.requester.name}} Made on:{{ ticket.created_on | date: "%d-%m-%Y %H:%M" }}</span><br>
Agent: <span class="emphasize">{{ticket.agent.name}}</span>
</div>
</div>
</div>
{% endif %} {% endfor %} {% if paginate.pages != 0 %}
<div class="pagination pagination-centered">
<ul>
{% if paginate.previous %}
<li class="previous">
<a rel="prev start" href="{{ paginate.previous.url }}">
{{ paginate.previous.title }} Previous
</a>
</li>
{% else %}
<li class="disabled">
<span>« Previous </span>
</li>
{% endif %}
{% for part in paginate.parts %}
{% if part.is_link %}
<li>
<a rel="prev start" href="{{ part.url }}">
{{ part.title }}
</a>
</li>
{% else %}
{% if part.title == paginate.current_page %}
<li class="active">
{{ part.title }}
</li>
{% else %}
<li>
<a rel"start"> {{ part.title }}</a>
</li>
{% endif %}
{% endif %}
{% endfor %}
{% if paginate.next %}
<li class="next_page">
<a rel="next" href="{{ paginate.next.url }}">
Next {{ paginate.next.title }}
</a>
</li>
{% else %}
<li class="next_page disabled">
<span> Next » </span>
</li>
{% endif %}
</ul>
</div>
{% endif %}
{% endpaginate %}
in the documentation that I viewed it just goes well:

How can add multiple collection in shopify liquid for loop?

I want to show products from the collections selected from theme setting.
Here is my code:
{% assign col1 = collections[settings.collection1] %}
{% assign col2 = collections[settings.collection2] %}
{% assign col = [col1, col2] %}
{% for product in col .products %}
{{ product }}
{% endfor %}
Your code should be like this
{% for i in (1..2) %}
{% capture col %}collection{i}{% endcapture %}
{% for product in collecitons.settings[col] %}
{{product}}
{% endfor %}
{% endfor %}

How to Filter all Blog.Articles by containing specific tag in Shopify?

hello is there anyone shopify experts out there .
my only goal is to filter or display all articles according to their tag .
so this is what i have so far from this forum
{% assign counter = 0 %}
{% for article in blogs['lookbook'].articles %}
{% if article.tags contains 'Fox Racing' and counter < 2 %}
{% assign counter = counter | plus: 1 %}
<div class="njohn_search_otherpage">
<a href="{{ article.url }}" title="{{ article.title | escape }}">
<div>{{ article.image.src | img_url: 'medium' | img_tag: article.title }}</div>
<div>{{ article.title }}</div>
</a>
</div>
{% endif %}
{% endfor %}
the code are working but it's only four article will showed up. but in that sample tag i have already 10 articles.
The above code has a counter that limits the articles displayed, removing that counter works as expected, filtering all the articles by a tag.
{% for article in blogs['lookbook'].articles %}
{% if article.tags contains 'Fox Racing' %}
<div class="njohn_search_otherpage"> <div>{{ article.image.src | img_url: 'medium' | img_tag: article.title }}</div> <div>{{ article.title }}</div> </div>
{% endif %}
{% endfor %}

how to use break in for loop of swig template node js?

Hello i am trying to stop my loop when it reaches to half of the total length.
just like using break it is not happening.
{% if page.member && page.member.length > 0 %}
{% for member in page.member %}
{{ member }}
{% if loop.index0 == ((page.member.length/2)-1) %}
{% set count = loop.index %}
{% break %}
{% endif %}
{% endfor %}
{% endif %}
please look into it.
Thanking you.
There is no {% break %} tag in Swig templates.
You don't need the break tag:
{% if page.member && page.member.length > 0 %}
{% set count = false %}
{% for member in page.member %}
{% if not count %}
{{ member }}
{% if loop.index0 == ((page.member.length/2)-1) %}
{% set count = loop.index %}
{% endif %}
{% endif %}
{% endfor %}
{% endif %}

Resources