TWIG - include variables in different template - twig

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

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 = {} ```

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

Trouble passing a string from an array to a variable within a for loop

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

Twig get object names ending with numbers dynamically

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

Navigation for pages (not for posts) in Jekyll

I'm building a website with Jekyll, just with pages.
I want to find a way to generate previous and next links for the pages, with an attribute for the pages like order.
Is there something that can do the job (without plugin)? I could only find something about posts.
{% assign sortedPages = site.pages | sort:'order' | where: 'published', true %}
{% for p in sortedPages %}
{% if p.url == page.url %}
{% if forloop.first == false %}
{% assign prevIndex = forloop.index0 | minus: 1 %}
<a href="{{site.baseurl}}{{sortedPages[prevIndex].url}}">
previous : {{sortedPages[prevIndex].title}}
</a>
{% endif %}
{% if forloop.last == false %}
{% assign nextIndex = forloop.index0 | plus: 1 %}
<a href="{{site.baseurl}}{{sortedPages[nextIndex].url}}">
next : {{sortedPages[nextIndex].title}}
</a>
{% endif %}
{% endif %}
{% endfor %}
This will do the job.
In order to filter which page you publish, you can add a published variable in pages front matter.
setting the variable
published: true -> this is a boolean
published: 'true' -> this is a string
using where filter
| where: 'published', true will test for boolean
| where: 'published', 'true' will test for string

Resources