what does {{template "name" pipeline}} mean [duplicate] - go

This question already has an answer here:
Golang template engine pipelines
(1 answer)
Closed 4 years ago.
In https://golang.org/pkg/text/template/#hdr-Actions, it has the following explanation
{{template "name" pipeline}} The template with the specified name is
executed with dot set to the value of the pipeline.
what does it mean? what's dot for?
For example, I see the following template code -
{{ define "header" }}
{{ template "top" . }}
{{ template "needs" }}
...
{{ end }}
What is the '.' for following the "top" in the code above?

The value '.' is the current value or cursor as explained in the third paragraph in the documentation:
Annotations in the template refer to elements of the data structure (typically a field of a struct or a key in a map) to control execution and derive values to be displayed. Execution of the template walks the structure and sets the cursor, represented by a period '.' and called "dot", to the value at the current location in the structure as execution proceeds.
The command {{ template "top" . }} executes the template "top" with dot as the argument. Inside "top", dot is set to the argument.

Related

Changing a html class after the first item in a Hugo loop

So I am making a review carousel using Bootstrap and Hugo, I've got code that breaks down into this:
{{ range seq 1 3 (len site.Data.reviews) }}
...
{{ range seq . (add . 2) }}
{{ with (index site.Data.reviews (string .)) }}
{{ .des }}
{{ end }}
{{ end }}
...
{{ end }}
So there's two loops, one to make the slides for the carousel, and the other to fill the slides with data files. The issue is I need to delete the active class and adjust the data-bs-interval input on the next few slides I thought about making an if statement but I'm not sure how to replace the first div with one that doesn't have the active class after that in whats generated.
I don't know if this is the best solution to it, instead of editing the loop I wrote a bit of javascript:
var addActive = document.getElementById('carouselExampleDark').getElementsByClassName('carousel-item')[0];
addActive.classList.add("active");
That works for my use case so I'll leave it at that.

How do you parse brackets from path string on a variable return in Hugo?

I am writing a layout template for a Hugo generated post/page. The .yaml header is
image:
- post/mytitle/image.jpg
The template incorporates the variable in Hugo as
{{ .Params.Image }}
When served, the variable is returned as
[post/mytitle/image.jpg]
My html then becomes
<img src="[post/mytitle/image.jpg]"/>
which is 404 in the browser. I've tried a number of Hugo functions to no avail like {{ trim .Param.Image "[]" }} and {{ subset .Params.Image 1 -1 }} and {{ print .Params.Image }}. Each time Hugo returned the error: "error calling substr: unable to cast []string{"post/mytitle/image.jpg"} of type []string to string".
How do I get the variable to return the string without the brackets, or alternatively, how do I omit the brackets from the string?
In Go template, you access an item in a slice with index:
{{ index .Params.Image 0 }}
The question is why the value is a sequence in the first place. You could simply do
image:
post/mytitle/image.jpg
Then you could keep the original syntax since it is now a simple value, not a sequence.
If you want to possibly include multiple images, you'd do
{{ range .Params.Image }}<img src="{{.}}">{{ end }}
Then you can have
image:
- post/mytitle/image.jpg
- post/mytitle/anotherimage.jpg

Hugo data files from dynamic parameter

I'm developing a big hugo template. I try to simplfy the problem, so I have two datafile:
PROMO_00_1.yaml
PROMO_00_2.yaml
that are phisically stored in this directory:
themes/data/hp/
So, in the site config the user will decide which of this data file will be used simply indicate it in a param (HpElement).
In the template I call the partial in this way:
{{ partial "multiplepages/homepage/promos/00_promo_singleslide_text_video" (dict "context" . "data" $.Site.Params.HpElement) }}
In a partial I write:
{{ $data_partial := (printf "$.Site.Data.homepage.%s" .data)}}
{{ $data_partial}}
and the Hugo output is on the website:
$.Site.Data.homepage.PROMO_00_1
What I need is to access the single variable inside the .yaml file but the user MUST can decide which YAML file have to use. How can I achieve that?
Thanks
I just finished up a similar use case where I needed to select a YAML based on a date stamp. The index function is key to this operation.
https://gohugo.io/functions/index-function/
Here is a simplified version of my situation:
{{ $date := now.Format "s20060102"}}
{{ $data := (index .Site.Data.schedule $date) }}
{{ with $data }}
<h1>.longdate</h1>
{{ range .times }}
<h2>{{ .name }} - {{ .location }}
{{ end}
{{ end}
The example in the Hugo documentation uses an example where the data file is selected based on an attribute in the front matter.

Cannot access liquid object properties (Jekyll)

I am new to Jekyll and am creating a documentation website for one of my projects. I am trying to create a sidebar that displays the current page in the documentation. To store the structure of the documentation, I created the a file in the _data folder called subsections.yml. Here is the file:
- title: Quickstart # Section
data:
- Get started # Subsections
- The basics
- title: API documentation # Another section with subsections
data:
- Introduction
Here is an excerpt from the html template file that will be used for pages in the documentation. (liquid template engine):
{% assign subsecs = site.data.subsections | where: 'title', page.section %}
The code above creates a variable called subsecs which is created by reading subsections.yml and filtering out the data on the section the documentation page is about. So if the page's section was Quickstart, the subsecs variable would contain all the data from the Quickstart section from subsecitons.yml. I tested this out with {{ subsecs }} and it worked by outputting:
{"title"=>"Quickstart", "data"=>["Get started", "The basics"]}
However, when I try to access a certain property from this object like title:
{{ subsecs.title }}
nothing is returned. Why is this happening, and how can I access property methods in liquid? The syntax looks correct, bu when I try it, an empty string is rendered.
I tried looking at the liquid documentation, but found nothing other than method.property, which doesn't work for some reason. I also looked at similar SO questions.
The where filter is returning an array.
{% assign subsecs = site.data.subsections | where: 'title', page.section %}
{{ subsecs | inspect }}
inspect filter prints => [{"title"=>"Quickstart", "data"=>["Get started", "The basics"]}] brackets denotes an array.
You can do :
{% assign subsecs = site.data.subsections | where: 'title', page.section | first %}
{{ subsecs | inspect }}
The first filter extracts the first element of your array.
inspect filter now prints => {"title"=>"Quickstart", "data"=>["Get started", "The basics"]}
You can now access your object's properties like subsecs.title.

How to set a golang variable to a template variable

I am new to golang and want to understand how to assign a template variable in golang using the golang variable.
I am using go-swagger to generate go code.
Below is the piece of a customized template to generate my swagger client.
func demo() {
{{range .Operations}}
Value := main.CheckAvail(*{{.Package}}.{{ pascalize .Name }})
{{$value := .Value}}
{{if $value }}
{{ pascalize .Name }}
{{end}}
{{end}}
}
But it gives me the error that:
" <.Value>: can't evaluate field Value in type generator.GenOperation ".
How do I assign the value to the $value? Any help?
This is not possible. The template file is a wire frame for a go file. It just generates a file of go code, which is not executed yet.
All in {{ ... }} is evaluated, when the template is parsed. At this point, all text outside the curly braces is just plain text. After the (successful) execution of the template generation the generated text will be picked up by the go compiler and compiled (if it is correct go code).
In this case you cannot evaluate Value.

Resources