Symfony : multiple "for" loops in Twig - symfony

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>

Related

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

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 %}

Twig : table total all row and columns

I have a table with 10 rows and 10 columns with int values; I need the total to be print at 11th row, also i need to total and print at columns.
Example :
cell(1x11) = row1+row2+row3...row10;
cell(2x11) = row1+row2+row3...row10;
and
cell(11x1) = col1+col2+col3...col10;
cell(11x2) = col1+col2+col3...col10;
How can i do this easily.
Required result :
<table>
<tr>
<th></th>
<th>One</th>
<th>Two</th>
<th>Three</th>
<th>Total</th>
</tr>
<tr>
<td></td>
<td>1</td>
<td>2</td>
<td>3</td>
<td>6</td>
</tr>
<tr>
<td></td>
<td>1</td>
<td>2</td>
<td>3</td>
<td>6</td>
</tr>
<tr>
<td></td>
<td>1</td>
<td>2</td>
<td>3</td>
<td>6</td>
</tr>
<tr>
<th>Total</th>
<td>3</td>
<td>6</td>
<td>9</td>
<td>18</td>
</tr>
</table>
My actual twig code:
`
{% set len = entities|length %}
{% set len_e = exams|length %}
{% if len_e != null and len_e is not empty and len_e > 0 %}
<div class="table-responsive">
<table id="FZUserExam" class="table table-striped table-bordered">
<thead>
<tr>
<th rowspan="2">SUBJECT</th>
<th rowspan="2">Max<br/>Marks</th>
{% for i in 1..len_e %}
{% if i == 1 %}
{% set terms = exams[0].term %}
{% set x = 1 %}
{% else %}
{% set name = exams[i-1].term %}
{% if name == terms %}
{% set x = x+1 %}
{% else %}
<th colspan="{{x+1}}">{{ terms }}</th>
{% set terms = exams[i-1].term %}
{% set x = 1 %}
{% endif %}
{% endif %}
{% if i == len_e %}
<th colspan="{{x+1}}">{{ terms }}</th>
{% endif %}
{% endfor %}
<th rowspan="2">Grand<br/>Total</th>
</tr>
<tr>
{% set terms = exams[0].term %}
{% set x = 1 %}
{% for i in 1..len_e %}
{% set name = exams[i-1].term %}
{% if name == terms %}
{% set x = x+1 %}
<th>{{ exams[i-1].exam }}</th>
{% else %}
<th>Total</th>
<th>{{ exams[i-1].exam }}</th>
{% set terms = exams[i-1].term %}
{% set x = 1 %}
{% endif %}
{% if i == len_e %}
<th>Total</th>
{% endif %}
{% endfor %}
</tr>
</thead>
<tbody>
{% set y = 0 %}
{% set total = 0 %}{% set gtotal = 0 %}
{% for a,s in subject %}
<tr>
<td>{{ s.name }}</td>
<td>{{ s.maxMarks }}</td>
{% set terms = exams[0].term %}
{% set x = 1 %}
{% for i in 1..len_e %}
{% set name = exams[i-1].term %}
{% if name == terms %}
{% set x = x+1 %}
{% if entities %}
{% if s.id == entities[y].subjects.id and entities[y].exams.id == exams[i-1].id %}
<td class="fz_t_e" data-val="{{ entities[y].marks }}" data-id="{{entities[y].id}}" data-exams="{{exams[i-1].id}}" data-student="{{student.id}}" data-subject="{{s.id}}">
{{ entities[y].marks }}
</td>
{% set total = total + entities[y].marks %}
{% if len > (y+1) %}
{% set y = y+1 %}
{% endif %}
{% else %}
<td class="fz_t_e" data-val="null" data-id="null" data-exams="{{exams[i-1].id}}" data-student="{{student.id}}" data-subject="{{s.id}}">-</td>
{% endif %}
{% else %}
<td class="fz_t_e" data-val="null" data-id="null" data-exams="{{exams[i-1].id}}" data-student="{{student.id}}" data-subject="{{s.id}}">-</td>
{% endif %}
{% else %}
<td>{{ total }}{% set gtotal = gtotal+total %}{% set total = 0 %}</td>
{% if entities %}
{% if s.id == entities[y].subjects.id and entities[y].exams.id == exams[i-1].id %}
<td class="fz_t_e" data-val="{{ entities[y].marks }}" data-id="{{entities[y].id}}" data-exams="{{exams[i-1].id}}" data-student="{{student.id}}" data-subject="{{s.id}}">
{{ entities[y].marks }}
</td>
{% set total = total + entities[y].marks %}
{% if len > (y+1) %}
{% set y = y+1 %}
{% endif %}
{% endif %}
{% else %}
<td class="fz_t_e" data-val="null" data-id="null" data-exams="{{exams[i-1].id}}" data-student="{{student.id}}" data-subject="{{s.id}}">-</td>
{% endif %}
{% set terms = exams[i-1].term %}
{% set x = 1 %}
{% endif %}
{% if i == len_e %}
<td>{{ total }}{% set gtotal = gtotal+total %}{% set total = 0 %}</td>
<td>{{ gtotal }}{% set gtotal = 0 %}</td>
{% endif %}
{% endfor %}
</tr>
{% endfor %}
<tr class="totalColumn">
<td>Total</td>
{% for i in 1..len_e+4 %}
<td></td>
{% endfor %}
</tr>
{#<tr class="totalGrade">
<td>Grade</td>
{% for i in 1..len_e+4 %}
<td></td>
{% endfor %}
</tr>#}
</tbody>
</table>
</div>
{% else %}
<h3 class="text-center">Exam Table Not Updated</h3>
{% endif %}
</div>`
I don't know what data structure you're using to memorize your ints, but you could start with something like this:
<table>
{% for r in rows %}
<tr>
{% set rowTotal = 0 %}
{% for c in r.columns %}
{% set rowTotal = rowTotal + c.value %}
<td>{{ c.value }}</td>
{% endfor %}
<td>{{ rowTotal }}</td>
</tr>
{% endfor %}
</table>

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