Hugo Nested loop to iterate through multi-level YAML - yaml

I have page front matter with multiple levels. My page front matter looks like this:
grids:
- template: section
background_image: "/uploads/2018/05/01/q-mark.png"
rows:
- template: row
stack-columns: tablet-s
cols:
- template: column
title: "Column-1 Title"
- template: section
background_image: "/uploads/2018/05/01/lk.png"
rows:
- template: row
stack-columns: tablet-s
cols:
- template: column
title: "Column-2 Title"
rows:
- template: row
stack-columns: tablet-l
cols:
- template: column
title: "Column-3 Title"
I can display the first-level template name for both of my grids:
{{ range .Params.grids }}
{{ .template }}
{{ end }}
How can I return the template name of rows and cols within the loop?

I figured it out. You can use a nested range to access parameters with multiple levels:
{{ range .Params.grids }}
<p>Grid template name: {{ .template }}</p>
{{ range .rows }}
<p><strong>row template name: </strong>{{ .template }}</p>
{{ range .cols }}
<p><em>col template name:</em> {{ .template }}</p>
{{ end }}
{{ end }}
{{ end }}
This displays:
Grid template name: section
row template name: row
col template name: column
Grid template name: section
row template name: row
col template name: column

Related

Ansible Nested variables and Jinja2 templates

I'm trying to figure out why my jinja2 template (and ansible for that matter) cannot find my variables in my inventory file.
Here is my inventory file:
all:
hosts:
test05:
ansible_host: 192.168.x.x
filebeat:
version: 7.15.2
applog:
- title: Separate Application Log Path with Tags
type: log
paths:
- /var/log/something/moresomething/current
tags: '["something", "application"]'
- title: Separate Application Log Path, with Tags, and "decode_json_fields" processor.
type: log
paths:
- /var/log/something/moresomething/blah-shell.log
tags: ["application", "something"]
fields: ["message"]
depth: 2
- title: Separate Application Log Path, with Tags, and Multiline fields
type: log
paths:
- /var/log/something/moresomething/production.log
tags: ["application", "something"]
multiline_type: pattern
multiline_patern: 'Started'
multiline_negate: true
multiline_match: after
Then attempting to get the first title. I'm doing the following:
- name: debugging
debug:
var: filebeat.applog.title
when I run this I end up getting filebeat.applog.title: VARIABLE IS NOT DEFINED! which I think is good since it doesn't know what title I want. So changing this to
- name: debugging
debug:
var: filebeat.applog.0.title
I end up getting what I want filebeat.applog.0.title: Separate Application Log Path with Tags. However, how do I use this in a jinja2 template?
I have the following for a template, I know I need to update this to loop through the different items in my inventory. That's a different problem on how to loop through this.
title: {{ filebeat.applog.title }}
- type: {{ filebeat.applog.type }}
enabled: true
paths:
- {{ filebeat.applog.path }}
tags: {{ filebeat.applog.tags }}
{% if filebeat.applog.fields is defined %}
processors:
- decode_json_fields:
fields: {{ filebeat.applog.fields }}
max_depth: {{ filebeat.applog.depth }}
target: {{ filebeat.applog.target | default "" }}
{% endif %}
{% if filebeat.applog.multiline_pattern is defined %}
multiline.type: {{ filebeat.applog.multiline_type }}
multiline.pattern: {{ filebeat.applog.multiline_pattern }}
multiline.negate: {{ filebeat.applog.multiline_negate }}
multiline.match: {{ filebeat.applog.multiline_match }}
{% endif %}
each time I get the following, even when I do use {{ filebeat.applog.0.logtitle }} in the template:
fatal: [test05]: FAILED! => changed=false
msg: |-
AnsibleError: template error while templating string: expected token 'end of print statement', got 'string'. String: title: {{ filebeat.applog.title }}
- type: {{ filebeat.applog.type }}
enabled: true
paths:
- {{ filebeat.applog.path }}
tags: {{ filebeat.applog.tags }}
{% if filebeat.applog.fields is defined %}
processors:
- decode_json_fields:
fields: {{ filebeat.applog.fields }}
max_depth: {{ filebeat.applog.depth }}
target: {{ filebeat.applog.target | default "" }}
{% endif %}
{% if filebeat.applog.multiline_pattern is defined %}
multiline.type: {{ filebeat.applog.multiline_type }}
multiline.pattern: {{ filebeat.applog.multiline_pattern }}
multiline.negate: {{ filebeat.applog.multiline_negate }}
multiline.match: {{ filebeat.applog.multiline_match }}
{% endif %}
I'm not sure what I'm missing or doing wrong. I'm thinking I'm doing something wrong since this the first time doing something like this.
So the template you have should either:
have a for loop to iterate over filebeat.applog
OR
reference n'th element of filebeat.applog
Aside from that, there are some errors like below:
1.
target: {{ filebeat.applog.target | default "" }}
This is the main one, and this is what the error message is complaining about, i.e. got 'string'. The correct usage for default filter is {{ some_variable | default("") }}.
2.
{% if filebeat.applog.multiline_pattern is defined %}
In the inventory this variable is mis-spelled, i.e. multiline_patern (missing one "t"). Fix this in your inventory.
3.
when I do use {{ filebeat.applog.0.logtitle }} in the template
This should be {{ filebeat.applog.0.title }} to work.
Considering the above fixes, a template that loops over filebeat.applog such as below should work:
{% for applog in filebeat.applog %}
title: {{ applog.title }}
- type: {{ applog.type }}
enabled: true
paths: {{ applog.paths }}
tags: {{ applog.tags }}
{% if applog.fields is defined %}
processors:
- decode_json_fields:
fields: {{ applog.fields }}
max_depth: {{ applog.depth }}
target: {{ applog.target | default("") }}
{% endif %}
{% if applog.multiline_pattern is defined %}
multiline.type: {{ applog.multiline_type }}
multiline.pattern: {{ applog.multiline_pattern }}
multiline.negate: {{ applog.multiline_negate }}
multiline.match: {{ applog.multiline_match }}
{% endif %}
{% endfor %}

