문제

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.

도움이 되었습니까?

해결책

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

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

다른 팁

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top