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.
Related
How to display simple OneToMany children relation table in EasyAdmin 3 Symfony parent details page?
I trying to display table(list) of children in new panel, and cant figure it out.
You can use a template for the field
{# #var ea \EasyCorp\Bundle\EasyAdminBundle\Context\AdminContext #}
{# #var field \EasyCorp\Bundle\EasyAdminBundle\Dto\FieldDto #}
{# #var entity \EasyCorp\Bundle\EasyAdminBundle\Dto\EntityDto #}
{% if ea.crud.currentAction == 'detail' %}
<table>
<tr>
<th>Some Field</th>
<th>Some Other Field</th>
</tr>
{% for value in field.value %}
<tr>
<td>{{ value.someField }}</td>
<td>{{ value.someOtherField }}</td>
</tr>
{% endfor %}
</table>
{% else %}
<span class="badge badge-secondary">{{ field.formattedValue }}</span>
{% endif %}
And then just add this template to it
yield CollectionField::new('children')
->setTemplatePath('admin/fields/children.html.twig')
;
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
I am trying to setup a custom symfony form collection prototype component of a form. I am referencing the proper documentation
https://symfony.com/doc/3.3/form/form_customization.html#how-to-customize-a-collection-prototype
The form collection is setup using the following
<table class="table table-bordered" data-prototype="{{ form_row(form.quoteItemDeliverables.vars.prototype)|e('html_attr') }}">
<caption>Deliverables</caption>
<tr>
<th>Quantity</th>
<th>Date Required</th>
</tr>
{% for itemDeliverable in form.quoteItemDeliverables %}
<tr>
<td>{{ form_widget(itemDeliverable.quantity) }}</td>
<td>{{ form_widget(itemDeliverable.dateRequired) }}</td>
</tr>
{% endfor %}
</table>
The twig block that is referencing the prototype is setup using the following
{% form_theme form _self %}
{% block _uniflytebundle_quoteitem_quoteItemDeliverables_entry_row %}
<tr>
{#<td>{{ form_widget(form.quoteItemDeliverables.vars.prototype.quantity) }}</td>#}
</tr>
{{ dump(form.children["quoteItemDeliverables"]) }}
{% endblock %}
The dump(); is returning the following error
Key "quoteItemDeliverables" for array with keys "quantity, dateRequired" does not exist.
What am I doing wrong?
If I dump(form); I get the form object displaying the children, the "quoteItemDeliverables" and the "prototype" elements
Can someone please point me in the right direction on how to access the various form properties? Trying to do what was done in the collection form for the prototype. Below being working form collection elements.
<td>{{ form_widget(itemDeliverable.quantity) }}</td>
<td>{{ form_widget(itemDeliverable.dateRequired) }}</td>
I would like the same for the prototype twig block but using something like
form_widget(itemDeliverable.prototype.dateRequired)
does not work. How can I do this?
Thank you in advance for your time and effort invested.
Inside the twig block, form does not refer to the "global" form but to the sub form corresponding to the entry of your collection. That is why the error produced by the dump call indicates that the array has quantity and dateRequired keys but no quoteItemDeliverables.
Thus what you should have is more likely to be something like:
{% block _uniflytebundle_quoteitem_quoteItemDeliverables_entry_row %}
<tr>
<td>{{ form_widget(form.quantity) }}</td>
<td>{{ form_widget(form.dateRequired}}</td>
</tr>
{% endblock %}
I would like to create a gallery with three columns and I have used twig batch filter. Is it possible to add an if condition so that all disabled images won't be listed? My example is not working:
<table>
{% for items in content.galleries|batch(3) if items.enabled %}
<tr>
{% for cell in items %}
<td><img src="{{ asset(cell.image) }}"></td>
{% endfor %}
</tr>
{% endfor %}
</table>
I am using knp paginator and it works well but when I want to use its sorting feature I have problem to get the sort direction in twig.
the following code is indicate how to get the sorted table header but not taking about how to get sorted table header direction.
{# total items count #}
<div class="count">
{{ pagination.getTotalItemCount }}
</div>
<table>
<tr>
{# sorting of properties based on query components #}
<th>{{ knp_pagination_sortable(pagination, 'Id', 'a.id') }}</th>
<th{% if pagination.isSorted('a.Title') %} class="sorted"{% endif %}>{{ knp_pagination_sortable(pagination, 'Title', 'a.title') }}</th>
</tr>
{# table body #}
{% for article in pagination %}
<tr {% if loop.index is odd %}class="color"{% endif %}>
<td>{{ article.id }}</td>
<td>{{ article.title }}</td>
</tr>
{% endfor %}
</table>
{# display navigation #}
<div class="navigation">
{{ knp_pagination_render(pagination) }}
</div>
I get this code from KnpPaginator documentation on following link:
https://github.com/KnpLabs/KnpPaginatorBundle
You should be able to just use {{ pagination.getDirection() }} in your twig template to find the current sort direction (if any) then set up your classes based on that.
{% set direction = pagination.getDirection() %}
<th{% if pagination.isSorted('p.id') %} class="sorted {{ direction }}"{% endif %}>
{{ knp_pagination_sortable(pagination, 'Id', 'p.id') }}
</th>
But... as of this post, KNP has not yet merged this fix:
https://github.com/sroze/KnpPaginatorBundle/commit/3105a38714c6f89c590e49e9c50475f7a777009d
When there is no direction parameter set, the current Paginator bundle throws an error.
So, until the above fix is merged, you can still get the direction with a bit more verbosity:
{% set directionParam = pagination.getPaginatorOption('sortDirectionParameterName') %}
{% set params = pagination.getParams() %}
{% set direction = params[directionParam] is defined ? params[directionParam] : null %}
<th{% if pagination.isSorted('p.id') %} class="sorted {{ direction }}"{% endif %}>
{{ knp_pagination_sortable(pagination, 'Id', 'p.id') }}
</th>
When you call {{ knp_pagination_sortable(pagination, 'Id', 'a.id') }}, bundle automatically generates link with a class holding an information about sort direction, which looks something like this: <a translationcount="" class="asc" href="?sort=a.id&direction=desc&page=1" title="Id">Id</a> So just put this class in your css file and style it with the arrow. If you, for some reason, need to get a sort direction inside a controller, just read it from request $request->query->get('direction').