saltstack jinja2 nested template inheritance - jinja2

I'm having some issues with nested template inheritance in saltstack.
I'm expecting create_mon to pass the string 'ceph_mon_config' to create, which then pulls the values of cpu, mem, etc. in the 'ceph_mon_config pillar to prepare, which does the heavy lifting. As it stands now when the renderer reaches:
{% block cpu %}{{ pillar['{% block pillar_id %}{% endblock pillar_id %}']['cpu'] }}{% endblock cpu %}
It looks like it is interpreting it literally:
Rendering SLS 'base:/apps/ceph/create-mon' failed: Jinja variable 'salt.pillar object' has no attribute '{% block pillar_id %}{% endblock pillar_id %}'
Any ideas?
Thanks!
create_mon.sls
{% extends "states/system/vm/create.sls" %}
{% block pillar_id %}ceph_mon_config{% endblock pillar_id %}
virsh create /kvmfs/vms/{{ pillar['ceph_mon_config']['hostname'] }}/config.xml:
cmd.run:
- requires:
- sls: /states/system/vm/create
create.sls
{% extends "states/system/vm/prepare.sls" %}
{% block cpu %}{{ pillar['{% block pillar_id %}{% endblock pillar_id %}']['cpu'] }}{% endblock cpu %}
{% block mem %}{{ pillar['ceph_mon_config']['mem'] }}{% endblock mem %}
{% block network %}{{ pillar['ceph_mon_config']['network'] }}{% endblock network %}
{% block os %}{{ pillar['ceph_mon_config']['os'] }}{% endblock os %}
{% block hostname %}{{ pillar['ceph_mon_config']['hostname'] }}{% endblock hostname %}
{% block disk %}{{ pillar['ceph_mon_config']['disk'] }}{% endblock disk %}
prepare.sls
/kvmfs/vms/{% block hostname %}{% endblock hostname %}/config.xml:
file.managed:
- source: https://git.cybbh.space/vta/saltstack/raw/master/apps/virsh/files/common.xml
- makedirs: True
- template: jinja
- skip_verify: True
- defaults:
name: {{ self.hostname() }}
mem: {% block mem %}{% endblock mem %}
cpu: {% block cpu %}{% endblock cpu %}
network: {% block network %}{% endblock network %}
/kvmfs/vms/{{ self.hostname() }}/disk0.qcow2:
file.copy:
- source: /kvmfs/images/{% block os %}{% endblock os %}-latest
qemu-img resize /kvmfs/vms/{{ self.hostname() }}/disk0.qcow2 {% block disk %}{% endblock disk %}:
cmd.run:
- requires:
- /kvmfs/vms/{{ self.hostname() }}/disk0.qcow2
/kvmfs/vms/{{ self.hostname () }}/data/meta-data:
file.managed:
- source: https://git.cybbh.space/vta/saltstack/raw/master/apps/virsh/files/common.metadata
- makedirs: True
- template: jinja
- skip_verify: True
- defaults:
hostname: {{ self.hostname() }}
/kvmfs/vms/{{ self.hostname () }}/data/user-data:
file.managed:
- source: https://git.cybbh.space/vta/saltstack/raw/master/apps/virsh/files/common.userdata
- makedirs: True
- skip_verify: True
genisoimage -o /kvmfs/vms/{{ self.hostname () }}/config.iso -V cidata -r -J /kvmfs/vms/{{ self.hostname () }}/data/meta-data /kvmfs/vms/{{ self.hostname () }}/data/user-data:
cmd.run:
- requires:
- /kvmfs/vms/{{ self.hostname () }}/data/metadata
- /kvmfs/vms/{{ self.hostname () }}/data/userdata

Jinja doesn't allow nested evaluations, but variables are also passed from template to template when using 'extends', not just blocks.
create_mon
{% set pillar_id = 'ceph_mon_config' %}
create
...
{% extends "states/system/vm/prepare.sls" %}
{% block cpu %}{{ pillar[pillar_id]['cpu'] }}{% endblock cpu %}
...
This works as expected.

