get fields of an object with twig - php

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 ;)

Related

Symfony get username from userid in twig template

I'm trying to make a table with all entities from the database. This works fine, but in the entity is only the user id saved.
How I can fetch the username?
{% for invite in invites %}
<tr>
<td>{{ invite.code}}</td>
<td>{{ invite.creator }}</td> {# This line returns the user id, but how I can get the username? #}
<td>{{ invite.creationdate|format_datetime(locale='de') }}</td>
<td><a class="btn btn-danger btn-sm" href="{{ path('admin.invite.delete', {'id': invite.id}) }}">
<i class="fas fa-trash">
</i>
Delete
</a>
</td>
</tr>
{% endfor %}
If invite.creator is object, then you can access to its properties.
{{ invite.creator.username }} {# name property of User::class #}
If it is id only, then you need to pass array 'userIdToUser' into twig from your contoller
{{ userIdToUser[invite.creator] }}

translate tabledata in twig

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

How use rendered variables in a foreach Symfony 2 ~ twig

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

Count content of columns in table symfony2/twig/doctrine

So I have this table created with a crud in my symfony2 project. The table displays different products with each their own price. I would like to have a output with the total value of all products at the bottom of the table.
index.html.twig of entity "waarde"
{% extends '::base.html.twig' %}
{% block body -%}
<h1>Waarde voorraad</h1>
<table class="records_list">
<thead>
<tr>
<th>Product</th>
<th>Type</th>
<th>Fabriek</th>
<th>Aantal</th>
<th>Prijs</th>
<th>Inkoop Waarde</th>
<th>Verkoop Waarde</th>
<th>Locatie</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
{% for entity in entities %}
<tr>
<td>{{ entity.getProduct().getNaam() }}</td>
<td>{{ entity.getProduct().getType() }}</td>
<td>{{ entity.getProduct().getFabriek() }}</td>
<td>{{ entity.aantal }}</td>
<td>{{ entity.getProduct().getInkoopprijs() }}</td>
<td>{{ entity.getProduct().getInkoopprijs() * entity.aantal }}</td>
<td>{{ entity.getProduct().getVerkoopprijs() * entity.aantal }}</td>
<td>{{ entity.getLocatie().getLocatienaam() }}</td>
<td>
<ul>
<li>
show
</li>
<li>
edit
</li>
</ul>
</td>
</tr>
{% endfor %}
</tbody>
</table>
<ul>
<li>
<a href="{{ path('waarde_new') }}">
Create a new entry
</a>
</li>
</ul>
{% endblock %}
I have a association between the entities "Product" and "Waarde"(=value).
<td>{{ entity.getProduct().getInkoopprijs() }}</td>
The code above returns the price of a product. I already have done a simple code to calculate the value of a single product by this code ("aantal" = quantity/amount of products)
<td>{{ entity.getProduct().getInkoopprijs() * entity.aantal }}</td>
{% set sum = 0 %}
{% for entity in entities %}
{% set sum = sum + (entity.getProduct().getInkoopprijs() * entity.aantal) %}
{% endfor %}
Total price: {{ sum }}

Symfony2: deprecated get in FormView replacement?

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.

Categories