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 %}
Related
Using Symfony 4.1 with EasyAdmin bundle.
I am trying to override a template for the User (one of my entites) show view. I have set up the override and it is working.
Created a user_show.html.twig and registered it in the easy_admin config file.
I then copy the twig blocks from corresponding bundle template for show.html.twig.
Then tried to render a User property from my user entity, called profile height.
Here are twig blocks I'm overriding:
{# templates/admin/user_show.html.twig #}
{% extends '#EasyAdmin/default/show.html.twig' %}
{% block content_title %}
Test Title {{ dump() }}
{% endblock %}
{% block main %}
{{ profileHeight }}
{% endblock %}
Error
Twig_Error_Runtime: Variable "profileHeight" does not exist
Debug
I did a dump and found the property is present on page:
Why can twig not see these variables appearing in the dump? How can I render the properties I want in the template?
As you can see in your dumped data, there is entity array key which hold the User object. So instead of:
{% block main %}
{{ profileHeight }}
{% endblock %}
Use:
{% block main %}
{{ entity.profileHeight }}
{% endblock %}
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.
(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).
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.
I have a twig template in my Symfony2 project.
In the twig template, I have an entity object.
This entity object is linked to another entity with oneToMany relation.
Example:
{{ submission }} -> Submission entity
{{ submission.histories }} -> Histories entity -> I have here an array collection of histories
The entity histories has a field "state_to"
My goal is to get only the histories object where the state_to is 4
I tryed like that:
{{ submission.histories('status_to', 4)[0] }}
But this is not working.
I know that I can use:
{% for history in submission.histories %}
{% if history.statusTo == 4 %}
{{ history.statusDate|date("d F Y") }}
{% endif %}
{% endfor %}
But I am allmost sure that there is a nicer way.
Add a method getHistoryByStatus($status) in your entity to filter your histories based on the status_to field, then in your template:
{% set filtered_history = submission.historyByStatus(4)|default(false) %}
{% if filtered_history %}
{{ filtered_history.statusDate|date("d F Y") }}
{% endif %}
you could just find the histories object where the state_to is 4 within a method called in your controller. then pass it to the view. This method can be inside your controller, but is better to have it in your history repository maybe? or a manager..
try avoiding complexity in the views.