Pergunta

Using twig, how can I translate all items in an array and join them with a slash? Do I have to use an additional variable or is there a cleverer method?

For the moment, I'm doing something like this:

{% set labels = [] %}
{% for feature in menu_item.features %}
  {% set labels = labels|merge([feature|trans([], 'features')]) %}
{% endfor %}
{{ labels | join(' / ')}}

It sucks.

Foi útil?

Solução

Why not just output the content while you're looping ?

{% for feature in menu_item.features %}
  {% if loop.index0 > 0 %}/{% endif %}
  {{feature|trans}}
{% endfor %}

Outras dicas

Not everything should be done within the "view".

This type of code is probably much better placed within your controller logic and then passed into the view as the merged+joined result. Because in your example all you're doing is compiling a result which can much more easily be done within code.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top