Getting value of array object in Twig file Symfony - php

I have following set of an array that is pass to twig file.
others ={
0:{id : 10, name: krist},
1:{id : 20, name: ryan}
}
When I retrieve back from twig file, I retrieve as follow.
{% for other in others %}
{{other.id}}
{% endfor %}
The above method always shows the error as follow:
An exception has been thrown during the rendering of a template
("Notice: Array to string conversion").
How can I fix this issue?

You can try this.
{% for other in others %}
{% for o in other %}
{{o.id}}
{% endfor %}
{% endfor %}
or
{% for other in others %}
{{other[0].id}}
{% endfor %}

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

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

Print Twig variable x times based upon randomized range

I am using Twig and Timber for a WordPress project. I have the following loop in my template that prints my custom post type titles into a HTML structure.
{% for company in companies %}
{% set dot = "<div class='company-dot'></div>" %}
{% set range = range(10, 20) %}
{{dot}}
{{random(range)}}
<div class="company">
<div class="company-dot dot-active"></div>
<p class="dot-caption">{{ company.title }}</p>
</div>
{% endfor %}
I would like to print my {{dot}} variable x amount of times based upon the number that is generated by {{random(range)}}. How can I do this?
The simplest solution would be to iterate random(range) times with a for loop:
{% for i in 0..random(range(10, 20)) %}
{{ dot }}
{% endfor %}
I don't really know Twig but my guess is that you could to the following:
{% for i in random(range) %}
{{dot}}
{% endfor %}
You already know how to use range, just use it again :
{% set dots_count = random(range) %}
{% for dot_index in range(1,dots_count) %}
{{dot}}
{% endfor %}
{{dots_count}}
Here is a fiddle : https://twigfiddle.com/ko595z

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

Is it possible to concatenate a variable to an if statement in Twig?

I am trying to concatenate a variable to an array key to get access to certain values in Twig, but no success so far.
I have a large PHP array that has for example keys like this:
$array = [
...
...
...
'test_1' => $test_1,
'test_2' => $test_2
];
I tried the following in my Twig template:
{% for i in 1..2 %}
{% if array.test_{{ i }} != 0 %}
<div>Test</div>
{% endif %}
{% endfor %}
but that doesn't work.
Is there a way to access values like this in Twig?
Try this:
{% for i in 1..2 %}
{% if array['test_' ~ i] != 0 %}
<div>Test</div>
{% endif %}
{% endfor %}

Categories