Navigation for pages (not for posts) in Jekyll - pagination

I'm building a website with Jekyll, just with pages.
I want to find a way to generate previous and next links for the pages, with an attribute for the pages like order.
Is there something that can do the job (without plugin)? I could only find something about posts.

{% assign sortedPages = site.pages | sort:'order' | where: 'published', true %}
{% for p in sortedPages %}
{% if p.url == page.url %}
{% if forloop.first == false %}
{% assign prevIndex = forloop.index0 | minus: 1 %}
<a href="{{site.baseurl}}{{sortedPages[prevIndex].url}}">
previous : {{sortedPages[prevIndex].title}}
</a>
{% endif %}
{% if forloop.last == false %}
{% assign nextIndex = forloop.index0 | plus: 1 %}
<a href="{{site.baseurl}}{{sortedPages[nextIndex].url}}">
next : {{sortedPages[nextIndex].title}}
</a>
{% endif %}
{% endif %}
{% endfor %}
This will do the job.
In order to filter which page you publish, you can add a published variable in pages front matter.
setting the variable
published: true -> this is a boolean
published: 'true' -> this is a string
using where filter
| where: 'published', true will test for boolean
| where: 'published', 'true' will test for string

Related

How to compare dates in twig

I have the following twig template
{% for id,value in item.attributes %}
{% set attribute_details = getAttributeSet(id) %}
ID: {{id}}<br>
VALUE: {{value.sec|date("d/m/Y")}}<br>
TYPE: {{data}}<br>
LABEL: {{attribute_details.label}}<br>
{% endfor %}
resulting in the following text:
ID: 607ecae15fb8e0c3c2d7ca02
VALUE: 19/05/2021
TYPE:
LABEL: scadenza_offerta
How can I check wether VALUE is grater than 19/05/2021? And How I can hide the text otherwise?
I have try this code but it does not work:
{% if VALUE == '19/05/2021' %}
work
{% endif %}
As seen in the linked answer
{% if date(value.sec) > date('19/05/2021') %}
work
{% endif %}
demo

TWIG - include variables in different template

I would like to include the same variables in different templates
vars_catchphrase.twig
{% set catchphrase_size = '' %}
{% if var.tile_catchphrase|length <= 4 %}
{% set catchphrase_size = 'size-lg' %}
{% elseif var.tile_catchphrase|length >= 5 and var.tile_catchphrase|length <= 8 %}
{% set catchphrase_size = 'size-md' %}
{% elseif var.tile_catchphrase|length >= 9 and var.tile_catchphrase|length <= 12 %}
{% set catchphrase_size = 'size-sm' %}
{% elseif var.tile_catchphrase|length >= 13 %}
{% set catchphrase_size = 'size-xs' %}
{% endif %}
I tried to include with this (because the context is sometime different) :
{% include 'vars_catchphrase.twig' with { 'var' : post } %}
When the context is different from post I use another one :
{% include 'vars_catchphrase.twig' with { 'var' : item } %}
example.twig
{% for item in list %}
{% include 'vars_catchphrase.twig' with { 'var' : item } %}
<p class="catchphrase {{ catchphrase_size }}">{{ item.title }}</p>
{% endfor %}
The variable is empty. Can I have some help please ?
Templates you include have their own variable scope, this means variables defined inside this template will not be known out the template. This said, included templates also can't alter the parent's context (by default), this is due to twig passing the context array by value, not by reference.
foo.twig
{% set foo = 'foo' %}
{% include 'bar.twig' %}
{{ foo }}
bar.twig
{% set foo = 'bar' %}
The example above will still output foo
In order to solve your problem, I'd suggest adding a custom filter to twig
<?php
$twig->addFilter(new \Twig\TwigFilter('catchphrase_size', function($value) {
switch(true) {
case strlen($value->tile_catchphrase) >= 13: return 'size-xs';
case strlen($value->tile_catchphrase) >= 9: return 'size-sm';
case strlen($value->tile_catchphrase) >= 5: return 'size-md';
default: return 'size-lg';
}
});
This way you can use the filter where ever,
{% for item in list %}
<p class="catchphrase {{ item|catchphrase_size }}">{{ item.title }}</p>
{% endfor %}

How can I search for the search query value from multiple specified fields in Craft CMS?

In Craft CMS I want to search for the search query value for only some fields/ multiple fields - but not all.
For example limiting to the fields title, introduction, cardContent.
I've added a search: property to to my queryEntry object with the value of title and the query string. But I would like to add more fields.
{% set searchQuery = craft.app.request.getParam('q') %}
{# {% set queryEntries = craft.entries({
section: queryFilters
}).search(searchQuery) %} #}
{% set queryEntries = craft.entries({
search: 'title:' ~ searchQuery,
order: 'score'
}) %}
{% if craft.app.request.getParam('q') %}
{% set searchQuery = '"' ~ craft.app.request.getParam('q') ~ '"' %}
{% set queryEntries = craft.entries({
search: 'title:' ~ searchQuery ~ ' OR cardContent:' ~ searchQuery ~ ' OR introduction:' ~ searchQuery ,
order: 'score'
}) %}
{% endif %}
Get the query string
add searchTerms is concatinated in a string using OR and the query
This returns the array of entries matching the queryEntries.search and you can do what you like with this - eg loop over and display results
You can concatenate any number of fields with their value in the variable and then you can simple pass that in the search parameter with entries query. Here is the example code for that.
{% set nameparam = craft.app.request.getParam('data') %}
{% set categoryparam = craft.app.request.getParam('data1') %}
{% set queryString = '' %}
{% if nameparam is defined and nameparam is not empty %}
{% set queryString = queryString ~ 'title:*'~nameparam~'* ' %}
{% endif %}
{% if categoryparam is defined and categoryparam is not empty %}
{% set queryString = queryString ~ 'blogCategory:'~categoryparam~' ' %}
{% endif %}
{% if queryString is defined and queryString is not empty %}
{% set queryParams = {
search: {
query: queryString,
order: 'score'
},
} %}
{% else %}
{% set queryParams = {} %}
{% endif %}
{% set queryEntries = craft.entries(queryParams) %}

