Twig one to many relations - twig

I am new to Twig. I thought I had mastered the basics but am completely stuck on this.
I have two arrays:
books => [
[
'book_id' => ...,
'book_title' => ...,
],
],
and
tags => [
[
'tag_id' => ...,
'book_id' => ...,
'tag' => ...,
],
],
How do I get Twig to produce something along these lines
Book title 1 tag1 tag2 tag3 tag5
Book title 2 tag1 tag3 tag4 tag2
and so on
So far I have
{% for book in books %}
{{book.title}}
{% for tag in tags %}
what goes here to get a list of tags for this book
{%endfor%}
{% endfor %}
I know it is only a few lines of code but I don’t know where to start
A hint in the right direction would be really appreciated.

So you know the book_id. You should be able to use this to limit your FOR loop over the Tags:
{% for book in books %}
{{book.title}}
{% for tag in tags if book.id == tag.id %}
{{ tag.tag }}
{%endfor%}
{% endfor %}
See: http://twig.sensiolabs.org/doc/tags/for.html#adding-a-condition

Related

How to get Data from Many-2-Many-Relation an ObjectBricks

When creating templates for document output, I would like to access the fields of ObjectBricks or many2many relations. With the relations, I can only access the path of the relation via Twig, but not the content of the object.
With ObjectBricks I can't get any further via Twig. I understood that all objects that are addressed in a class are loaded.
Or do I have to create extra snippets or views for each object, which are then included in the template? This then again in PHP and not in Twig?
Example :
a product has a m2m relation to a class "Material" , which contains the field "Description".
{% set matdate = product.Material %}
{% if matdate %}
{% for element in matdat %}
{{ element }}
so far it works, but
{% if(item) %}
{% for dat in element %}
{{dat}}
{% endfor %}
{% endif %}
When I then address the array, I get no content and no error.
Unfortunately, the descriptions from the Pimcore documentation don't help me with the ObjectBricks either. I can't figure out how to query them.
Maybe it's just a problem of understanding that is clear to everyone, but I can't find an answer.
Here is the dump:
Pimcore\Model\DataObject\Data\ElementMetadata {#4103 ▼
#elementType: "object"
#elementId: "88"
#fieldname: "Material"
#columns: array:1 [▶]
#data: []
#dao: Pimcore\Model\DataObject\Data\ElementMetadata\Dao {#4078 ▶}
#_owner: Pimcore\Model\DataObject\Implantat {#4079 ▶}
#_fieldname: "Material"
#_language: null
elementType: "object"
elementId: "88"
fieldname: "Material"
columns: array:1 [▶]
data: []
_owner: Pimcore\Model\DataObject\Implantat {#4079 ▶}
_fieldname: "Material"
_language: null

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.

loop through key values of an array in twig

I have not been able to find my answer elsewhere (maybe because I didn't know how to ask google as I'm pretty new to this ;))
I'm working with symfony and twig.
I pass an array in my view with only one entry related to the id. It looks like this in my view
array:2 [▼
"sponsor" => Sponsor {#473 ▼
-id: 5
-sponsorCode: "FUT"
-name: "MANULO"
-city: "OLERDOLA"
-zipCode: 0
-address: ""
-country: "ESPANA"
-phoneNumber: 32767
-email: ""
-creationDate: DateTime {#470 ▶}
}
"app" => AppVariable {#476 ▶}
]
I know I can access each property by doing
{{sponsor.name}}
But I'm trying to do it through a loop for each field of this array
something like
{% for key, value in sponsor %}
<div class="field-group">
<div class="field">{{ key }}:</div>
<div class="value">{{ value }}</div>
</div>
{% endfor %}
Am I missing something?
Thank you very much
From the TWIG documentation:
Keys Only
By default, a loop iterates over the values of the sequence. You can iterate on keys by using the keys filter:
<h1>Members</h1>
<ul>
{% for key in users|keys %}
<li>{{ key }}</li>
{% endfor %}
</ul>
Keys and Values
You can also access both keys and values:
<h1>Members</h1> <ul>
{% for key, user in users %}
<li>{{ key }}: {{ user.username|e }}</li>
{% endfor %} </ul>
https://twig.sensiolabs.org/doc/2.x/tags/for.html
Keep your eye on the TWIG documentation, its rather comprehensive.
Looking at your code, it looks ok. However, the issue could be that the {{value}} may need further identification, such as {{ value.id }}

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

Swig (node.js) Include a File and pass an object

I am facing this problem.
I've got a page with an include of another like this:
index.html
{{ set pets = { pets : petsObject } }}
{{ include pets.html }}
petsObject is an object like this
petsObjects: [
{ name : "cat" },
{ name : "dog" }
]
When I try to render the page I get a blank page with only this:
[object Object]
I have no clue about what is going on :(
Thanks in advance!
Seems you'll need to use:
{% include pets.html with pets %}
According to docs for include:
Locally declared context variables are not passed to the included template by default.
It is also recommended for performance to use the only keyword after the included terms, like this:
{% include pets.html with pets only %}
Beyond that, it depends on the contents of pets.html, which you haven't included here. But, make sure that you're attempting to output the name:
{% for pet in pets %}
{{ pet.name }}
{% endfor %}
Or use a filter like json_encode() to format it:
{% for pet in pets %}
{{ pet|json_encode }}
{% endfor %}
Trying to output the Objects themselves will simply produce [object Object]:
new Object().toString() === "[object Object]"

Resources