HUGO IN function not accepting variable as ITEM - go

I am using the IN function of Hugo that can be used as in SET ITEM as defined at Hugo Documentation for IN
I am trying to filter out posts of a particular category by the below code
{{ range where .Site.Pages "Section" "products"}}
{{ if in .Params.categories "New Arrival" }}
<li>{{ .Title }}</li>
{{end}}
{{end}}
the above code works completely fine.
But if I use a variable instead of a string for the ITEM. It always returns False.
{{ $display_product_cat := "New Arrival" }}
{{ range where .Site.Pages "Section" "products"}}
{{ if in .Params.categories $display_product_cat }}
<li>{{ .Title }}</li>
{{end}}
{{end}}
The above code does not work as expected.
Wondering if I am missing something here. Appreciate your time and help.

You can test what the boolean is by something like:
{{ $i := in .Params.categories "New Arrival" }}
{{ $i }} ## will output true|false
{{ $var := "New Arrival" }}
{{ $j := in .Params.categories $var }}
{{ $j }} ## will output true|false
If it's giving false outcomes, you might need to inspect how your .Params are actually constructed (what pattern are they in, etc.).
Providing more info will allow a better debug for you (like the snippet of your Params).

Related

how to remove the file extension from an href in an go template?

I am new to go-templates and i wonder if there's a way to remove the file extension within the template.
{{- range $val := .Filelist}}
<li>
<a href="{{$val}}">
{{ $val }}
</a>
</li>
{{- end }}
As you can see, i declared my filelist and declared the href. the outcome of this is for example:
1.graph
2.graph
But i only want the 1 or 2 to be shown.

Calling variables inside Hugo partial

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.

Range over string or string array in Go template

I am writing a Go template that uses consecutive alpha characters. As a simple example:
{{ range $i, $e := until 2 }}
{{ range $a := .[]string{"a", "b", "c"} }}
<p>{{ $e }}{{ $a }}. Sample Text</p>
{{ end }}
<br>
{{ end }}
Should yield:
<p>0a. Sample Text</p>
<p>0b. Sample Text</p>
<p>0c. Sample Text</p>
<br>
<p>1a. Sample Text</p>
<p>1b. Sample Text</p>
<p>1c. Sample Text</p>
<br>
The above code would work if go templates let you define arrays this way inside the template. I would need something similar to this, or the answer here, with the exception that I am not able to write my own functions. I am using this for another piece of software that lets me write go templates, but I cannot modify the go code itself, meaning no passing variables either.
Am I asking the impossible?
It looks like the template host uses Sprig. Use the list function from Sprig.
{{ range $i, $e := until 2 }}
{{ range $a := list "a" "b" "c"}}
<p>{{ $e }}{{ $a }}. Sample Text</p>
{{- end}}
<br>
{{- end}}
Run an example on the playground.

Hugo static site - issue trying to loop through multiple sub sub folders and returning one sorted list

I have a folder in “content” called “projects” which has sub folders and also sub-sub folders. I’m trying to get the latest 12 pages content of all the sub-sub folders to show in a single list by date order. (see illustration below)
My current code (that doesn’t quite work):
<ul>
{{ range .Sections }}
<li>{{ .Title }}
{{ range .Sections }}
{{ range first 12 .Pages }}
<ul>
<li>{{ .Title }}</li>
</ul>
{{ end }}
{{ end }}
</li>
{{ end }}
</ul>
This shows the first 12 pages from each sub-sub section. I need to get them all in one list and only show the latest 12 regardless or which sub-sub section they are in.
(This is on a list.html template and needs to be dynamic if it makes a difference - the folders could be called anything)
Cheers,
A.
if what you are looking for is listing 12 pages from each 'main' section, this should work:
<ul>
{{ range .Site.Sections }}
{{ range first 12 .Pages }}
<li>{{ .Title }}</li>
{{ end }}
{{ end }}
</ul>
Reference

In a template how do you access an outer scope while inside of a "with" or "range" scope?

When inside a with or range, the scope of . is changed. How do you access the calling scope?
{{with .Inner}}
Outer: {{$.OuterValue}}
Inner: {{.InnerValue}}
{{end}}
$ is documented in the text/template docs:
When execution begins, $ is set to the data argument passed to Execute, that is, to the starting value of dot.
You can save the calling scope with a variable:
{{ $save := . }}
{{ with .Inner }}
Outer: {{ $save.OuterValue }}
Inner: {{ .InnerValue }}
{{ end }}

Resources