Symfony & Twig: how to get vars in twig by DB data?

One question please.
{{ dump(app.user.slugName) }}
If I do the above snippet in Twig, I get the slugName of the user loged ("my-user-2", i.e.) in the app (SlugName is an atribute of the entity user). Ok & Correct. But... I want to order this action from a var (var from BD data)
I have a variable named option which is set like this:
{% set option = 'app.user.slugName' %}
But when I'm trying output this variable with {{ dump(option)}} it returns app.user.slugName as literal. It does not return my-user-2.
Is there are any way in twig to solve this? It's a function to generate a menu, but some links needs some parameters.
I see what you mean, but Twig can't evaluate expression like that.
To achieve something like that you would need a snippet like this,
{% set value_methods = 'app.user.slugname' %}
{% set option_value = _context %}
{% for method in (value_methods|split('.')) if method != '' %}
{% set option_value = attribute(option_value, (method|replace({'()': '', }))) %}
{% endfor %}
{{ option_value }}
twigfiddle
(edit)
Remember you can create a macro to achieve some reusability for this snippet,
{% import _self as macros %}
{{ macros.evaluate(_context, 'app.user.slugname') }}
{% macro evaluate(context, value_methods) %}
{% set option_value = context %}
{% for method in (value_methods|split('.')) if method != '' %}
{% set option_value = attribute(option_value, (method|replace({'()': '', }))) %}
{% endfor %}
{{ option_value }}
{% endmacro %}

Trouble passing a string from an array to a variable within a for loop

Im setting a "category" and passing it to a template that I'm including:
{% set categoryA = {
category: "categoryA",
}
%}
{% include "something.twig" with categoryA %}
{% set categoryB = {
category: "categoryB",
}
%}
{% include "something.twig" with categoryB %}
This is working fine but I'm repeating a lot of code which I want to avoid (in my actual code there are more than 2 categories).
Im trying to put the categories in an array and include something.twig for each one, passing a different category for each instance:
{% set categories = ['categoryA', 'categoryB', 'categoryC', 'categoryD', 'categoryE'] %}
{% for i in categories %}
<h3>{{ i }}</h3>
{% set categoryOption = {
category: {{ i }},
}
%}
{% include "something.twig" with categoryOption %}
{% endfor %}
The title in the h3 is printed OK however the categoryOption category is passed as [object Object] rather than the string name as I need
For example, you use category in the "something.twig":
...
{{ category|default }}
...
So you can use "include" like:
{% include "something.twig" with { category: 'Name of Category' } %}
Full code:
{% set categories = ['categoryA', 'categoryB', 'categoryC', 'categoryD', 'categoryE'] %}
{% for i in categories %}
<h3>{{ i }}</h3>
{% include "something.twig" with { category: i } %}
{% endfor %}

Resources