Related

What is the "base" file that the Bokeh index.html extends?

I am a little confused about what {% extends base %} is extending at the start of index.html in Bokeh server application packages.
Examples of this can be seen in:
Bokeh Docs: Embedding in Templates
{% extends base %}
{% block contents %}
<div>
<p> Hello {{ user_id }}, AKA '{{ last_name }}, {{ first_name }}'! </p>
</div>
{% endblock %}
Bokeh Server Application Examples
Example code from the Gapminder package in templates/index.html
{% extends base %}
{% block title %}Bokeh Gapminder Example{% endblock %}
{% block postamble %}
<style>
{% include 'styles.css' %}
</style>
{% endblock %}
What is this "base" that is being extended?
I see that there is a "contents" block, "title" block, and "postamble" block from the above examples.
How do I know what other jinja blocks I can modify?
Thanks.

Bokeh Server loading a Static CSS Stylesheet

I've created a project and I'm attempting to load a static CSS for the styling of the webpage. However No matter what I do it does not appear to load. It gives me an internal 500 error. The docs are rather limiting on explaining how it works and how to setup it up. They just show a screeshot of the folder structure.
I tried using this but no luck, and I'm not entirely sure if i even need it, the docs don't really say.
{% include './static/css/custom.css'%}
Here is my setup.
myapp
+---main.py
+---static
| +---css
| | +---special.css
|
|---templates
| +---index.html
|
+---theme.yaml
However here is my index.html file
<!DOCTYPE html>
<html lang="en">
{% block head %}
<head>
{% block inner_head %}
<meta charset="utf-8">
<title>{% block title %}{{ title | e if title else "Bokeh Plot" }}{% endblock %}</title>
{% block preamble %}{% endblock %}
{% block resources %}
{% block js_resources %}
{{ bokeh_css | indent(8) if bokeh_css }}
{% endblock %}
{% block css_resources %}
{{ bokeh_js | indent(8) if bokeh_js }}
{% endblock %}
{% endblock %}
{% block postamble %}{% endblock %}
{% endblock %}
</head>
{% endblock %}
{% block body %}
<body>
<h1>MY MAP</h1>
{% block inner_body %}
{% block contents %}
{% for doc in docs %}
{{ embed(doc) if doc.elementid }}
{% for root in doc.roots %}
{{ embed(root) | indent(10) }}
{% endfor %}
{% endfor %}
{% endblock %}
{{ plot_div | indent(8) }}
{{ plot_script | indent(8) }}
{% endblock %}
</body>
{% endblock %}
</html>
You need to specify the full path from the application directory. So if the application folder is called myapp, the include statement should be:
{% include 'myapp/static/css/custom.css'%}

Shopify Liquid capture settings not outputting for some reason

I'm trying to get and display some theme settings which gets the site name and applies to end which i have setup in the schema but cant get it to display the settings.
In the promo_header.liquid snippet file i have:
{% capture promo_header_text_1 %} locale_promo_1_text_{{ shop.name }} {% endcapture %}
{% capture promo_header_url_1 %} locale_promo_1_url_{{ shop.name }} {% endcapture %}
{% capture promo_header_text_2 %} locale_promo_2_text_{{ shop.name }} {% endcapture %}
{% capture promo_header_url_2 %} locale_promo_2_url_{{ shop.name }} {% endcapture %}
<p>
{% if settings[promo_header_text_1] %}
{{ settings[promo_header_text_1] }}
{% endif %}
{% if settings[promo_header_text_2] %}
<span>/</span>{{ settings[promo_header_text_2] }}
{% endif %}
</p>
I have also tried to output using just for example
{{ settings.promo_header_text_1 }}
But not displaying anything either... when i debug and display for example
{{ promo_header_text_1 }}
It does return
locale_promo_1_text_website-test2
Which is correct and in the schema settings file (example based on that one above it is matching and is set in theme customisation)
{
"type": "text",
"id": "locale_promo_1_text_website-test2",
"label": "Promo 1 Text",
"default": "FREE US SHIPPING OVER $35"
}
What am I doing wrong?
For anyone else that might come across the same issue here is the solution I got working:
{% assign promo_header_text_1 = 'locale_promo_1_text_' | append:shop.name %}
and to output
{{ settings[promo_header_text_1] }}

