How can I dynamically reference template file in volt template? - include

We know, {% include header.volt %} is reference file with name's header.volt.
How can I dynamically reference template file in volt template?
like this:
{% include variable %}
when variable equal footer.volt, theen the program will reference file with name's footer.volt
when variable equal index.volt, then the program will reference file with name's index.volt...
thanks!

I'm not sure if using variables in include will work, but you can always use partial() to include your template:
{% set partialName = 'footer' %}
{{ partial('partials/'~partialName) }}

Related

Ansible: Include a file if it exists and do nothing if it doesn't

I have a Jinja2 template in Ansible and I need to include some files conditionally.
Basically, I have a for loop that includes "subparts" that might or might not be present. The following sample demonstrates the basic code that would work if all tools actually had a 'pbr.j2' file, but it crashes because Ansible cannot find some of the files:
{% for tool in tools %}
{% set template = 'tools/' + tool.name + '/pbr.j2' %}
{% include template %}
{% endfor %}
I have also tried the exists and is_file filters, but I always get "false" even when the file exists. Here is a sample code of what I have tried:
{% for tool in tools %}
{% set template = 'tools/' + tool.name + '/pbr.j2' %}
{% if template|exists %}
{% include template %}
{% endif %}
{% endfor %}
The result of this last sample is that no file gets included. If I replace exists with is_file, I get the same behavior.
What do I need to change to accomplish what I want?
I have found a solution to my problem: using ignore missing in the include directive.
In this case, I would use:
{% for tool in tools %}
{% set template = 'tools/' + tool.name + '/pbr.j2' %}
{% include template ignore missing %}
{% endfor %}
Ansible will ignore missing files and only the existing files from that loop will get included.
You are trying to use Ansible filters (exists, is_file) in a Jinja2 template. This works for templates processed by Ansible templating engine, but not for templates processed by the template module.
Jinja2 does not have capability to check the existence of a file, so you need to move your logic to Ansible.
For example: create a find task to search for directories in tools/, and provide an intersection of find-results list with tools list to Jinja2.

Cannot push with Jekyll

I have created an empty array in Jekyll but I am not able to push data.
Here it is described how to do but it does not seem to work on my machine. I use Jekyll 2.5.3
{% assign selected = site.array %}
{% for success in site.data.success %}
{% assign selected = selected | push: success.id %}
{% endfor %}
site.array is defined into my _config.yml as
site.array: []
I am sure that success.id exists
UPDATE 1
I changed in _config.yml
array: []
Still no luck.
Why define a site.something in _config.yml ?
Any variable defined here will be accessed with the site prefix, but it's not up to you to add this prefix, Jekyll takes care of it. eg: {{ site.title }} refers to site: My awesome title. It's automatic.
Just define : emptyArray: [], and access it with site.emptyArray.
With site.array: [], you're defining a configuration variable that will never be site.site.array because a yaml key is supposed to be a string. Here, the only way to access your variable, is to write site["site.array"].
Note: If you can, upgrade to a newer version of Jekyll if you want to be future-proof.
I'm not sure if Jekyll accepts an empty array, "out of the blue".
So, try to use an if statement before the for loop:
{% if site.array %}
{% assign selected = site.array %}
{% for success in site.data.success %} {% assign selected = selected | push: success.id %}
{% endfor %}
{% endif %}
And keep just array: in your _config.yml. Any variable defined in this file will be called by site.variable.
I suggest you to try with some values in the array before leaving it empty, to make sure the code works.
Hope to have helped!

how to use variable to pass filter name in jinja2 templates

I have defined some filters and use it very often. I need to do some A/B tests and for this in some situations some of filters should work in different way.
Easiest way to do this would be create a variable in template which store a filter name. something like this:
{% set filter_name = 'some_name' %}
{{ my_value|filter_name }}
But when I try this, I get an error:
TemplateAssertionError: no filter named 'filter_name'
Please help me to find a solution.
By doing {% set filter_name = 'some_name' %}, you have create a string variable named "filter_name". You should create a filter which takes one more argument on basis of which it decides what to do.
{% set filter_name = 'some_name' %}
{{ my_value|myfilter(filter_name) }}
def myfilter(value, filtername):
if(filtername is 'twice')
return value*2
else
.....

django-registration: Cannot translate email and subject

Any .html template for django-registration module works fine with {% blocktrans %} and {% trans %} template blocks. With {% load i18n %} in place, of course.
But I cannot make use of i18n tags in activation_email.txt and activation_email_subject.txt templates. Strings marked for translation just don't appear in .po file after makemessages.
Also, when wrapping a text with {% blocktrans %}{% endblocktrans %}, all variables such as {{ site.domain }} and {{ site.name }} are not processed.
Can you suggest what I am doing wrong?
That was my bad, I just improperly used makemessages. By default it processes only .html files.
In my case
django-admin.py makemessages -a -e html,txt
does all the work.
As for variables, {% blocktrans %}{% endblocktrans %} cannot process variables inside object, so we have to retrieve them before translation:
{% blocktrans with site.name as site_name and site.domain as site_domain %}
Good examples of templates for django-registration are given here.

Liquid templates - accessing members by name

I'm using Jekyll to create a new blog. It uses Liquid underneath.
Jekyll defines certain "variables": site, content, page, post and paginator. These "variables" have several "members". For instance, post.date will return the date of a post, while post.url will return its url.
My question is: can I access a variable's member using another variable as the member name?
See the following example:
{% if my_condition %}
{% assign name = 'date' %}
{% else %}
{% assign name = 'url' %}
{% endif %}
I have a variable called name which is either 'date' or 'url'.
How can I make the liquid equivalent of post[name] in ruby?
The only way I've found is using a for loop to iterate over all the pairs (key-value) of post. Beware! It is quite horrible:
{% for property in post %}
{% if property[0] == name %}
{{ property[1] }}
{% endif %}
{% endfor %}
Argh! I hope there is a better way.
Thanks.
I don't know what I was thinking.
post[name] is a perfectly valid liquid construction. So the for-if code above can be replaced by this:
{{ post[name] }}
I thought that I tried this, but apparently I didn't. D'oh!
Liquid admits even fancier constructs; the following one is syntactically correct, and will return the expected value if post, element, categories, etc are correctly defined:
{{ post[element.id].categories[1].name }}
I am greatly surprised with Liquid. Will definitively continue investigating.
Actually, this did not work for me. I tried a bunch of different combinations and what finally worked was
<!-- object myObj.test has the string value "this is a test" -->
{% assign x = 'test' %}
{{ myObj.[x] }}
Output:
this is a test

Resources