I found the difference between two dates and now I am trying to sum up all the differences. Can someone help me?
{% for clock in clock %}
{% set difference = date(clock.dateTimeStart).diff(clock.dateTimeEnd).format('%H:%I:%S') %}
<tr>
<td>{{ clock.dateTimeStart|date("d/m/Y") }}</td>
<td>{{ clock.dateTimeStart|date("H:i:s") }} to {{ clock.dateTimeEnd|date("H:i:s") }}</td>
<td>{{ difference }}</td>
</tr>
{% endfor %}
You can add DateTimeInterval's by adding them into a DateTime object and testing the difference at the end again.
{% set total_start = date('00:00') %}
{% set total_end = date('00:00') %}
{% for range in data %}
{% set difference = date(range.start).diff(date(range.end)) %}
{% do total_end.add(difference) %}
{{ difference.format('%H:%I:%S') }}
{% endfor %}
Total difference: {{ total_start.diff(total_end).format('%H:%I:%S') }}
demo
Related
I have 2 tables related, InProducto and InUnidadMedida, which InProducto is related ManyToOne with InUnidadMedida.
I want to know how can I get the name of my related ID to show it in the list.
Index twig
{% for entity in entities %}
<tr>
<td>{{ entity.idProducto }}</td>
<td>{{ entity.nombre }}</td>
<td>{{ entity.idUnidadMedida }}</td>
<td>{{ entity.costoPromedio }}</td>
<td>{{ entity.idSubLinea }}</td>
<td>{{ entity.idTipoProducto }}</td>
<td>{{ entity.precio1 }}</td>
<td>
{% if entity.inventariable == 0 %}
No
{% elseif entity.inventariable == 1 %}
Sí
{% endif %}
</td>
<td>
{% if entity.facturable == 0 %}
No
{% elseif entity.facturable == 1 %}
Sí
{% endif %}
</td>
<td>
{% if entity.activo == 0 %}
No
{% elseif entity.activo == 1 %}
Sí
{% endif %}
</td>
<td class = "actions">
ver
editar
</td>
</tr>
{% endfor %}
Where {{ entity.idSubLinea }} and {{ entity.idUnidadMedida }} are the related fields and I want to display the names of each category.
Currently it prints the ID.
It's just {{ entity.unidadMedida.name }} (given the property is $name)
You can also make it a bit simpler by making just {{ entity.unidadMedida }} and making a method __toString in InUnidadMedida Entity
...
public function __toString()
{
return $this->name;
}
...
For a series of similar named string how to I case them to a variable similar to php's $$variable ?
I have tried
{% for col in 1..cols %}
{% set field = 'field'~col %}
<td>
{{ form_widget(form.field) }}
</td>
{% endfor %}
But I get error property does not exist.
Solved it:
{% for col in 1..cols %}
{% set field = 'field'~col %}
<td>
{{ form_widget(attribute(form,field)) }}
</td>
{% endfor %}
I am new to Twig and Symfony2. I was wondering how can I create a 3 column table with Twig. My data comes from a database
So far I've tried everything but still nothin works. I found this on Stackoverflow about making a 2 column table, and it worked perfectly except for me. I want 3 columns .
<table>
{% for var in var1 %}
{% if (loop.index % 2) %}<tr>{% endif %}
<td>
<div class="bloc">
<a href="{{ path('xxxxxxx', {'id':var.id}) }}">
<span>{{ var.name}} </spann></a></div>
<img src="{{ asset(var.image ) }}" />
</div>
</td>
{% if (loop.index % 2) and loop.last %}
<td> </td>
{% endif %}
{% if (loop.index0 % 2) or loop.last %}</tr>{% endif %}
{% endfor %}
</table>
ex: var1 contains names and pictures from database.
name1 name2 name3
name4 name5 name6
...
This is what I have ATM
name1 name2
name3 name4 name5
name6 name7 name8
My solution works for any number of columns:
{% set columns = 3 %}
{% for name in names %}
{% if loop.first or loop.index0 is divisibleby(columns) %}
<tr>
{% endif %}
<td>{{ name }}</td>
{% if loop.last and loop.index is not divisibleby(columns) %}
{% for n in range(low=columns - (loop.index % columns), high=1, step=-1) %}
<td> </td>
{% endfor %}
{% endif %}
{% if loop.last or loop.index is divisibleby(columns) %}
</tr>
{% endif %}
{% endfor %}
Correct way are you use batch (array_chunk):
{% for batchResults in result.items|batch(result.total_results / columns) %}
<div class="{{cycle(['left', 'left', 'right'], loop.index)}}">
{% for item in batchResults %}
<div class="{% if loop.last %}last{% endif %}">
{{item}}
</div>
{% endfor %}
</div>
{% endfor %}
Peekmo's solution is correct but there is possibly a neater way using the batch filter in twig. this filter breaks a list into smaller subsets. You control the size and use nested for loops to display rows and their contents. The example in the docs is an exact answer to this problem. The filter will also neatly handle empty cells. Ie you have 8 values in an array and you want a 3 column table, the last cell will be emtpy
http://twig.sensiolabs.org/doc/filters/batch.html
You should try something like that :
<table>
{% for var in var1%}
<tr>
<td>Title1<td>
<td>Title2<td>
<td>Title3<td>
</tr>
<tr>
<td>{{ var.attr1 }}<td>
<td>{{ var.attr2 }}<td>
<td>{{ var.attr3 }}<td>
</tr>
{%endfor%}
</table>
<table>
<tr>
{% for var in var1%}
{% if loop.index0 is divisibleby(3) %}
</tr>
<tr>
{% endif %}
<td>{{ var }}</td>
{% if loop.last %}
</tr>
{% endif %}
{%endfor%}
</table>
I think that could work for your problem, you have to open tag every 3 iterations, and do not forget to close the last one when your loop is terminated.
Untested, but should print a clean table.
<table>
{% for var in var1 %}
{% if not (loop.index0 % 3) %}<tr>{% endif %}
<td>
<div class="bloc">
<a href="{{ path('xxxxxxx', {'id':var.id}) }}">
<span>{{ var.name}} </spann></a></div>
<img src="{{ asset(var.image ) }}" />
</div>
</td>
{% if (loop.index % 2) and loop.last %}
<td> </td>
{% endif %}
{% if (loop.index % 3) and loop.last %}
<td> </td>
{% endif %}
{% if not (loop.index % 3) or loop.last %}</tr>{% endif %}
{% endfor %}
</table>
I want to total count resuslt. I have already done it like total group result and In total result in one product....
like:-
Total Group of Product = 15
In one group showing 36 product.
In second group showing 56 product.
I want to show total group product result = 36+56 => 92.
without for loop.
twig file:
<table>
<h2> Showing {{ templates | length }} Products</h2>
{% for groupResults in templates %}
{% if groupResults.doclist.docs[0].first_product_name is defined %}
Show all {{ groupResults | length }} results
<tr>
<td>
{{ groupResults.doclist.docs[0].first_product_name }}
{% if groupResults.doclist.numFound > 4 %}
Show all {{ groupResults.doclist.numFound }} results
{% endif %}
</td>
</tr>
<tr>
{% set count = 0 %}
{% for template in groupResults.doclist.docs %}
<td>
<a href="{{ path("customer_design_editor", { "templateSlug": template.slug, "productSlug": template.product_slug[0] }) }}">
<img src="{{ path("design_template_thumbnail_by_slug", { "slug": template.slug}) }}" alt="" />
</a>
</td>
{% set count = count + 1 %}
{% endfor %}
{% if count < 4 %}
{% for i in count..3 %}
<td> </td>
{% endfor %}
{% endif %}
{% else %}
{% endif %}
{% endfor %}
How about:
{% set totalCount = firstGroup|length + secondGroup|length %}
I have double for inside a twig template:
<table>
{% for user in users %}
<tr>
{% for field in fields %}
<td>{{ user.{{field}} }}</td>
{% endfor %}
</tr>
{% endfor %}
</table>
this can be done? what would be the correct syntax for {{ user.{{field}} }}?
The attribute function does this, it can be used to access a "dynamic" attribute of a variable:
<table>
{% for user in users %}
<tr>
{% for field in fields %}
<td>{{ attribute(user,field) }}</td>
{% endfor %}
</tr>
{% endfor %}
</table>
So the correct syntax is {{ attribute(user,field) }}, read the documentation here
If the user has one-to-many relationship with field (i.e. one user can have multiple fields), and it's defined as such in ORM mapping and in the entities (i.e. the User entity has a getFields() method, which returns a collection of Field entities), then you can do this:
<table>
{% for user in users %}
<tr>
{% for field in user.fields %}
<td>{{ field }}</td>
{% endfor %}
</tr>
{% endfor %}
</table>
But if that's not the case, then it might help to describe a little bit more about what kind of relationship a User has with its fields.