Using `with_scope` in Locomotive CMS - locomotivecms

###I have a question regarding the use of with_scope in Locomotive CMS.
To start I have a model, movies, with full fields, one of which is called film_etats with type: belongs_to.
I have another model, film_etats, where I have an etat field which is of type: string and another field movies with type: has_many.
This works okay - I get to see my data - but what I would like to do is to filter the information.
In the Locomotive CMS docs I find this:
https://doc.locomotivecms.com/docs/tags#with_scope
Here the first example.
{% with_scope author: 'John Doe' %}
{% for post in content_type.posts %}
{{ post.title }}
{% endfor %}
{% endwith_scope %}
So I did this:
{% with_scope film_etats.etat: 'Production' %}
{% for film in contents.film %}
etat du film : {{film.film_etats.etat}}
{% endfor %}
{% endwith_scope %}
but it does not work and what I want but a type error: undefined method `entries' for nil: NilClass
my model films
title_film => type (string)
film_etats => type belongs_to
my model film_etats
etat => type(string)
films => type(has_many)
thanks

First of all, your code is a little strange, because if it worked, all it would do is print out a list of the word "Production" many times. i.e.
etat du film : Production
etat du film : Production
etat du film : Production
....
The reason it's not working is that you can't use with_scope to filter based on a related model's field. The following code will print the title of all films in production.
{% with_scope etat: 'Production' %}
{% assign production = contents.film_etats.first %}
{% endwith_scope %}
Production Films:<br />
{% for film in production.films %}
- {{ film.title_film }}<br />
{% endfor %}

Related

How to add entity extension in Shopware 6 App?

I want to query the categories of the product in the product listing of a Shopware app, so that I can query the customFields of all categories. Is this even possible with an app?
I have already tried via a navigation-page-loaded. And when I override the box-standard.html.twig and access the product, I can't access the categories there.
For each product i want the categories extensions
As the categories association is not loaded for products in the listing you have to fetch the categories using an app script.
As already noted, add the script to the navigation-page-loaded hook, i.e. in Resources/scripts/navigation-page-loaded/category-loader.twig:
{% set products = [] %}
{% if hook.page.cmsPage.type === 'product_list' %}
{% foreach hook.page.cmsPage.sections as section %}
{% foreach section.blocks as sectionBlock %}
{% if sectionBlock.type !== 'product-listing' %}
{% continue %}
{% endif %}
{% foreach sectionBlock.slots as slot %}
{% if slot.type !== 'product-listing' %}
{% continue %}
{% endif %}
{% foreach slot.data.listing.entities as product %}
{% set products = products|merge([product]) %}
{% endforeach %}
{% endforeach %}
{% endforeach %}
{% endforeach %}
{% endif %}
{% set categoryIds = products|reduce((carry, v) => carry|merge(v.categoryIds), []) %}
{% if categoryIds %}
{% set categories = services.repository.search('category', {'ids': categoryIds}) %}
{% foreach products as product %}
{% do product.addArrayExtension('myCategories', {
'categories': categories.entities.getList(product.categoryIds),
}) %}
{% endforeach %}
{% endif %}
Where we first extract all the products, than load the categories of all products at once, and than assign the categories back to the products.
Note that for reading the category entity you need the correct permission, i.e. in the manifest.xml add:
<permissions>
<read>category</read>
<read>category_translation</read>
</permissions>
Now you should be able to access in the box-standard.html.twig template the categories using product.extensions.myCategories.categories.
If I understand your issue correctly, you are trying to access a product's category's custom fields in some piece of storefront logic (like a Twig template).
The way I see it, you would have to add a script that would enable you to query the repository for the categories and their custom fields. The issue is, a SalesChannelProductEntity will not contain the information about all its categories (only the SeoCategory), so you might need to first query the product_category repository.
Generally, it is going to be complicated but it should be doable.

How to translate Text string inside title include using trans

I need to translate the 'Welcome' title , example:
{% include 'index.html.twig' with {'title': 'Welcome' } %}
But symphony does not let me to translate it using trans, I tried :
{% include 'index.html.twig' with {'title': '{% trans%}Welcome{% endtrans %}' } %}
{% include 'index.html.twig' with {'title': {% trans%}'Welcome'{% endtrans %} } %}
But I did not worked out.
Please, if you know how, let me know.
I solved it doing :
{% include 'index_toolbar.html.twig' with {'title':'Usuaris'|trans} %}
In Wordpress it's as easy as this - however I am assuming that you aren't using Wordpress?
https://timber.github.io/docs/guides/internationalization/
{{ __('Welcome', 'my-text-domain') }}

How can I iterate over the terms in a taxonomy outside the list.html using Zola?

I've found out you can use
{% set posts = get_taxonomy(kind="posts") %}
to retrieve a taxonomy but I'm clueless how to iterate over the terms of the taxonomy in for example single.html of this taxonomy.
I tried things like the following, but I get:
"Tried to iterate using key value on variable 'posts', but it is
missing a key"
{% set posts = get_taxonomy(kind="posts") %}
{% for term in posts %}
<li class="list__item">
<a href="{{ term.permalink }}">
{{ term.name }}
</a>
</li>
{% endfor %}
get_taxonomy returns a struct with keys items & kind. You can debug using:
{% set posts = get_taxonomy(kind="posts") %}
<code>{{ posts.kind | json_encode(pretty=true) }}
{{ posts.items | json_encode(pretty=true) }}</code>
kind seems to have TaxonomyConfig structure and each element in items seems to have TaxonomyTerm structure.

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 %}

Navigation for pages (not for posts) in Jekyll

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

Resources