Twig generate <td> in <tr> based on an integer value - php

search over this in google and looked the official docs of twig, didn't find an answer,
I have this
$data['count'] = $count;//6
in my php code,
I want to generate td as much as the $beltCount in the twig file I tried this but it didn't work:
{% for i in count%}
<td> {{ i }}</td>
{% endfor %}
Should I do this in a different way? I didn't find a syntax for this
Thanks

The integer 6 isn't enumerable, it's just a number.
You can however use range(1, count) or, as stated Teneff in comments, the range operator 1..count to create an enumerable from 1 to 6 (or whatever value is in count) :
{% for i in range(1, count) %}
<td> {{ i }}</td>
{% endfor %}
{% for i in 1..count %}
<td> {{ i }}</td>
{% endfor %}
fiddle
You will find more infos in the documentation

Related

How to get ID from symfony Form Field to fill out rest of form

I would appreciate any kind of help for this problem I'm having.
I have 2 Symfony Forms :
One for the search part (Serial Number)
Another form that will be filled out if the Serial Number exists.
I need to get dynamicly the value of the ID.
Here is a snippet of my code :
{% for ClientInfo in getInstallationClientBySerialNumber %}
<tr id="ClientInfo_{{ installClientInfo.idRma }}">
<td>{{ ClientInfo.numeroSerie }}</td>
{% endfor %}
The loop works but I'm struggling with the JQuery part to get the ID. I've tried :
$("#client_sn").click(function(){
var elmId = $("#ClientInfo_id").attr("id");
alert(elmId);
});
Any help would be very kind.
Many Thanks in advance.
HTML ID's have to be unique on a page. You're looping over multiple elements, giving them all the same ID "client_sn"
What you can do with Twig in a for loop is access loop.index as a counter
https://twig.symfony.com/doc/2.x/tags/for.html#the-loop-variable
{% for ClientInfo in getInstallationClientBySerialNumber %}
<tr id="ClientInfo_{{ installClientInfo.idRma }}">
<td>{{ ClientInfo.numeroSerie }}</td>
{% endfor %}
In the jQuery you can then use a pattern to match all elements with an ID starting client_sn:
$("a[id^='client_sn']").click(function(){
var elmId = $("#ClientInfo_id").attr("id");
alert(elmId);
});
https://api.jquery.com/attribute-starts-with-selector/
Or use some other selector to identify the links.
Alternatively instead of {{loop.counter}} you could reuse the installClientInfo.idRma ID you're also using:
{% for ClientInfo in getInstallationClientBySerialNumber %}
<tr id="ClientInfo_{{ installClientInfo.idRma }}">
<td>{{ ClientInfo.numeroSerie }}</td>
{% endfor %}

Print Twig variable x times based upon randomized range

I am using Twig and Timber for a WordPress project. I have the following loop in my template that prints my custom post type titles into a HTML structure.
{% for company in companies %}
{% set dot = "<div class='company-dot'></div>" %}
{% set range = range(10, 20) %}
{{dot}}
{{random(range)}}
<div class="company">
<div class="company-dot dot-active"></div>
<p class="dot-caption">{{ company.title }}</p>
</div>
{% endfor %}
I would like to print my {{dot}} variable x amount of times based upon the number that is generated by {{random(range)}}. How can I do this?
The simplest solution would be to iterate random(range) times with a for loop:
{% for i in 0..random(range(10, 20)) %}
{{ dot }}
{% endfor %}
I don't really know Twig but my guess is that you could to the following:
{% for i in random(range) %}
{{dot}}
{% endfor %}
You already know how to use range, just use it again :
{% set dots_count = random(range) %}
{% for dot_index in range(1,dots_count) %}
{{dot}}
{% endfor %}
{{dots_count}}
Here is a fiddle : https://twigfiddle.com/ko595z

PHP - Twig change array key value

I am new to Twig and looking for a solution for the following (which would be very easy in PHP, however, our templates are setup in Twig)
What I'm trying to do
Edit an array value (using its index) in Twig so that it can be output after a loop.
What is happening
The array value (retrieved using its index) does not change when I try to edit the array value by index. Instead, it may append the value to the array
My code
...
{% set amount = [0,0,0] %}
{% for invoice in invoices %}
<tr>
<td>{% if invoice.age <= 10 %}{% set amount ?????? %}{% endif %}</td>
<td>{% if invoice.age > 10 and invoice.age <= 20 %}{% set amount ?????? %}{% endif %}</td>
<td>{% if invoice.age > 20 %}{% set amount ?????? %}{% endif %}</td>
</tr>
{% endfor %}
...
{{ amount[0] }}
What I've tried
I have tried the following to change the value of age[0] to no avail.
{% set amount = amount|merge({0: 'test'}) %}
{% set amount = amount|merge({0: 'test'})|keys %}
{% set amount = amount|merge({(0), 'test'}) %}
{% set amount = amount|merge({(0), 'test'})|keys %}
... and many more.
Intended outcome
I want to be able to output {{ age[0] }} at the end to display the amount total of all invoices aged 10 or less. Similarly, I would also like to output age[1] and age[2] to display amount total for all invoices aged between 10 and 20 days, and over 20 days, respectively.
In twig, always keep everything simple, without hard logic.
In your case, just create 3 variables.
{% set amount_under_10, amount_between_10_and_20, amount_over_20 = 0,0,0 %}
{% for invoice in invoices %}
<tr>
<td>{% if invoice.age <= 10 %}{% set amount_under_10 = amount_under_10 + 1 %}{% endif %}{{ invoice.age }}</td>
<td>{% if invoice.age > 10 and invoice.age <= 20 %}{% set amount_between_10_and_20 = amount_between_10_and_20 + 1 %}{% endif %}{{ invoice.age }}</td>
<td>{% if invoice.age > 20 %}{% set amount_over_20 = amount_over_20 + 1 %}{% endif %}{{ invoice.age }}</td>
</tr>
{% endfor %}
{{ amount_under_10 }}
{{ amount_between_10_and_20 }}
{{ amount_over_20 }}
See fiddle
If you need to be more generic (arbitrary number of ranges for example) don't do it in Twig. Twig is made for rendering information, no more.
See my answer here for a different approach.
I think I understand the idea behind keeping logic out of templates, but in my case I have SQL queries written by the administrator that are executed and a generic template outputs the results.
It makes more sense to calculate totals in the template.

How do I make a simple count loop in Wordpress Timber(Twigg)?

How do I make a simple count loop in Wordpress Timber(Twigg)?
So basically just a loop like this:
($i = 0;0 < 3;i++){
echo $test[i];
}
You could use
{% for value in test %}
{{ value }}
{% endfor %}
that is safer than
{% for i in 0..2 %}
{{ test[i] }}
{% endfor %}
because in second version you have to care about index (is setted? and so on) whereas in the first you don't.
Of course if your final goal is to print only three elements from the array you should consider slice filter
{% for value in test|slice(0, 3) %}
{{ value }}
{% endfor %}

numeric loop in volt

I have read the volt documentation in phalcon page and i cant find any example for this...
You can make easy loops in objects, for example, in php:
foreach($pages as $page){
echo $page->title;
}
in volts would be ...
{% for page in pages %}
{{ page.title }}
{% endfor %}
My question is, how i can make a normal numerical loop in volt? For example:
for($n=1;$n<10;$n++){
echo $n;
}
Thanks.
This will count from 1 to 10
{% for i in 1..10 %}
{{ i }}
{% endfor %}

Categories