I fill my table with data in this way
<tbody>
{% for post in posts %}
<tr>
<td>{{ post.title }}</td>
<td>{{ post.status }}</td>
</tr>
{% endfor %}
</tbody>
I need translate values from status, but it does not work. post.status has only 2 values in database PO_DRAFT and PO_ACCEPT and I need it translate. Is it possible?
I tried in this way but it is wrong
<td>{% trans %}{{ post.status }}{% endtrans %}</td>
Twig's i18n documentation mentions a trans filter, so maybe this would work?
{{ post.status | trans }}
Related
I have a problem on a personal project and need your help.
I have an entity bien, users can list all bien entities from the database in a table with all the fields of the entity. I'd like to make an administration page where people with proper access can choose the fields to display on this listing.
I created a system with a JSON file ("field" => <boolean>). I can get the admin choice and create the appropriate JSON, however there is a problem when I try to display the list with only the fields that are set to true. I wanted to display it with something like this: (you can see the former way to display the table which is now commented)
{% for bien in biens %}
<tr>
{% for categorie, affichage in donnee %}
{% if affichage %}
<td>{{ bien.{{ categorie }}}}</td>
{% endif %}
{% endfor %}
{# former way #}
<td>{{ bien.id }}</td>
<td>{{ bien.titre }}</td>
<td>{{ bien.description }}</td>
<td>{{ bien.type }}</td>
<td>{{ bien.surfaceHabitable }}</td>
<td>{{ bien.nombrePiece }}</td>
<td>{{ bien.nombreEtage }}</td>
<td>{% if bien.sousSol %}Yes{% else %}No{% endif %}</td>
<td>{{ bien.prix }}</td>
<td>{{ bien.honoraire }}</td>
<td>{{ bien.charge }}</td>
<td>{{ bien.adresse }}</td>
<td>{{ bien.codePostal }}</td>
<td>{{ bien.ville }}</td>#}
<td>
<ul>
<li>
Voir
</li>
<li>
Editer
</li>
</ul>
</td>
{# end former way #}
</tr>
{% endfor %}
But I have an error on the line
<td>{{ bien.{{ categorie }}}}</td>
Expected name or number.
I think I am not making it the right way (I also tried {{ bien.categorie }}without goodthe proper result).
Thank you for your help ;)
its not the first time I met this issue and I cannot fix it!
Actually, I'm rendering a template with a controller who give to the rendered page many variables. One of them, called $categories, in it, There are many of Category objectes so, one of them its a Collection what references to another Category.
The point is, I'm try to do this code, but obviusly I get an error because im trying to print as a String a Collection
{% for category in categories %}
<tr>
<td>{{ category.name }}</td>
<td>{{ category.description }}</td>
<td>{{ category.isPublic }}</td>
<td>{{ category.parentCategory }}</td>
<td>{{ category.childrens }}</td>
<td>
<i class="fa fa-pencil-square-o" aria-hidden="true"></i>
<i class="fa fa-times" aria-hidden="true"></i>
</td>
</tr>
{% endfor %}
So, I decided to try something like:
{% for category in categories %}
<tr>
<td>{{ category.name }}</td>
<td>{{ category.description }}</td>
<td>{{ category.isPublic }}</td>
<td>{{ category.parentCategory }}</td>
<td>
{% for children in {{ category.childrens }} %}
children.name
{% endfor %}
</td>
<td>
<i class="fa fa-pencil-square-o" aria-hidden="true"></i>
<i class="fa fa-times" aria-hidden="true"></i>
</td>
</tr>
PROBLEM:
I don't know how use a rendered variable in a foreach, im gettin this error:
A hash key must be a quoted string, a number, a name, or an expression enclosed in parentheses (unexpected token "punctuation" of value "{" in AppBundle:admin:category/listCategory.html.twig at line 55.
{{ }} or {% %} or {# #} are Twig's open and close tags. Similair to <?php ?> in PHP code. Once you use an open tag, the text is parsed by Twig until the close tag is found (this is also how things are done in PHP, the only difference is that Twig has a different tag to echo stuff).
Once open, you don't have to reopen it again. You don't want to dump category.childrens, you want to use it in the for loop. So instead of doing: {% for children in {{ category.childrens }} %}, use {% for children in category.childrens %}.
(you can compare this to PHP, doing
<?php foreach (<?php echo $category->childrens ?> as $children) { ?>
doesn't make much sense).
The error probably comes from this line:
{% for children in {{ category.childrens }} %}
This is not a valid syntax, the {{ }} can't be used inside another Twig tag.
The following code should work:
{% for children in category.childrens %}
To be honest I have 0 experience with the twig templating langauge, so I may be wrong here, but my experience with other languages tells me the following code:
{% for children in {{ category.childrens }} %}
children.name
{% endfor %}
Should probably look like this:
{% for children in category.childrens %}
{{ children.name }}
{% endfor %}
I'm not sure why my code isn't executing. I know that it should be working this way but all that happens now is that it doesn't do the else portion.
In debugging I know descriptions is not null and descriptions show for those that have it.
{% if descriptions is not null %}
{{ dump(descriptions) }}
{% for description in descriptions %}
<td>{{ description.productDesciption }}</td>
{% endfor %}
{% else %}
<td>
Create a Description for this Product
</td>
{% endif %}
You can simplify using the The else Clause of the for statement:
{% for description in descriptions %}
<td>
{{ description.productDesciption }}
</td>
{% else %}
<td>
Create a Description for this Product
</td>
{% endfor %}
Hope this help
You can use if into for loop.
{% for description in descriptions if descriptions is not null %}
<td>
{{ description.productDesciption }}
</td>
{% else %}
<td>
Create a Description for this Product
</td>
{% endfor %}
I have a strange situation. My code is :
{% set total_amount=0 %}
{% for result in a_result %}
<tr>
<td>{% set total_amount=total_amount+("%.2f"|format(result.tva*result.prix_ht)) %}
{{ "%.2f"|format(result.tva*result.prix_ht) }}
</td>
/tr>
{% endfor %}
<tr>
<td colspan="5">Total</td>
<td>{{ total_amount }}</td>
</tr>
As result I have :
15.98, 25.49, 25.49
And Total = 65 but total should be equal with 65.96. I don't understand where is the problem. Can you help me please ?
I suggest you to use the round and number_format filter as follow:
{% set total_amount=0 %}
{% for result in a_result %}
{% set value = (result.tva* result.prix_ht)|round(2) %}
{% set total_amount=total_amount+value %}
<tr>
<td>
{{ value|number_format(2, '.', ',') }}
</td>
/tr>
{% endfor %}
<tr>
<td colspan="5">Total</td>
<td>{{ total_amount|number_format(2, '.', ',') }}</td>
</tr>
A running example with sample data in this twigfiddle files
Hope this help
In versions < 2.3, the attribute
data-prototype="{% filter escape %}{% include 'ManaClientBundle:Member:member_prototype.html.twig' with {'form': form.members.get('prototype')} %}{% endfilter %}"
allowed the prototype to display as a row of cells. In 2.3, of course, this line generates the error
Method "get" for object "Symfony\Component\Form\FormView" does not
exist
Per UPGRADE-2.1.md, get is deprecated. It cannot be replaced by the suggested vars as it produces a similar error. How can I (easily?) render the prototype as before in 2.3? Using
data-prototype="{{ form_widget(form.members.vars.prototype)|e }}"
does not provide a useful solution regardless of div or table settings for twig:resources:form in config.yml.
prototype
<tr id="member-form">
<td><input class="smallform" type="radio" name="household[isHead]" value="__name__"></td>
<td>{{ form_widget(form.include, {'attr': {'class':'smallform'}}) }}</td>
<td>{{ form_widget(form.fname, {'attr': {'class':'smallform'}}) }}</td>
<td>{{ form_widget(form.sname, {'attr': {'class':'smallform'}}) }}</td>
<td>{{ form_widget(form.dob, {'attr': {'class':'smallform'}}) }}</td>
<td>{{ form_widget(form.sex, {'attr': {'class':'smallform'}}) }}</td>
<td>{{ form_widget(form.ethnicity, {'attr': {'class':'smallform'}}) }}</td>
<td><a id="removeTr" class="smallbutton" href="#" onclick="removeTr(this)">Remove</a>
Replace form.members.get('prototype') with form.members.vars.prototype.
Could have been a whole lot clearer what to do, but fewer than infinite monkeys found a solution.
Above won't work for me when extending block.
In 2.3.7 at least, I was able to access vars like:
{% block form_row %}
{{ form.vars.value }}
.....
{% endblock %}
And when an ArrayCollection like:
{% block sonata_type_collection_widget %}
{% for child in form %}
{{ child.vars.form.vars.value.name }}
{% endfor %}
{% endblock %}
Hope that helps someone.