How to check, also, if var is defined at Twig template - php

I have this code at Twig template:
{% if form_action is empty or form_action is null or form_action is not defined %}
{% set form_action = '' %}
{% endif %}
But when I load the page I got this error message:
Variable "form_action" does not exist in AppBundle::pdone.html.twig at line 1
How do I check also if variable is set? Is not the condition enough? Any advice?

The order of your statement is not correct. Usually this should be enough:
{% if form_action is not defined %}
to see if a variable has a value:
{% if form_action %}

Use is defined
{% if form_action is defined %}
{# Do your stuff here #}
{% endif %}

Related

How to render twig condition dynamically from php array

How to render twig condition dynamically from php array
Php data array
arry('label'=>'test','parentRoleExp'=>"is_granted('ROLE_ADMIN') OR is_granted('ROLE_BLOG')"
twig code
{% set parentRoleExp = '' %}
{% if link['parentRoleExp'] is defined %}
{% set parentRoleExp = link['parentRoleExp'] %}
{% endif %}
{% if parentRoleExp %}
<h1>Admin Blog</h1>
{% else %}
<h1>Blog <?h1>
{% endif %}
Above code not working
Expected result
{% if is_granted('ROLE_ADMIN') OR is_granted('ROLE_BLOG') %}
<h1>Admin Blog</h1>
{% else %}
<h1>Blog <?h1>
{% endif %}
I tried with various method but not working. please help
Thanks in advance
it's very bad idea - ulnerability
But if you want... register eval() function:
$twig = new Twig\Environment($loader);
$twig->addFunction(new Twig\TwigFunction('phpEval', 'eval'));
in template
{% if phpEval(parentRoleExp) %}

How can I check if a value exists with twig?

I checked if my value exists before printing:
{% if address.company|length %}{{ address.company}}{% endif %}
But still I get an error message:
Key "company" does not exist as the array is empty.
Using the default filter you can easily verify if a company exists and is not empty simultaneously:
{% if address.company|default %}
The company is not empty
{% else %}
The company is empty.
{% endif %}
Try using the empty twig function:
{% if address.company is not empty %}
{{ address.company }}
{% endif %}

Find value in array and return with new from different array

Currently I have two arrays
{% set code = [AMS, EIN, RTM] %}
{% set city = [Amsterdam, Eindhoven, Rotterdam] %}
I would like to check if the value of {{airport}} exists in the first array and if it is code[0] I would like to change {{airport}} into the value of city[0]. Is this possible with Twig?
You can loop over the code array:
{% for c in code %}
{# ... #}
{% endfor %}
Documentation: https://twig.symfony.com/doc/2.x/tags/for.html
Then if the item does match:
{# ... #}
{% if airport == c %}
{# ... #}
{% endif %}
{# ... #}
Documentation: https://twig.symfony.com/doc/2.x/tags/if.html
Replace the variable airport, at the same loop index:
{# ... #}
{% set airport = city[loop.index0] %}
{# ... #}
Documentation: https://twig.symfony.com/doc/2.x/tags/for.html#the-loop-variable
So, in full:
{% for c in code %}
{% if airport == c %}
{% set airport = city[loop. index0] %}
{% endif %}
{% endfor %}
Running fiddle: https://twigfiddle.com/xflfas/2
Out of the scope note: your arrays would be better named cities and codes.
This way, when you loop over them, you end up with meaningful naming
{% set codes = ['AMS', 'EIN', 'RTM'] %}
{% for code in codes %}
{{ code }}
{% endfor %}
{# and #}
{% set cities = ['Amsterdam', 'Eindhoven', 'Rotterdam'] %}
{% for city in cities %}
{{ city }}
{% endfor %}
Use {% if value in array %} to search from the first array and Twig's merge function to replace the value in the second array. See this https://twig.symfony.com/doc/2.x/filters/merge.html

Storing HTML in a Twig Array

Ok I'm trying to add HTML code to a Twig array and ending up into problems. I'm not the most knowledgable when it comes to Twig so I'll need help. I'm getting this error when I try: Fatal error: Uncaught Twig_Error_Syntax: Arguments must be separated by a comma.
What am I doing wrong? https://pastebin.com/gEGLnCid
{% set myArray= [] %}
{% for product in products %}
You have to do it in two steps e.g.
{% set arr = [] %}
{% for i in 1..10 %}
{% set foo %}
{{ i * 10 }}
foo
bar
foobar
{{ i }}
{% endset %}
{% set arr = arr | merge([ foo, ]) %}
{% endfor %}
{% for val in arr %}
{{ val }}
{% endfor %}

Twig accessing variables that need a variable expansion

I have the following code in a Twig template:
{% macro option_display(type) -%}
{% if server.options_array.options.{{type}}.name is defined %}
{{server.options_array.options.{{type}}.name}} +${{server.options_array.options.~type~.price}}
{%else%}
None
{% endif %}
{%- endmacro %}
I'm getting this error:
Twig_Error_Syntax: Expected name or number
How should I get it to expand the variable 'type' in that context? I've tried using ~type~ as well (concat).
would be something like $server['options_array']['options'][$type]['name']; in PHP.
Just use the bracket notation:
{% macro option_display(type) -%}
{% if server.options_array.options[type].name is defined %}
{{ server.options_array.options[type].name }} +${{ server.options_array.options[type].price }}
{% else %}
None
{% endif %}
{%- endmacro %}
You probably want to also check whether server.options_array.options[type] is defined before checking whether server.options_array.options[type].name is defined.
Furthermore, like the documentation of the macro tag says, "as with PHP functions, macros don't have access to the current template variables." So you probably need to pass the server variable to the macro as well.
So here's a more complete example:
{% macro option_display(server, type) -%}
{% set option = server.options_array.options[type]|default(null) %}
{% if option.name is defined %}
{{ option.name }} +${{ option.price }}
{% else %}
None
{% endif %}
{%- endmacro %}
{% from _self import option_display %}
{{ option_display(server, 'foo') }}
{{ option_display(server, 'bar') }}
Notice that I'm using a helper variable option and the default filter. Without the filter, you'd get a Twig_Error_Runtime exception if the server.options_array.options array doesn't have the key you are using. (Whether you get this exception or not depends on the value of the environment option strict_variables.)
See TwigFiddle

Categories