Change variable in included jinja2 template

Say I have two templates:
main.j2
{% include "vars.j2" %}
main: {{ var1 }}
vars.j2
{% set var1 = 123 %}
vars: {{ var1 }}
When run, only this line is output:
vars: 123
i.e. var1 is undefined in main.j2, even though it gets set to a value in the included vars.j2 template.
How can I pass variables from included template back to template that includes it? I considered chaining extends, but wondered if there's a more elegant approach.
I recently had a need to do the same thing, and found 2 solutions.
If you have Jinja version 2.10 or later, namespaces can be used:
main_ns.j2:
{% set ns = namespace() %}
{% include "vars_ns.j2" %}
main_ns: {{ ns.var1 }}
vars_ns.j2:
{% set ns.var1 = 123 %}
vars_ns: {{ ns.var1 }}
In Jinja 2.2 or later, it can be accomplished with block scoping of variables. I put the variable settings in the base template so that multiple children can extend it.
vars_block.j2:
{% set var1 = 123 %}
vars_block: {{ var1 }}
{% block content scoped %}{% endblock %}
main_block.j2:
{% extends "vars_block.j2" %}
{% block content %}
main_block: {{ var1 }}
{% endblock %}
You can try using with:
{% with var1=0 %}
{% include "vars.j2" %}
vars: {{ var1 }}
{% endwith %}

How to use Jekyll code in (inline) code highlighting?

How can I use for example {{ post.date | date: "%H:%M %p - %-d %b %y" }} in code highlighting?
The only way I have found so far is using {% raw %}{{ post.date | date: "%H:%M %p - %-d %b %y" }}{% endraw %}. However, then it shows the code snippet as unformatted text rather than with proper inline code highlighting.
If I use
`{{ post.date | date: "%H:%M %p - %-d %b %y" }}`
then the code is rendered instead of shown as code.
{% highlight html %}
{% raw %}
{% include google_analytics.html %}
{% endraw %}
{% endhighlight %}
That is how I do it.
Check it out live:
http://www.madhur.co.in/blog/2013/11/05/makingmostdatadirectory.html
{% highlight html %}
{% raw %}
{% for project in site.data.projects %}
{% if project.publish == true %}
<strong>{{ project.project }}</strong>
<span class="tag-project">{{ project.category }}</span>
{{ project.description }}
<hr/>
{% endif %}
{% endfor %}
</div>
{% endraw %}
{% endhighlight %}
you can do that easily by combining {% highlight %} and {% raw %}
for example
{% highlight ruby %}
{% raw %}
---
limit: 100
---
{% for post in site.posts limit: page.limit %}
{
"title": "{{ post.title }}",
"date" : "{{ post.date | date: "%B %d, %Y" }}",
"excerpt" : "{{ post.excerpt }}",
{% if post.categories %} "categories" : [
{% for category in post.categories %} "{{ category }}"
{% if forloop.last %}{% else %},{% endif %}
{% endfor %}
],
{% endif %}
{% if post.categories == nil %} "categories" : [], {% endif %}
"url": "{{ post.url }}",
{% if post.tags %} "tags" : [
{% for tag in post.tags %} "{{ tag }}"
{% if forloop.last %}{% else %},{% endif %}
{% endfor %}
]
{% endif %}
{% if post.tags == nil %} "tags" : [] {% endif %}
}
{% unless forloop.last %},{% endunless %}
{% endfor %}
{% endraw %}
{% endhighlight %}