Hugo: How can I display an image in my post list

I am trying to display an image in my post list.
In order to achieve that I added some tags in my post.md:
---
title: "Hello"
header_image: /images/blog/2019/water.jpg
images: /images/blog/2019/water.jpg
resources:
src: /images/blog/2019/water.jpg
title: "The image I want"
---
Then I edited list.html and tried different things:
{{ define "main" }}
<div class="archive animated fadeInDown">
<ul class="list-with-title">
<div class="listing-title">{{.Title}}</div>
{{ range .Pages }}
<ul class="listing">
<div class="listing-item">
<div class="listing-post">{{ .Title }}
{{ with .Resources.ByType "image" }}
<div class="Image">
{{ range . }}
<img src="{{ .RelPermalink }}">
{{ end }}
</div>
{{ end }}
{{ $.Param "header_image" }}
-- {{ range .Page.Resources }}
THERE IS ONE ITEM => NOT WORKING
{{ end }} <<
<div class="post-time"><span class="date">{{.Date.Format "Jan 2" }}</span></div>
</div>
</div>
</ul>
{{ end }}
</div>
{{ end }}
But when I try to display Resources, I always get [] (nothing)
Any idea what I am doing wrong?
I don't think your {{ $.Param "header_image" }} is working, either.
The way to access your custom, non-standard variables on pages, as well as sites, is through the .Params object, e.g. .Params.header_image. Note the small letter at the beginning, as opposed to capital letters for built-in params.
Page-level params on the Hugo Docs
Custom page params
To access
---
header_image: /images/blog/2019/water.jpg
---
you can use this in your page template.
{{ .Params.header_image }}
Resources
Page resources on Hugo Docs
It seems that resources is actually an array of objects, and with yaml, you should actually have something like this (note the dash):
resources:
- src: /images/blog/2019/water.jpg
title: "The image I want"
Also mind that this feature seems to only be available only for page bundles
Debugging
You can use {{ printf "%#v" .Resources }} for debugging.

What are Parse Html token methods for email templates in golang?

I am trying to create email templates having html tokens in golang. I have searched all over the web and found
"html/template"
library. It supports token format like below
Hello {{.Name}}
Confirm email address
But the requirement for html token is something like
Name: {{ test.name }}
Phone: {{ test.phone }}
Address: {{ test.address }}, {{ test.city }}, {{ test.state }} {{ test.zip }}
I could not found such token system in golang or any library supporting such format. Can anyone please tell how can I achieve to create such tokens. There should be no dot before the attribue. either it should be only the attribute like {{Name}} or like {{ test.name }}.
Thank you!
If you can use a $ before attribute names, you can use the template's [with][1] action. Something like:
tmpl :=`
{{ with $test := . }}
Name: {{ $test.Name }}
Phone: {{ $test.Phone }}
Address: {{ $test.Address }}, {{ $test.City }}, {{ $test.State }} {{ $test.Zip }}
{{ end }}
`
Note that each struct field needs to be exported.

Golang iris framework render inside a range

I'm trying to loop over items and render it in separate template, but {{ . }} in item.html contains root variables instead of a single item. I tried to assign item to a variable {{ range $i, $item := .Items }}, but the result is the same.
<!-- list.html -->
{{ range .Items }}
{{ . }} this one is OK
{{ render "item.html" }}
{{ end }}
<!-- item.html -->
{{ . }} this one is not

Jekyll: Dealing with Images in Markdown

I've got a design for my post page that is made up of text and images, more specifically, galleries. Take a look at the illustration below:
I've got these galleries set up my front matter like so:
---
gallery-1:
rows:
- images:
- { url: '1.jpg'}
- images:
- { url: '2.jpg'}
- { url: '3.jpg'}
gallery-2:
rows:
- images:
- { url: '4.jpg'}
- { url: '5.jpg'}
---
Is there a way to print these galleries to the page on my .md file. I know the code below won't work, but something similar?
This is my markdown
[gallery-1]
This is more markdown
[gallery-2]
Is something like this possible with Jekyll?
Any help with this is appreciated. Thanks in advance!
First, let's simplify your yaml front matter :
---
galleries:
# gallery number one
1:
# row one in gallery one
-
- { url: '1.jpg', alt: 'alt 1'}
# row two in gallery one
-
- { url: '2.jpg', alt: 'alt 2'}
- { url: '3.jpg', alt: 'alt 3'}
# gallery number two
2:
# row one in gallery two
-
- { url: '4.jpg', alt: 'alt 4'}
- { url: '5.jpg', alt: 'alt 5'}
other front matter like title, ...
---
Your .md file :
Markdown
{% include gallery.html gallery=1 %}
Other markdown
{% include gallery.html gallery=2 %}
And then the _includes/gallery.html file :
{% comment %}-----------------
- This page receives a gallery index (include.gallery)
- It then assign the gallery[include.gallery] to the rows variable
%}-----------------{% endcomment %}
{% assign rows = page.galleries[include.gallery] %}
{% comment %}%}-----------------
We now loop over rows
%}-----------------{% endcomment %}
{% for row in rows %}
<div class="row">
{% comment %}%}-----------------
and then loop over images in row
%}-----------------{% endcomment %}
{% for img in row %}
<p>src="{{ site.baseurl }}{{ img.url }}" alt="{{ img.alt }}"</p>
{% endfor %}
</div>
{% endfor %}
See yaml documentation And Jekyll template documentation

Resources