I have a simple template fetching a list of blog posts:
<div class="row">
<div class="cell">
{{range .Pages}}
<p class="dialogue dialogue--wide">
{{.Title}}
</p>
{{end}}
<br />
<br />
<br />
</div>
</div>
I would like to alternate the design between posts e.g. the first will use a certain class like
and the second record (or even and odd records if you prefer) another p e.g.
thank you!
I suggest solving this with CSS to save yourself some trouble. You can use odd or even in nth-child:
.dalogue:nth-child(odd) {
// CSS Property
}
Alternatively, you can use the index of the items. Below, I am using the modulo operator to determine if it's odd or even.
{{ range $index, $page := .Pages }}
{{ if eq (mod $index 2) 0 }} odd {{ else }} even {{ end }}
{{end}}
Note, I am switching odd and even around. Because the index starts at 0 and not at 1. But when looking at a table, you start counting at 1 and this is also how CSS would behave.
I have implemented the mod function myself using template.Funcs.
t.Funcs(map[string]any{"mod": func(a, b int) int { return a % b }})
https://go.dev/play/p/aAupH-4CugV
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 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.
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.
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).
In the project I'm working on, I need to display 5 of the latest news articles on the website. In the Controller, I have written the following code:
$news = $repository->createQueryBuilder('p')
->Where('p.contenttype = :type')
->setParameter('type', 'newsarticle')
->orderBy('p.lastedit', 'ASC')
->getQuery();
$latestnews = $news->getResult();
This doesn't work for some reason, as I get the error message:
Item "url" for "Array" does not exist in "ShoutMainBundle:Default:page.html.twig" at line 34
However, when I change the getResult(); to getSingleResult(); it works, but will only display the one record (which is what I expect when I use that code).
This is where I come unstuck and confused about what I'm supposed to be doing. I have googled "how to display multiple records in symfony" and I haven't found the answer. (If the answer has been out there, I apologise in advance for this). In normal PHP, I would expect to do a foreach loop (something similar anyway) to get the results which I need. But I also have a feeling that to achieve what I want I need to do something in Twig. But what I need to do I don't know.
Any help with this would be very much appreciated.
Thanks
Edit:
Here is the template code that is used to display this:
<section id="latestnews">
<h2>Latest News</h2>
<ul>
<li><a href="..{{ news.url }}" title="Read {{ news.title }}" />{{ news.title }}</a></li>
</ul>
</section>
Your code tries to read from the variable news, and assumes that this variable has fields url and title. If your controller returns an array, you have to tread news as an array and iterate over it.
<section id="latestnews">
<h2>Latest News</h2>
<ul>
{% for news in latestnews %}
<li><a href="..{{ news.url }}" title="Read {{ news.title }}" />{{ news.title }}</a></li>
{% endfor %}
</ul>
</section>
It looks like in your template, you're looking for an object that's not found. It's looking for the url in an array object, but it doesn't exist. I think you need to put in a check, to see if that exists in the array, and then echo if it does. So something like if(news.url) echo news.url;
It may not be that exact syntax obviously, I'm not all that familiar with twig, but something similar to that.
You need to loop through the "news" results array in Twig.
{% for n in news %}
<li><a href="..{{ n.url }}" title="Read {{ n.title }}" />{{ n.title }}</a></li>
{% endfor %}