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 = {} ```
Related
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
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 %}
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 %}
Im setting a "category" and passing it to a template that I'm including:
{% set categoryA = {
category: "categoryA",
}
%}
{% include "something.twig" with categoryA %}
{% set categoryB = {
category: "categoryB",
}
%}
{% include "something.twig" with categoryB %}
This is working fine but I'm repeating a lot of code which I want to avoid (in my actual code there are more than 2 categories).
Im trying to put the categories in an array and include something.twig for each one, passing a different category for each instance:
{% set categories = ['categoryA', 'categoryB', 'categoryC', 'categoryD', 'categoryE'] %}
{% for i in categories %}
<h3>{{ i }}</h3>
{% set categoryOption = {
category: {{ i }},
}
%}
{% include "something.twig" with categoryOption %}
{% endfor %}
The title in the h3 is printed OK however the categoryOption category is passed as [object Object] rather than the string name as I need
For example, you use category in the "something.twig":
...
{{ category|default }}
...
So you can use "include" like:
{% include "something.twig" with { category: 'Name of Category' } %}
Full code:
{% set categories = ['categoryA', 'categoryB', 'categoryC', 'categoryD', 'categoryE'] %}
{% for i in categories %}
<h3>{{ i }}</h3>
{% include "something.twig" with { category: i } %}
{% endfor %}
I have some objects like address1, address2, address3 ... address10. All these objects have lat and long values.
I know we can get it from the attribute() function of Twig, but what I want in my twig template is to get the main objects
{% for i in 1..10 %}
{% set address = address~i %}
{{ address.lat }}
// or like
{{ attribute(address, 'lat') }}
{% endfor %}
Just simply use :
{% for i in 1..10 %}
{{ attribute(attribute(_context, 'address'~i), 'lat') }}
{% endfor %}