Use Twig variable in include path - include

I want to use a Twig variable in a include line, but it isn't working:
{% include 'folder/{{ component }}.twig' %}
I get the error:
Unable to find template "folder/{{ component }}.twig"
Can anyone help me out?
Thanks!

I just found the answer here:
https://groups.google.com/forum/#!topic/twig-users/zfnxC16gHx0
The correct syntax is:
{% include 'folder/' ~ component ~ '.twig' %}

Related

Transform ansible yml files "with vars templating" to yml files "without templating"

I have this yml files inside an ansible project with templating and vars :
custom_values:
postgresql:
postgresqlDatabase: "{{ secrets.db_name }}"
postgresqlPassword: "{{ secrets.postgres_password }}"
I search a solution to generate the same yml file without the templating like :
custom_values:
postgresql:
postgresqlDatabase: "mydatabase"
postgresqlPassword: "mypassword"
Do you know an existing software to do that automatically ?
You already have a set of ansible templates, that are rendered by a render engine like Jinja2.
The easiest way to convert them would be to actually use the render engine to render the templates, supplying the correct values to it.
You'll end up with a bunch of templates that have the {{ something }} blocks, replaced with the values you want.
Since this looks to be simple Jinja2 templating, please refer to: https://jinja.palletsprojects.com/en/2.10.x/
You'll end up with something like this:
>>> from jinja2 import Template
>>> template = Template('Hello {{ name }}!')
>>> template.render(name='John Doe')
Please also refer to this stackoverflow post: How to load jinja template directly from filesystem
That explains how to load templates from files
This python code is OK for me :
#import necessary functions from Jinja2 module
from jinja2 import Environment, FileSystemLoader
#Import YAML module
import yaml
#Load data from YAML into Python dictionary
config_data = yaml.load(open('./my_vars.txt'))
#Load Jinja2 template
env = Environment(loader = FileSystemLoader('./templates'), trim_blocks=True, lstrip_blocks=True)
template = env.get_template('my_template.yml')
#Render the template with data and print the output
print(template.render(config_data))
thanks :)

Put one dinamic object with assets in laravel

Hi i dont know how put img with assets.
I have this
{{asset('Articulos/{{$Articulo->path}}')}}
but i have error
syntax error, unexpected '}'
can you help me?
The correct syntax is:
{{ asset('Articulos/' . $Articulo->path) }}

How to highlight Markdown code in Jekyll?

I want to highlight markdown code in jekyll but I can't find a way how to do it. I looked at Pygement's lexers but I did not found one for markdown or kramdown.
I can highlight other codes but not markdown and kramdawn.
You can use Rouge or Coderay for this purpose.
You could use something like this:
```markdown
{% raw %}{% if page.tags %}...{% endif %}{% endraw %}
```

Using app engine interactive console with jinja2 template

from jinja2 import Template
template = Template('Hello {{ name }}!')
template.render(name='John Doe')
I have entered the above into app engine's interactive console and get no output. How can I get output?
I have tried adding the code I found at the following link to the console, but still no output.
Debug Jinja2 in Google App Engine
Thanks,
Brian in Atlanta
You forgot to add a print. Try this:
from jinja2 import Template
template = Template('Hello {{ name }}!')
print template.render(name='John Doe')

Jekyll Liquid - accessing _config.yml dynamically

For internationalizing my app I need to be able to dynamically access entries in a YAML file.
It is best explained with an example:
Page:
---
layout: default
title: title_homepage
---
This will then allow access to the title_homepage variable in the Default Layout Template:
Default Layout:
page.title = "title_homepage"
Now normally I would access my _config.yml file like this:
{{ site.locales[site.default_locale].variable }}
However, now for this to work, I need to access the _config.yml with the value of page.title. This will not work:
{{ site.locales[site.default_locale].page.title }}
I need the following (pseudo code):
{{ site.locales[site.default_locale].#{value of page.title}}
With the way your vars are set, it would be something alog the lines of
{{ site.locales[site.default_locale][page.title] }}
The thing is, ... I don't really see the point of doing this. Let's say your page is the english page. The locale should then be defined within the page, and so should your title!
---
locale: en
title: My Wonderful Page
---
Which you can retrieve with {{ page.title }} ...
What could be the point of putting the title into the _config.yml file?
(edit) well unless you want to access page.title when in another page/post, in this case you have no choice but to put it into _config.yml.

Resources