Unexpected token "name" of value "if" ("end of statement block" expected) - symfony

I tried to upgrade Symfony to 4.4.17 and I got the above error for the line:
{% for lang, group in lang_groups if lang == from %}
This is the code. I have no idea how to fix this.
<div class="input-style">
<input type="text" name="query" value="{{ query|default('') }}" />
</div>
<div class="select-style">
<select name="lang_from">
{% for lang, group in lang_groups %}
<option value="{{ lang }}"{% if from == lang %} selected{% endif %}>{{ group.label }}</option>
{% endfor %}
</select>
</div>
⇄
{% for lang, group in lang_groups if lang == from %}
<div class="select-style lang-group" data-lang="{{ lang }}">
<select name="{{ lang }}">
{% for lang_to, data in group.to %}
<option value="{{ lang_to }}"{% if to|default('') == lang_to %} selected{% endif %}>{{ data.label }}</option>
{% endfor %}
</select>
</div>
{% endfor %}

You have to put the if statement out of your for...in
So instead of this
{% for lang, group in lang_groups if lang == from %}
<div class="select-style lang-group" data-lang="{{ lang }}">
<select name="{{ lang }}">
{% for lang_to, data in group.to %}
<option value="{{ lang_to }}"{% if to|default('') == lang_to %} selected{% endif %}>{{ data.label }}</option>
{% endfor %}
</select>
</div>
{% endfor %}
If I understood what you are trying to do, you should do something like this:
{% for lang, group in lang_groups %}
{% if lang == from %}
<div class="select-style lang-group" data-lang="{{ lang }}">
<select name="{{ lang }}">
{% for lang_to, data in group.to %}
<option value="{{ lang_to }}"{% if to|default('') == lang_to %} selected{% endif %}>{{ data.label }}</option>
{% endfor %}
</select>
</div>
{% endif %}
{% endfor %}

Related

Symfony 6 : Customize a form with radio buttons

