php - Twig use specific value for 0 results, 1 result and multiple results -


while working on article module in symfony2, have display how many times read. make 'sentence' grammatically correct used code below.

this works obvious botters me can't find shorter, cleaner way. there article.getreads|length|displayresult('no results', '%d result', '%d results) available or have make on own?

{% if article.getreads|length == 0 %}be first 1 read this! {% else %}   {{ article.getreads|length|number_format(0, ',', '.') }}    read{% if article.getreads|length != 1 %}s{% endif %} {% endif %} 

you can use symfony2 pluralization translate component described here.

as example can declare file like:

#src / acme / demobundle / resources / translations / messages.en.xliff              <trans-unit id="11">                 <source>article.read</source>                 <target>{0} no results|{1} 1 result|[2,inf] results</target>             </trans-unit> 

ad use follow in twig template:

{{ 'article.read'|transchoice(article.getreads|length) }} 

hope help


Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -

How to provide Authorization & Authentication using Asp.net, C#? -