How to get Pagination direction in twig for KNP paginator? - symfony

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').

Related

Duplicate form row in twig template symfony 4

I want to duplicate each form row inside my template by using for loop
this is my code, but it's doesn't work, it's returned only the first line with form and those other without form, please could somebody help me ?
{% for commande in commandes %}
<tr><td>{{ form_row(form.id, {'attr': 'value': commande.id} ) }}</td></tr>
<tr><td>{{ form_row(form.number, {'attr': 'value': commande.number} ) }}</td></tr>
<tr><td>{{ form_row(form.date, {'attr': 'value': commande.date} ) }}</td>
<td><button type="submit" name="btn">update</td>
</tr>
{% endfor %}
Forms are sometimes smart and know they have been rendered.
Workaround:
{% set output_id = form_row(form.id, {'attr': 'value': commande.id}) %}
{{ output_id }}
and later on you can access it again:
{{ output_id }}
You can also do this for more than a form row (or for adding markup):
{% set output %}
{{ form_row(form.id) }}
{{ form_row(form.number) }}
{{ form_row(form.date) }}
{% endset %}
{{ output }}
(Alternatively, you can probably set the rendered attribute to false or something, but to be honest, that seems worse to me than capturing the output).

Dyanamic Variable names in Twig

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

Twig template variable of variable

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.

Twig - display a property inside an entity item

I have an entity that has an items property that is an array of item entities. the item entity has id and name properties.
what I want to do is to get entity.items and display all name properties, separated by commas.
the way I have it now:
<tr>
<th>Items</th>
<td>
{% for item in entity.items %}
{{ item.name }}
{% endfor %}
</td>
</tr>
But it is not separated by commas. I tried the Join filter, but I can't find a way to use it in this situation, since I have an array of objects.
You can combine twig syntax with regular HTML. The {%%} markup indicates tags, telling the twig that there's some rendering logic, but you don't need to write strictly twig syntax inside the tags. So:
{% for item in entity.items %}
{{ item.name }}{% if not loop.last %}, {% endif %}
{% endfor %}
will work just fine

Symfony2 presenting an array value as a translated text

it does not seem possible to use a value i am getting right from an array, as a translated text....so when i have
{% for key,value in ratings %}
<th scope="row">
{% trans %}
{{ value.type }}
{% endtrans %}</th>
<td ><div class="rating" id= "{{ value.type }}" data-rating="{{ value.ratingaverage }}"
thread="{{thread_id}}" rating_readonly= "{{ value.readOnly }}" route="{{ path('addrating') }}" ></div> </td>
{% endfor %}
i get the error
A message must be a simple text in TrackerMembersBundle::rating.html.twig at line 92
what is meant here, is the line with
{% trans %}
{{ value.type }}
{% endtrans %}
i cannot seem to be able to use trans upon the value coming directly from array? the value would be for example "file.quality"
Perhaps its better when you search here before.
Symfony2+Twig, variable in translation return "A message must be a simple text"
The mistake is the variable in the translation block. You have to set an placeholder and replace this with a value.
{% trans with {'%type%':value.type} %}
This is my %type%!
{% endtrans %}

Resources