TWIG with double loop - php

I have a problem with show the value from variables. For example, i have function for choose columns which i want to render. I sent the 2 variables for TWIG schema to show. First is variable which stores all data from some table (e.g. user) and second for stored columns which we choosed from this tables to show. Now in the twig and want to do two loops, for show next entity and show columns from entity. The better explanation is below example but this is not working.
{% for user in users %}
{% for column in columnts%}
<li>{{ user.column}}</li>
{% endfor %}
{% endfor %}
Somebody know how can i solve this ?

Try something like that:
{% for user in users %}
{% for column in columns %}
{% if attribute(user, column) is defined %}
<li>{{ attribute(user, column) }}</li>
// or attribute(user, 'get' ~ column|capitalize) if you have getters for your properties
{% endif %}
{% endfor %}
{% endfor %}

What about using the attribute twig helper, which is commonly used to access any dynamic attribute of a given variable.
{% for user in users %}
{% for column in columns %}
<li>{{ attribute(user, column) }}</li>
{% endfor %}
{% endfor %}
Also, "attribute(user, column) is defined" should help you check for the existence of your dynamic attribute/method.

Related

Looping through a limited number of elements inside an array

I have an array of users, how can I make a limited number of iterations using twig.
I have 5 elements inside the array, and i only need to loop through 4 of them.
instead of doing like this:
for user in users
user
endfor
it would be something like this
for ... in 0..3
user
endfor
You can use the slice filter
{% for user in users|slice(0,4) %}
...
{% endfor %}
You can even use the twig shortcut
{% for user in users[0:4] %}
...
{% endfor %}
You can use Twig extension Slice
In example :
{% for user in users|slice(0, -1) %}
...
{% endfor %}
slice(0, -1) means you start from 0 and end to last element - 1

Test existence of a macro in Twig 2

I need to be able to test the existence of a macro in Twig and to call it dynamically.
Here is what I tried:
{% macro test(value) %}
Value: {{ value }}
{% endmacro %}
{% import "_macros.html.twig" as macro %}
{{ attribute(macro, 'test', ['foo']) }}
But I get this error: Accessing Twig_Template attributes is forbidden.
Regards,
Since Twig 1.20.0, template attributes are not available anymore for security reasons, so there are no native way to do it properly.
You can eventually use the source function to get macro's source file and parse it to check if a macro exists, but that's a kind of ugly hack easy to bypass.
Example:
main.twig
{% import 'macros.twig' as macros %}
{% set sourceMacros = source(macros) %}
foo {% if 'foo()' in sourceMacros %} exists {% else %} does not exist {% endif %}
bar {% if 'bar()' in sourceMacros %} exists {% else %} does not exist {% endif %}
macros.twig
{% macro foo() %}
Hello, world!
{% endmacro %}
See this example live
Another approach would be to create a custom test to do the job.

How to automatically check if variables are not empty in Symfony 3?

(Sorry for this bad english)
I would like to know if it's possible to automatically check if variables from MySQL request are not empty in Symfony 3. I know that I can put {% if foo is defined %} in Twig or something like this but I didn't find if there's a way to do it automatically. I can test the request in the controller too. But with those solutions I have to do it for every request.
All my website uses "if not empty then show it" that's why I'm trying to find it.
Edit : I know how to check every fields of every request in Twig or in php (Controller) one by one but there is a lot of duplication code, which is "boring". Thats why I am asking you if something automatic exists to check my data. (parameter in Symfony, ...)
Thank you <3
If I understand, your problem :
You want to iterate on each property in an entity. But you can't, then you are searching for a solution to not write :
{% if entity.property1 %}
{{ entity.property1 }}
{% endif %}
{% if entity.property2 %}
{{ entity.property2 }}
{% endif %}
{% if entity.property3 %}
{{ entity.property3 }}
{% endif %}
You have two solutions to make your properties traversable :
Get your entity with a Doctrine query using ->getArrayResult() instead of ->getResult()
Use ReflectionClass to get the properties as an array :
http://php.net/manual/fr/class.reflectionclass.php
So you can iterate on each property, and do something like that :
{# Where you get your entity as an array #}
{% for property in entity %}
{% if not property is null %}
{{ property }}
{% endif %}
{% endfor %}
{# where fields comes from the ReflectionClass #}
{% for field in fields %}
{% if not attribute(entity,field) is null %}
{{ attribute(entity,field)}}
{% endif %}
{% endfor %}
Use the Twig strict_variables config parameter and set it to false (should be the default value):
strict_variables boolean
If set to false, Twig will silently ignore invalid variables
(variables and or attributes/methods that do not exist) and replace
them with a null value. When set to true, Twig throws an exception
instead (default to false).

Symfony2/Twig - how to set template file contents as variable?

In Symfony 2.8 I've got SharedBundle, where I keep views and other things I share between other bundles, including the whole layout that should be extended by other bundles' views.
That shared layout has some typical blocks, from which sidebar block should be different for different bundles.
Currently I did an ugly workaround:
In SharedBundle sidebar container:
{% if sidebarcontent is defined %}
{{ sidebarcontent|raw }}
{% else %}
SIDEBAR NOT FOUND
{% endif %}
And in other bundles (here: BundleA) in every view that extends the shared main view:
{% extends 'SharedBundle:layout.html.twig' %}
{% block sidebar %}
{% include 'BundleA:_partials:sidebar.html.twig' %}
{% endblock %}
{% set sidebarcontent = block('sidebar') %}
But that doesn't look good, I think. Is there a better way?
I think this is a valid approach. The only simplification I can think of is not using the variable sidebarcontent.
You can use block('sidebar') inside the if statement:
{% if block('sidebar')|length > 0 %}
{{ sidebarcontent|raw }}
{% else %}
SIDEBAR NOT FOUND
{% endif %}
Make sure the block exists before checking it's content, so initialise it with an empty string:
{% block sidebar %}{% endblock %}

Twig Type Reflection in Symfony 2

I'm working in a generic index.html.twig template, for testing purposes. So, I want to check the objects properties arriving to the view. I'm testing a lister for any entity in my database.
I'll loop an object's keys obtained. But the function: keys, from Twig official book, only works with with arrays or collections.
Exists any solution to this?
Here goes the code:
{% if model and model.data %}
{% set firstKeys= model.data|first %}
{% for key, item in firstKeys %}
{{ attribute(item, key) }}<br>
{% endfor %}
<p>
...
</p>
{% endif %}

Categories