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.
Related
I have a helm chart in yaml like this:
env:
- name: EmailHeader
value: {{ .Values.npsweb.email.support.header | quote }}
and in my Values file, I have like this:
npsweb:
email:
support:
header: <html><body><img src='https://nps.dev.nuancepowershare.com/smr/images/weblogo.png' alt='SMR LOGO' /><hr />
But it says that Anchor lt;html><body><img is never used. I tried putting double quote around it, but it still didn't get picked up.
What is the problem here and how can I fix this?
You need to explicitly quote the string. There are a couple of ways to do this, so pick whichever makes sense:
header-with-single-quotes: '<.../>'
header-with-double-quotes: "<.../>"
header-as-block-scalar: |-
<html>...</html>
header-as-block-scalar-with-whitespace: |-
<html>
<body>
...
</body>
</html>
All of these forms are the same, except that the last one will include embedded newlines and spaces. (Since your value includes single quotes, the single-quoted form will be a little less convenient.) You could also consider storing the HTML content in a file and reading it in with .Files.Get (though Helm doesn't include a template function for escaping HTML).
What's wrong with your existing values.yaml file? YAML has the notion of an anchor; you can give a part of a document a label like this, and reference it from elsewhere in the document. The '&' character at the beginning of the value marks an anchor, and the text up to the first space is the name of the anchor.
Not so much in Kubernetes, but in Docker Compose you'll somewhat routinely see this for reusable blocks of environment variables:
# (This is a Docker Compose file, not Kubernetes, as a YAML example)
version: '3.8'
x-environment: &environment # <-- anchor
DB_HOST: db
services:
db: { image: postgres }
application:
build: .
environment: *environment # <-- reference to anchor
worker:
build: .
command: ./worker
environment: *environment # <-- same reference to anchor
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 :)
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' %}
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')
In foo.markdown I have the following:
---
layout: default
title: Snarky little Ewok
---
A little Ewok is sometimes referred too as <h3>. But pappa Ewok is called <h1> - if you know what's good for you.
Well, I want Jekyll to automatically html escape the greater than and less than characters. I'm seriously fatigued after today's apprentice training and I'm just too lazy to manually html escape myself: >h3<
Is there a config option or something to automatically escape Jekyll markdown content?
If you used textile instead of markdown, there would be a way.
Liquid markup has textilize & escape filters; those two would allow you to do what you wanted, but on textile. You would have to save your files as text (file extension: txt), and then escape the html before textilizing:
---
layout: default
title: Snarky little Ewok
---
This file's extension is .txt
A little Ewok is sometimes referred too as <h3>. But pappa Ewok is called <h1> - if you know what's good for you.
Then on the default.html layout, instead of having:
{{ page.content }}
You would have this:
{{ page.content | xml_escape | textilize }}
Since there's no 'markdownify' filter on Jekyll yet, you can't do that with markdown. There's an issue (Issue 134) on Jekyll for adding a markdownify filter.
EDIT:
It's now possible to use markdown (since jekyll 0.10.1)
{{ page.content | xml_escape | markdownify }}