How can I write an if statement in resources.Get Hugo? - go

I have the following code:
{{ with resources.Get .Site.Params.image }}
<meta property="og:image" content="{{ .Permalink }}" />
{{ end }}
I get a image path in the config.toml file. However I now want to be able to overwrite the file by frontmatter data... Something like this (in pseudo code):
{{ if isset .Image }}
{{ $image := .Image }}
{{ else }}
{{ $image := .Site.Params.image }}
{{ end }}
{{ with resources.Get $image }}
...
How can I write this statement? Also if you have good tutorials on these statements and syntax let me know!

Use the or function:
{{ with resources.Get (or .Params.thumbnailImage .Site.Params.ogImage) }}
<meta property="og:image" content="{{ .Permalink }}" />
{{ end }}

EDIT: I updated to #brendan solution.
This is my solution:
{{ $ogImage := "" }}
{{ if .Params.thumbnailImage }}
{{ $ogImage = .Params.thumbnailImage }}
{{ else }}
{{ $ogImage = .Site.Params.ogImage }}
{{ end }}
{{ $ogImageRender := resources.Get $ogImage }}
{{ with $ogImageRender }}
<meta property="og:image" content="{{ .Permalink }}" />
{{ end }}
:= defines variables and set initial value
= set variables to another value

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.

Use the same template with different param/variables in 1 page

I am using Go gin gonic for my web app. How do I use the same template file multiple times in 1 page with different variables passed to the template.
segment.tmpl
{{ define "segment" }}
<div>{{ .Variable }}</div>
{{ end }}
layout.tmpl
<!DOCTYPE HTML>
<html>
<body>
{{ template "segment . }} #with a variable 1
{{ template "segment . }} #with different variable
{{ template "segment . }} #another same template with another
</body>
</html>
main.go
r.GET("/home/", func(c *gin.Context) {
tmpl := template.Must(template.ParseFiles("templates/layout.tmpl", "templates/product_add.tmpl", "templates/segment.tmpl")
r.SetHTMLTemplate(tmpl)
c.HTML(200, "layout", gin.H {
"Variable1": "var1",
"variable2": "var2",
})
}
How do I use segment.tmpl multiple times in the page "home" and passing different kind of variables to the segment.tmpl?
I have searched everywhere and have found nothing, the closest thing is template.Clone, but still couldn't find any examples of it.
You can pass any value as the "pipeline" to the template, it doesn't have to be the "dot", i.e. you could pass the result of a function call, or, in this case, the result of accessing a map's value.
{{ template "segment" .Variable1 }}
and then inside the template "segment" you can refer to the pipeline using the dot, i.e. {{ . }}.
segment.tmpl
{{ define "segment" }}
<div>{{ . }}</div>
{{ end }}
layout.tmpl
<!DOCTYPE HTML>
<html>
<body>
{{ template "segment .Variable1 }}
{{ template "segment .Variable2 }}
{{ template "segment .AnotherVariable }}
</body>
</html>

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

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