I'm creating a blog with Hugo.
i would like to list the first 3 Blog entries. That is not a problem so far.
{{ range first 3 .Data.Pages.ByPublishDate }}
But i need the index for adding css classes. I do that with this line
{{ range $index, $element := .Data.Pages.ByPublishDate }}
My problem now is how following. How do I get the index like in the second line of code but still limit the result to 3.
Unfortunately this doesn't seem to work.
{{ range first 3 $index, $element := .Data.Pages.ByPublishDate }}
Any ideas?
I think what you are looking for based on your examples is the following:
{{ range $index, $element := (first 3 .Data.Pages.ByPublishDate) }}
Related
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.
I have this code I use inside a partial in Hugo to pass context to it.
{{- $ctx := . -}}
{{- $curPage := .page -}}
{{- $otherVar := .otherVar -}}
{{- with $curPage -}}
{{ $section := .CurrentSection }}
{{ if .IsHome }}
<span class="post-section">{{ $section.Title }}</span>
{{ else }}
{{ $section.Title }}
{{ end }}
{{- end -}}
I then add {{- $curPage := . -}} at the top of the template where I want the partial to appear, then call the partial as {{ partial "partial-name.html" (dict "page" $curPage "otherVar" .) }}. However, the content returns nil on the homepage while it works everywhere else sitewide. Could anyone look at my code and tell me where I went wrong?
Sorry - didn't see your with statement.
So {{- $curPage := .page -}}
is a typo.
.Page
Tested on local - most present version of hugo.
Also note - I don't think homepage has a section so your span will output very little as most of the currentsection or section related will return nothing.
Since you call the partial like this:
{{ partial "partial-name.html" (dict "page" $curPage "otherVar" .) }}
^^^^^^^^^^^^
Notice
The dot (.) is contained in .otherVar. To find out if you are on the home page, use something simple like this at the top of partial-name.html:
{{ if .otherVar.IsHome }}
<pre>Debugging: YES .IsHome</pre>
{{ else }}
<pre>Debugging: NOT .IsHome</pre>
{{ end }}
After you test with the above GO HTML fragment, you can update your original code above.
TIP: In the Hugo world, it is common to use "context", "ctx", "page", or "Page" rather than "otherVar" as the name of the dictionary key that contains the dot. For a discussion about this, see Naming convention for page context (within a dictionary) when calling partial: seeking opinions in the Hugo discussion group.
ANOTHER TIP: There are some Hugo weirdnesses related to case sensitivity so I would not use "otherVar" anyway. Instead use "othervar", "context", or any name that is all lower case with no whitespace. I do this because I have spent a lot of time messing around with case sensitivity Hugo issues.
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.
I have a question about how to order a map by a slice in Go Templates and if it is possible.
Problem: I have a slice of ordered variable names that I want to display on a website, accompanying them I have a map of metadata of variable information that I would like to display together with the variable.
If I have the following struct that I pass in to the template:
type Data struct {
Variables []string
Information map[string]int
}
I would iterate over the slice and pass the variable name in to the map
{{ range $v := .Variables }} {{ index .Information $v }} {{ end }} // Doesn't work.
Here's a Go Playground with the example.
https://play.golang.org/p/AL2csnXdoU
Question: How can I do this?
I am fairly new to Golang. Thankful for any input.
The following should work. To access .Information inside range, you should use $, which is basically d in your Playground example.
{{ range .Variables }} {{ index $.Information . }} {{ end }}
I am trying to display a list of comma separated values, and don't want to display a comma after the last item (or the only item if there is only one).
My code so far:
Equipment:
{{$equipment := .Equipment}}
{{ range $index, $element := .Equipment}}
{{$element.Name}}
{{if lt $index ((len $equipment) -1)}}
,
{{end}}
{{end}}
The current output: Equipment: Mat , Dumbbell ,
How do I get rid of the trailing comma
A nice trick you can use is:
Equipment:
{{$equipment := .Equipment}}
{{ range $index, $element := .Equipment}}
{{if $index}},{{end}}
{{$element.Name}}
{{end}}
This works because the first index is 0, which returns false in the if statement. So this code returns false for the first index, and then places a comma in front of each following iteration. This results in a comma separated list without a leading or trailing comma.
Add a template function to do the work for you. strings.Join is perfect for your use case.
Assuming tmpl contains your templates, add the Join function to your template:
tmpl = tmpl.Funcs(template.FuncMap{"StringsJoin": strings.Join})
Template:
Equipment:
{{ StringsJoin .Equipment ", " }}
Playground
Docs: https://golang.org/pkg/text/template/#FuncMap
Another way to skin the cat, if you can create a new type for the field.
type MyList []string
func (list MyList) Join() string {
return strings.Join(list, ", ")
}
Then you can use the function in the template like regular:
{{ .MyList.Join }}
without index
it can be useful for any iteration, like on maps that do not have index:
{{ $first := true }}
{{ range $_, $element := .}}
{{if not $first}}, {{else}} {{$first = false}} {{end}}
{{$element.Name}}
{{end}}
Playground
If you are willing to use an external library, it seems like sprig library has a "join" function (see here):
join
Join a list of strings into a single string, with the given separator.
list "hello" "world" | join "_"
The above will produce hello_world
join will try to convert non-strings to a string value:
list 1 2 3 | join "+"
The above will produce 1+2+3