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

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.

Related

Looping through a map in helm

I am new to yaml and helm templating and have a question in it.
I have the following in my values.yaml file
regionInfo:
usa:
DrRegions: uk
region: usa
region_key: usa
uk:
DrRegions: usa
region: uk
region_key: lhr
I need to iterate over the above map and the output should be in this format for each region
{{ $region }}:
dnsAbbrev: {{ region_key }}
abbrev: {{ region_key }}
drRegion: {{ DrRegions }}
I tried the following
Method 1
{{ range $reg, $props := .Values.regionInfo }}
{{ $reg }}:
dnsAbbrev: {{ $props.region_key }}
abbrev: {{ $props.region_key }}
drRegion: {{ $props.DrRegions }}
{{ end }}
Method 2
{{ range reg, props := .Values.regionInfo }}
{{ range $props }}
{{ $reg }}:
dnsAbbrev: {{ .region_key }}
abbrev: {{ .region_key }}
drRegion: {{ .DrRegions }}
{{ end }}
{{ end }}
I was getting couldn't range over map in both the scenarios... Any help or guidance on this is highly appreciated.

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.

URL's are repeated

I want to output my breadcrumbs using schema, but the #id repeats the URL twice or sometimes more than that?! So if I visit the about page, I see:
"#id":"http://localhost:1313/about/http://localhost:1313/about/",
When I use pagination, it repeats the URL even more:
"#id":"http://localhost:1313/blog/http://localhost:1313/blog//http://localhost:1313/blog/http://localhost:1313/blog//http://localhost:1313/blog/http://localhost:1313/blog/",
The code I am using:
Taken from: https://gohugohq.com/partials/breadcrumb-partial-with-structured-data-in-hugo/
{{ $url := replace .Permalink ( printf "%s" .Site.BaseURL) "" }}
{{ $.Scratch.Add "path" .Site.BaseURL }}
{{ $.Scratch.Add "breadcrumb" (slice (dict "url" .Site.BaseURL "name" "home" "position" 1 )) }}
{{ range $index, $element := split $url "/" }}
{{ $.Scratch.Add "path" $element }}
{{ $.Scratch.Add "path" "/" }}
{{ if ne $element "" }}
{{ $.Scratch.Add "breadcrumb" (slice (dict "url" ($.Scratch.Get "path") "name" . "position" (add $index 2))) }}
{{ end }}
{{ end }}
<script type="application/ld+json">
{
"#context": "http://schema.org",
"#type": "BreadcrumbList",
"itemListElement": [{{ range $.Scratch.Get "breadcrumb" }}{{ if ne .position 1 }},{{ end }}{
"#type": "ListItem",
"position": {{ .position }},
"item": {
"#id": "{{ .url }}",
"name": "{{ .name }}"
}
}{{ end }}]
}
</script>
So I'm not sure what your list page template looks like, but for example in mine I had
{{ partial "header.html" . }}
when it should have been
{{ partial "header" . }}
This removed the repeating url's. I have all of the same code you have rendering in my header partial.
There are 2 reasons URLs are repeated
Its known bug with hugo hot reloading. But final production version will not have it. So run hugo and check the public folder.
If the issue persists on public folder then check how many partials you are doing {{ $.Scratch.Add "path" .Site.BaseURL }}. After you add it once then same data is available via scratch on all partials of the same page.
I have blog post on breadcrumb partial for hugo with json-ld
I reuse the same scratch to display breadcrumbs on page.
DONT JUST ADD BREADCRUMBS FOR SEARCH ENGINE. SHOW THEM TO USERS ALSO.

Ansible: multi line over playbook

I have a question about multi line and Ansible playbook:
I have created a playbook with a line very large, I need to cut this line for better reading. How I can doing it?
- name: 'Create VM Azure :-P '
shell: if ! grep {{ item }} /tmp/vm_{{ rgName }}; then azure vm create --vm-size {{ groups['item'][vmsize] }} --resource-group {{ rgName }} --name {{ item }} --location {{ location }} --admin-username {{ username }} --ssh-publickey-file {{ sshfile }} --storage-account-name {{ rgName | lower }} --os-type {{ groups['item'][type_os] }} --image-urn {{ image }} --data-disk-size {{ disksize }} --subnet-id {{ subnetid_key }} --nic-names {{ item }}; fi
with_items: groups['test']
I would like to make it as follows way, but I have some errors when execute the playbook
- name: 'Create VM Azure :-P '
shell: if ! grep {{ item }} /tmp/vm_{{ rgName }}; then
azure vm create --vm-size {{ groups['item'][vmsize] }}
--resource-group {{ rgName }} --name {{ item }}
--location {{ location }} --admin-username {{ username }}
--ssh-publickey-file {{ sshfile }} --storage-account-name {{ rgName | lower }}
--os-type {{ groups['item'][type_os] }} --image-urn {{ image }}
--data-disk-size {{ disksize }} --subnet-id {{ subnetid_key }}
--nic-names {{ item }}; fi
with_items: groups['test']
The error:
ERROR! Syntax Error while loading YAML.
The error appears to have been in '/home/pvillarruel/docker/azure-ansible/data/playbook.yml': line 79, column 1, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
shell: if ! grep {{ item }} /tmp/vm_{{ rgName }}; then azure vm create --vm-size
{{ groups['item'][vmsize] }} --resource-group {{ rgName }} --name {{ item }}
^ here
We could be wrong, but this one looks like it might be an issue with
missing quotes. Always quote template expression brackets when they
start a value. For instance:
with_items:
- {{ foo }}
Should be written as:
with_items:
- "{{ foo }}"
Thanks
Use a YAML block scalar, they were designed exactly for this kind of thing:
shell: >
if ! grep {{ item }} /tmp/vm_{{ rgName }}; then
azure vm create --vm-size {{ groups['item'][vmsize] }}
--resource-group {{ rgName }} --name {{ item }}
--location {{ location }} --admin-username {{ username }}
--ssh-publickey-file {{ sshfile }}
--storage-account-name {{ rgName | lower }}
--os-type {{ groups['item'][type_os] }} --image-urn {{ image }}
--data-disk-size {{ disksize }} --subnet-id {{ subnetid_key }}
--nic-names {{ item }}; fi
The > indicates that the following block is a scalar and lines should be folded, i.e. a newline character will be converted into a single space. You also do not need to worry about special characters, everything will be taken literally in a folded block scalar.

Can I embed helpers with PyroCMS?

{{ blog:posts limit="1" order-by="created_on" order-dir="desc" category="nurse" }}
<p>
{{ helper:word_limiter limit="10" string={{ helper:strip_tags value=body }} }}
</p>
{{ /blog:posts }}
Doesn't seem to work. Ideas?
If you are trying to send a tag with attributes to another attribute it actually looks like this:
{{ blog:posts limit="1" order-by="created_on" order-dir="desc" category="nurse" }}
<p>
{{ helper:word_limiter limit="10" string={ helper:strip_tags value=body } }}
</p>
{{ /blog:posts }}
Double brackets say "I am starting Lex now".
Single brackets say "I am grouping these attributes to this call".

Resources