Twig variable error - twig

I have array in PHP:
array(
'name1' => 'one',
'name2' => 'two',
'name3' => 'three'
)
but in twig:
{% for i in range(1, 3) %}
{{'name' ~ i}}
{% endfor %}
give me:
1
2
3
Please help me. Thanks.

You can do this:
{% for i in range(1, 3) %}
{{ attribute(_context, 'name' ~ i) }}
{% endfor %}
To have:
one
two
three

Related

jinja2 template generate data from dictionary with loop and condition

I have one csv file which has few colums marked with .M as mandatory which will be passed to jinja template as it with no condition check . But for some columns i have to check for column heading and then make a sub section in template to accept all those parameters in that section .
Input :
|configuration|Server Node|ENUM-DNS service Instance Name.M|Zone Name.M|type.M|domain.M|description|ttl|soa-mname|soa-rname|soa-refresh|soa-retry|soa-expire|soa-minimum|soa-name-server|transfer-master-server|transfer-order|Remarks
enum-dns-zone,nn-edge01,,mnc001mcc655,dns,ims.mnc001.mcc655.3gppnetwork.org,,86400,kashif,hehhe,jjjj,jkjkj,ssss,kjkjkj,kjkjkjk,jkjkjk,hhhh,k
enum-dns-zone,nn-edge02,,mnc001mcc655,dns,ims.mnc001.mcc655.3gppnetwork.org,,86400,iqbal,jhjhjhs,hshss,hjjahj,h,sss,aa,ddeee,sss,
Output :
enum-dns-zone:: {
Zone name=mnc001mcc655
type=dns
domain=ims.mnc001.mcc655.3gppnetwork.org
soa {
mname=kashif
rname=hehhe
refresh=jjjj
retry=jkjkj
expire=ssss
minimum=kjkjkj
name=kjkjkjk
}
.... repeated to number of occurances of **soa** keyword
transfer {
master=jkjkjk
order=hhhh
}
... repeated to n number of occurances to keyword **transfer**
Expected Output:
enum-dns-zone:: {
Zone name=mnc001mcc655
type=dns
domain=ims.mnc001.mcc655.3gppnetwork.org
soa {
mname=kashif
rname=hehhe
refresh=jjjj
retry=jkjkj
expire=ssss
minimum=kjkjkj
name=kjkjkjk
}
transfer {
master=jkjkjk
order=hhhh
}
J2Template:
data_enum= (""" {{row.configuration}}::{{row.type}} {{row.type}} {
{% for key1,value1 in mydata.items() %}
{{key1}}={{value1}}
{% endfor %}
{% for key,value in row.items() %}
{% if "soa" in key %}
soa {
{% for key, value in row.items() %}
{% if "soa" in key %}
{% set split_list=key.split('-') %} {{ split_list[1] }}={{value}}
{% endif %}
{% endfor %}
}
{% elif "root" in key %}
root {
{% for key, value in row.items() %}
{% if "root" in key %}
{% set split_list=key.split('-') %} {{ split_list[1] }}={{value}}
{% endif %}
{% endfor %}
}
{% elif "Probability" in key %}
probability {
{% for key, value in row.items() %}
{% if "Probability" in key %}
{% set split_list=key.split('-') %} {{ split_list[1] }}={{value}}
{% endif %}
{% endfor %}
}
{% elif "recursion" in key %}
recursion {
{% for key, value in row.items() %}
{% if "recursion" in key %}
{% set split_list=key.split('-') %} {{ split_list[1] }}={{value}}
{% endif %}
{% endfor %}
}
{% elif "transfer" in key %}
transfer {
{% for key, value in row.items() %}
{% if "transfer" in key %}
{% set split_list=key.split('-') %} {{ split_list[1] }}={{value}}
{% endif %}
{% endfor %}
}
{% endif %}
{% endfor %}
}
"""
Python Code:
with open("enum-dns-zone.csv", 'r') as read_obj:
# if files_temp.split('.')[0]=="enum-monitoring-endpoint":
datatemp=data_enum
# pass the file object to DictReader() to get the DictReader object
csv_dict_reader = csv.DictReader(read_obj)
# iterate over each line as a ordered dictionary
for row in csv_dict_reader:
# row variable is a dictionary that represents a row in csv
for key in row.keys():
#print(key)
if key[-1]=="M":
if row[key]!="":
mandatory_parameters[key[:-2]]= row[key]
#print(mandatory_parameters)
temp = Template(data_enum)
with open("kashif", 'a') as f:
f.write(temp.render(row=row, mydata=mandatory_parameters))
f.close()
print(temp.render(row=row, mydata=mandatory_parameters))
# print(mandatory_parameters)
mandatory_parameters = {} ```

How to compare dates in twig

I have the following twig template
{% for id,value in item.attributes %}
{% set attribute_details = getAttributeSet(id) %}
ID: {{id}}<br>
VALUE: {{value.sec|date("d/m/Y")}}<br>
TYPE: {{data}}<br>
LABEL: {{attribute_details.label}}<br>
{% endfor %}
resulting in the following text:
ID: 607ecae15fb8e0c3c2d7ca02
VALUE: 19/05/2021
TYPE:
LABEL: scadenza_offerta
How can I check wether VALUE is grater than 19/05/2021? And How I can hide the text otherwise?
I have try this code but it does not work:
{% if VALUE == '19/05/2021' %}
work
{% endif %}
As seen in the linked answer
{% if date(value.sec) > date('19/05/2021') %}
work
{% endif %}
demo

TWIG - include variables in different template

I would like to include the same variables in different templates
vars_catchphrase.twig
{% set catchphrase_size = '' %}
{% if var.tile_catchphrase|length <= 4 %}
{% set catchphrase_size = 'size-lg' %}
{% elseif var.tile_catchphrase|length >= 5 and var.tile_catchphrase|length <= 8 %}
{% set catchphrase_size = 'size-md' %}
{% elseif var.tile_catchphrase|length >= 9 and var.tile_catchphrase|length <= 12 %}
{% set catchphrase_size = 'size-sm' %}
{% elseif var.tile_catchphrase|length >= 13 %}
{% set catchphrase_size = 'size-xs' %}
{% endif %}
I tried to include with this (because the context is sometime different) :
{% include 'vars_catchphrase.twig' with { 'var' : post } %}
When the context is different from post I use another one :
{% include 'vars_catchphrase.twig' with { 'var' : item } %}
example.twig
{% for item in list %}
{% include 'vars_catchphrase.twig' with { 'var' : item } %}
<p class="catchphrase {{ catchphrase_size }}">{{ item.title }}</p>
{% endfor %}
The variable is empty. Can I have some help please ?
Templates you include have their own variable scope, this means variables defined inside this template will not be known out the template. This said, included templates also can't alter the parent's context (by default), this is due to twig passing the context array by value, not by reference.
foo.twig
{% set foo = 'foo' %}
{% include 'bar.twig' %}
{{ foo }}
bar.twig
{% set foo = 'bar' %}
The example above will still output foo
In order to solve your problem, I'd suggest adding a custom filter to twig
<?php
$twig->addFilter(new \Twig\TwigFilter('catchphrase_size', function($value) {
switch(true) {
case strlen($value->tile_catchphrase) >= 13: return 'size-xs';
case strlen($value->tile_catchphrase) >= 9: return 'size-sm';
case strlen($value->tile_catchphrase) >= 5: return 'size-md';
default: return 'size-lg';
}
});
This way you can use the filter where ever,
{% for item in list %}
<p class="catchphrase {{ item|catchphrase_size }}">{{ item.title }}</p>
{% endfor %}

twig variable won't pass - symfony sonata

5 days have gone by, still no success!
I dumped the results of my card variable and it returns results.
But when I try to pass it trough varibale on my twig tamplate it throws:
Variable "card" does not exist.
It's defined in SonataAdmin.
protected function configureShowFields(ShowMapper $showMapper)
{
$card = $this->getCardTransactions(); // on dump(), it works
$showMapper->tab('Cards')
->add('Data', 'string', array(
'template' => "#AdminTemplates/sonata/details.html.twig",
'card' => $card
))
->end()
->end();
}
and in my twig;
{% for c in card %}
{{ c.id }}
{% endfor %}
I think it has to do with SonataAdmin and how it handles this type of calls but I have read the documentation and searched online but still no luck.
You have to use the field_description.options object in your template to access your variable.
protected function configureShowFields(ShowMapper $showMapper)
{
$showMapper
->tab('Cards')
->add('Data', 'string', [
'template' => "#AdminTemplates/sonata/details.html.twig",
'card' => $this->getCardTransactions(),
])
->end();
}
{# #AdminTemplates/sonata/details.html.twig #}
{% extends '#SonataAdmin/CRUD/base_show_field.html.twig' %}
{%- block field -%}
{% spaceless %}
{% for card in field_description.options.cards %}
{{ card.id }}
{% else %}
<p>No card</p>
{% endfor %}
{% endspaceless %}
{%- endblock -%}

Symfony & Twig: how to get vars in twig by DB data?

One question please.
{{ dump(app.user.slugName) }}
If I do the above snippet in Twig, I get the slugName of the user loged ("my-user-2", i.e.) in the app (SlugName is an atribute of the entity user). Ok & Correct. But... I want to order this action from a var (var from BD data)
I have a variable named option which is set like this:
{% set option = 'app.user.slugName' %}
But when I'm trying output this variable with {{ dump(option)}} it returns app.user.slugName as literal. It does not return my-user-2.
Is there are any way in twig to solve this? It's a function to generate a menu, but some links needs some parameters.
I see what you mean, but Twig can't evaluate expression like that.
To achieve something like that you would need a snippet like this,
{% set value_methods = 'app.user.slugname' %}
{% set option_value = _context %}
{% for method in (value_methods|split('.')) if method != '' %}
{% set option_value = attribute(option_value, (method|replace({'()': '', }))) %}
{% endfor %}
{{ option_value }}
twigfiddle
(edit)
Remember you can create a macro to achieve some reusability for this snippet,
{% import _self as macros %}
{{ macros.evaluate(_context, 'app.user.slugname') }}
{% macro evaluate(context, value_methods) %}
{% set option_value = context %}
{% for method in (value_methods|split('.')) if method != '' %}
{% set option_value = attribute(option_value, (method|replace({'()': '', }))) %}
{% endfor %}
{{ option_value }}
{% endmacro %}

Resources