HUGO How to use pagination inside terms page? - pagination

I'm trying to use a pagination inside a terms page (e.g. /layouts/tags/terms.html) because I got many tags.
And I also want to display all my tags sorted by popularity. For the last point, I did:
{{ range .Data.Terms.ByCount}}
...
{{ end }}
And it works fine.
I tried:
1)
{{ range .Data.Terms.ByCount}}
{{ range .Paginator.Pages }}
...
{{ end }}
{{ end }}
But I got "can't evaluate field Paginator in type hugolib.OrderedTaxonomyEntry" ERROR.
2)
{{ range .Paginator.Pages }}
{{ range .Data.Terms.ByCount}}
...
{{ end }}
{{ end }}
I got no error but no posts are displayed, only the pagination is there.
3)
{{ $paginator := .Paginate .Data.Terms.ByCount }}
{{ range $paginator.Pages }}
...
{{ end }}
I got: "error calling Paginate: cannot convert type hugolib.OrderedTaxonomy to Pages" ERROR.
I tried many others things but I couldn't figure out how to do display all my posts by popularity with a pagination in my taxonomy terms.html page.
Do you have more idea ?

In your terms.html page put the following code. It should work.
{{ range .Paginator.Pages }}
{{ .Title }}: {{ .RelPermalink }}
{{ end }}
But since you want it sorted, you probably need something like this.
{{ range .Paginator.Pages.ByDate }}
{{ .Title }}: {{ .RelPermalink }}
{{ end }}
You can sort using other parameters. Check the full list here.
You wanted to sort by popularity. I'm not sure what how you're defining popularity. But if popularity is a front matter variable, you can sort by it with the following.
{{ range (.Pages.ByParam "popularity") }}
<!-- ... -->
{{ end }}

I got the answer from somewhere else; it is:
{{ $topaginate := slice }}
{{ range $k, $v := .Data.Terms.ByCount}}
{{ $topaginate = $topaginate | append (site.GetPage (print $.Data.Plural "/" $v.Term )) }}
{{ end }}
{{ range (.Paginate $topaginate).Pages }}
{{ . }}
{{ end }}

Related

Jinja2 Get the first item in a sorted list

I'm currently using Jinja2 to display reviews (taken from a database) on my webpage, and I stumbled upon the sort() filter. So I wrote some code to sort the reviews by their lowest rating.
{% for reviews in reviews.all()|sort(attribute='rating', reverse=false) %}
{{ reviews.text }}
{{ reviews.rating }}
{% endfor %}
The code above works, but now I want to get the very first sorted items (reviews.text, reviews.rating) from the list. I tried using:
{{ reviews.text[0] }}
{{ reviews.rating[0] }}
But this only returns the first character. I also tried using the |first filter, but this didn't work either.
Sorry if this seems like a silly question - I'm still getting to grips with Jinja2 - but is there any way I can pick out the first sorted item from my list?
Okay, the solution was simple (bear with me): Jinja2 has a list of control structures, one of them being loop.first which can be used in this instance.
{% for reviews in reviews.all()|sort(attribute='rating', reverse=false) %}
{% if loop.first %}
{{ reviews.text }}
{{ reviews.rating }}
{% endif %}
{% endfor %}

Variable/Index within head of a loop

I have a for-loop within another for-loop and I would like the inner for-loop to go through the array test[INDEX] with [INDEX] being the index of the outer for-loop. I know I can get the loops index with the variable {{ loop.index() }}, however I do not know how to apply that within the head of my inner loop.
I've tried {% for x in test.{{ loop.index0 }} %}, but that throws me the error
Expected name or number.
Is there any way to do this?
In twig you can also use the array notation to get values from a variable
A note here is that the default loop.index is 1 indexed so you might want to use loop.index0 to get the correct offset
{% for f in foo %}
- {{ f }}: {{ bar[loop.index0] }}
{% endfor %}
As an alternative you can also get the key in the {% for key, value ... format
{% for key, value in foo %}
- {{ value }}: {{ bar[key] }}
{% endfor %}
demo
edit
As for you comment, you can succesfuly use this in any inner-loop, here is a more readable example
{% for country in countries %}
{{ country }}:
{% if cities[loop.index0]|default %}
<ul>
{% for city in cities[loop.index0] %}
<li>{{ city }}</li>
{% endfor %}
</ul>
{% endif %}
{% endfor %}
demo

concat string and variable in twig

I have this in my template
{{ ad.title_de }}
Now I'm incorporating multiple languages, so 'title_de' has to change
I also have a variable 'tld' which is one of de, en , fr
So I'd like to have something like
{% if tld == 'fr' %}
{{ ad.title_fr }}
etc
Any ideas?
Try using the attribute function.
http://twig.sensiolabs.org/doc/functions/attribute.html
attribute(ad, 'content_'~tld) should work.
Try with this:
{{ ad["title_" ~ tld] }}

twig slice filter on object print Array

http://twig.sensiolabs.org/doc/filters/slice.html
{% set varTest = 'azertyuiop' %}
{{ varTest[:2] }} {# show 'az' #}
but on an object as {{ myObj.name[:2] }} result is Array !
is-it the filter limit ?

Twig - using variable

I want to achive this is twig:
{{ form_widget(form.orderItems.0.enabled) }}
{{ form_widget(form.orderItems.1.enabled) }}
{{ form_widget(form.orderItems.2.enabled) }}
....
but the number to be a variable.
I tried this:
{% set index = 0 %}
{{ form_widget(form.orderItems.index.enabled) }}
Error: Method "index" for object "Symfony\Component\Form\FormView" does not exist
and this:
{{ form_widget(form.orderItems.{{index}}.enabled) }}
Error: Expected name or number
and this:
{{ form_widget(form.orderItems.~index~.enabled) }}
Error: Expected name or number
It is possible to achieve this :(
Some digging suggests you use the 'attribute' function - see Accessing array values using array key from Twig.
I suppose that would be something like
form_widget(attribute(form.orderItems, index).enabled)
Unfortunately I can't easily test that at the moment, but it should get you on the right track.
I am solving the same problem now and getting "expected name or number error" when I want to access variable dynamically.
I can not find simple answer, how to dynamically replace some part of variable in twig.
But It works without first dot as was first comment here.
{{ form[othervariable value].vars.label }}
and NOT
{{ form.[value].name }}

Resources