I have a form and want to customize it with symfony :
<input type="radio" name="type" value="auto" onClick="getMarques('auto')" id="check1">
<label class="s-submitCheckLabel" for="check1"><span class="m-circle"></span></label>
<label class="s-submitCheck" for="check1">Auto</label>
I have tried this:
{% for child in formAnnonce.category %}
{{ form_widget(child, {'attr' : {'class': 'form-check-input'}, {'name': 'type'}, {'id': 'check1'} }) }}
{{ form_label(child)}}
{{ form_help(child) }}
{{ form_errors(child) }}
{% endfor %}
{% for child in formAnnonce.category %}
{{ form_widget(child, {'attr': {'onclick': 'getMarques("' ~ child.vars.value~'")'}})}}
<label class="s-submitCheckLabel" for="{{ child.vars.id }}"><span class="m-circle"> </span></label>
<label class="s-submitCheck" for="{{ child.vars.id }}">{{ child.vars.label </label>
{% endfor %}

Symfony : multiple "for" loops in Twig

I have all these {% for %} loops that are nested one inside the other but I would like them to be all at the same level. Independent from one another because like this, my goalkeepers only appear if there are defenders, midfielders... I would like them all to appear wether one category is missing or not.
<tbody>
{% for defenser in defensers %}
{% for midfielder in midfielders %}
{% for forward in forwards %}
{% for gk in goalkeepers %}
<tr>
<td>
<div class="player-in-club">
{{ gk.name }}
</div>
</td>
<td>
<div class="player-in-club">
{{ defenser.name }}
</div>
</td>
<td>
<div class="player-in-club">
{{ midfielder.name }}
</div>
</td>
<td>
<div class="player-in-club">
{{ forward.name }}
</div>
</td>
</tr>
{% endfor %}
{% endfor %}
{% endfor %}
{% endfor %}
</tbody>
You can ensure that your arrays always contain at least one (empty string) Element if you put this in front of your for loops:
{% if defensers is empty %}
{% set defensers = [""] %}
{% endif %}
{% if midfielders is empty %}
{% set midfielders = [""] %}
{% endif %}
{% if forwards is empty %}
{% set forwards = [""] %}
{% endif %}
{% if goalkeepers is empty %}
{% set goalkeepers = [""] %}
{% endif %}
But I feel like you want something kompletely differently. The way you have set up your loops they will print every possible row kombination.
If you just want a table that is filled as the items are available you might want to use this instead of your loops:
{% set row_count = max(defensers|length, midfielders|length, forwards|length, goalkeepers|length) %}
<tbody>
{% for index in range(0, row_count - 1) %}
<tr>
<td>
<div class="player-in-club">
{% if goalkeepers[index] %}
{{ goalkeepers[index].name }}
{% endif %}
</div>
</td>
<td>
<div class="player-in-club">
{% if defensers[index] %}
{{ defensers[index].name }}
{% endif %}
</div>
</td>
<td>
<div class="player-in-club">
{% if midfielders[index] %}
{{ midfielders[index].name }}
{% endif %}
</div>
</td>
<td>
<div class="player-in-club">
{% if forwards[index] %}
{{ forwards[index].name }}
{% endif %}
</div>
</td>
</tr>
{% endfor %}
</tbody>

Shopify Custom Text box according to product collection

Im trying to do a conditional where if a product is in collection "personal" it will show a text box. I have this in the product.liquid page but it does't seem to be working.
{% if collection.title == 'personal' %}
<div>
<p><input type="text" id="letter" placeholder="Enter up to 6 Letters" name="properties[letter]" /></p>
</div>
{% endif %}
Try this:
{% for collection in product.collections %}
{% if collection.handle == 'personal' %}
<div>
<p><input type="text" id="letter" placeholder="Enter up to 6 Letters" name="properties[letter]" /></p>
</div>
{% endif %}
{% endfor %}
See the Shopify docs for product.collections.

Is it possible to wrap a form_widget with a form_label in Twig?

To integrate this in Twig:
<label class="control-label" for="lastname">Last Name:</label>
<div class="controls">
<input type="text" id="firstname" name="firstname">
<span class="help-block">{{ form_errors(form.firstname) }}</span>
</div>
I used the following code snippet:
{{ form_label(form.firstname, null, {'label_attr': {'class': 'control-label'}}) }}
<div class="controls">
{{ form_widget(form.firstname) }}
<span class="help-block">{{ form_errors(form.firstname) }}</span>
</div>
And everything worked fine.
But my question is ...
Is it possible to wrap a form_widget with a form_label in Twig? The final result should then be similar to:
<label class="radio" for="dn">
<input type="radio" id="dn" name="spotlight" value="1"> Test 1
</label>
<label class="radio" for="gn">
<input type="radio" id="gn" name="spotlight" value="1"> Test 2
</label>
<span class="help-block">Errors</span>
Can I use anything else than form_label and form_widget to achive the same result?
You can change the display of the form_row() output in only one file:
{% form_theme form _self %}
{% block form_row %}
<div class="form_row">
{% if label is not sameas(false) %}
{% if not compound %}
{% set label_attr = label_attr|merge({'for': id}) %}
{% endif %}
{% if required %}
{% set label_attr = label_attr|merge({'class': (label_attr.class|default('') ~ ' required')|trim}) %}
{% endif %}
{% if label is empty %}
{% set label = name|humanize %}
{% endif %}
<label{% for attrname, attrvalue in label_attr %} {{ attrname }}="{{ attrvalue }}"{% endfor %}>{{ label|trans({}, translation_domain) }}
{% endif %}
{{ form_widget(form) }}
{% if label is not sameas(false) %}
</label>
{% endif %}
{{ form_errors(form) }}
</div>
{% endblock form_row %}
And display your field:
{{ form_row(form.firstname) }}
The <label> is now opening before the field and closing after the field.
I took the original code from default theme and use the cookbook entry Form Theming in Twig > Method 1: Inside the same Template as the Form.
If you want to apply your idea to all of your fields, consider using form customization in order to change the display of {{ form_row(....) }} in all your bundle.

Select options are overwritten in symfony2 . How can i avoid that

{% for tableField in tableFieldsArr %}
<tr>
<td>
<select>
<option name="{{ tableField }}">{{ tableField }}</option>
</select>
</td>
</tr>
{% endfor %}
I am trying to generate a select box with its options for each tr row with the above code I am getting a select box with 1 item in each row. How can I fix it?
$tablefieldsArr comes from the controller.
You have to move the for loop inside your select:
{% for i in 0..tableFieldsArr|length %}
<tr>
<td>
<select>
{% for tableField in tableFieldsArr %}
<option name="{{ tableField }}">{{ tableField }}</option>
{% endfor %}
</select>
</td>
</tr>
{% endfor %}

Resources