I want to show my string value into my array 'nameComments' with key {{loop.index}} of my comments array, but {{ nameComments[{{ loop.index }}] }} show an error
{% for com in comments %}
<p>Comment {{ nameComments[{{ loop.index }}] }} : "{{ com['comment'] }}"</p>
{% endfor %}
If I try:
{% for com in comments %}
<p>Comment {{ nameComments[1] }} : "{{ com['comment'] }}"</p>
{% endfor %}
And the {{ loop.index }} show me value : 1
So how can I implement my loop index into my array?
{% for com in comments %}
<p>Comment {{ nameComments[ loop.index ] }} : "{{ com['comment'] }}"</p>
{% endfor %}
Just leave out the curly brackets. This should work fine.
By the way loop.index is 1 indexed. If you loop through an array which normally starts with index 0 you should consider using loop.index0
See the documentation
It is safer to iterate over the real value of the array index and not using loop.index and loop.index0 in case where array indexes do not start in 1 or 0 or do not follow a sequence, or they are not integers.
To do so, just try this:
{% for key,com in comments %}
<p>Comment {{ nameComments[key] }} : "{{ com['comment'] }}"</p>
{% endfor %}
See the documentation
Related
I'm trying to loop a matrix field that has one block containing 3 items.
{% for block in entry.galeria.type('itemsGaleria') %}
{% if block.titulo|length %}
{{ block.titulo.first }}
{% endif %}
{% endfor %}
But craft always throws the error variable entry does not exist.
I read the matrix section from craft 3 docs but cannot fix this problem.
Any clues?
Well, as there where no sugestions, and i never give up, i figured out for myself :)
Here it is:
{% set entries = craft.entries.section("galeria").all() %}
{% for entry in entries %}
{% for block in entry.galeriaMatrix.all() %}
{% switch block.type %}
{% case "itemsGaleria" %}
{% for image in block.fotografia %}
<img src="{{image.url}}" alt="{{image.title}}" />
{% endfor %}
{{ block.titulo }}
{{ block.texto }}
{% default %}
{% endswitch %}
{% endfor %}
{% endfor %}
It does what i need, which is loop all the entrances in the matrix block field.
I wanna array my collection in 3 cols, for 3 different statuses. And if there isn't any gig/item with that status, it has to say ' No projects'
I have tried this:
<div class="col-sm">
<h2>Up next</h2>
{% assign next = site.gigs | gig.status == 'Next' | sort: gig.date %}
{% if next.gigs.size == 0 %}
No projects
{% else %}
{% for gig in next %}
{{ gig.title }}
{% endfor %}
{% endif %}
</div>
<div class="col-sm">
<h2>Working on</h2>
{% assign on = site.gigs | gig.status == 'On' | sort: gig.date %}
{% if on.gigs.size == 0 %}
No projects
{% else %}
{% for gig in on %}
{{ gig.title }}
{% endfor %}
{% endif %}
</div>
<div class="col-sm">
<h2>Done</h2>
{% assign done = site.gigs | gig.status == 'Done' | sort: gig.date %}
{% if done.gigs.size == 0 %}
No projects
{% else %}
{% for gig in done %}
{{ gig.title }}
{% endfor %}
{% endif %}
</div>
But it just arrays all of the gigs/items :(
Maybe it can be done in a way more simple way.
I don't know if you could make one compact liquid code and array 3 columns by counting the number of different statuses.
Help!
Ordering
Our statuses are "Next", "On" and "Done". They must be ordered in such order.
As this is not an alphabetical sort order, we need to define this order by ourself.
In _config.yml :
status-order:
-
name: "Next" ### status as it is set on gigs (!!! Case-Sensitive !!!)
display: "Up Next" ### status column header to display
-
name: "On"
display: "Working On"
-
name: "Done"
display: "Done"
We can now loop over site.status-order and get our gigs in desired status order.
{% for status in site.status-order %}
{{ status.name }} - {{ status.display }}
{% endfor %}
Presenting
As your current code is a little repetitive, we can factor it like this :
{% for status in site.status-order %}
{% assign items = site.gigs | where: 'status', status.name | sort: date %}
<div class="col-sm">
<h2>{{ status.display }} ({{ items.size }})</h2>
{% if items.size > 0 %}
<ul>
{% for item in items %}
<li>{{ item.title }}</li>
{% endfor %}
</ul>
{% else %}
No project
{% endif %}
</div>
{% endfor %}
Note
You must be sure to set status with the right case (eg : "Next" an not "next").
And the right type. If you set status: On it is understood as the true boolean value, not the string "on". In this case the correct status expression is status: "On". It must be quoted or double quoted to be understood as "On" string.
Any item with incorrectly cased or typed status expression will not appear in our listing.
I am accessing dynamic data from an array:
{% for key, value in columns_arr %}
{% for k,v in group %}
var {{ value }} = "{{ attribute(v, value) }}";
{% endfor %}
{% endfor %}
This is working well for name and id (see below). In the example of name the attribute ...
{{ attribute(v, value) }}
is replacing:
{{ v.name }}
and in the example of id it is replacing ...
{{ v.id }}
But this is not working with type because here I actually need to replace:
{{ v.type.name }}
So my question is, how would this look like in the attribute function?
I tried {{ attribute(v.name, value) }} but I get the error
Impossible to access an attribute ("type") on a string variable
("ID").
group:
array:4 [▼
0 => Fields {#7444 ▼
-id: 1
-name: "ID"
-unique_id: "6ab8c870ed"
-productgroup: PersistentCollection {#7448 ▶}
-type: Type {#7525 ▼
+__isInitialized__: true
-id: 2
-name: "hidden"
-unique_id: "5e1086c862"
-label: "hidden"
…2
}
}
1 => Fields {#7526 ▶}
2 => Fields {#7530 ▶}
3 => Fields {#7534 ▶}
]
columns_arr:
array:3 [▼
0 => "id"
1 => "name"
2 => "type"
]
My approach according to this question:
How to check a multidimensional Twig array for values?
{% for key, value in columns_arr %}
{% for k,v in group %}
{% for k1,v1 in v %}
var {{ value }} = "{{ attribute(name, v1) }}";
{% endfor %}
{% endfor %}
{% endfor %}
But this gives me an error, My page is not loading anymore.
Another approach is this:
{{ attribute(v, [value.name]) }}
But I get the error:
Impossible to access an attribute ("name") on a string variable
("type").
If you are able to change the column array to something like 2 => 'type.name', you can use the following snippet to read out nested data:
{% for value in data %}
{% for column in columns %}
{% set output = value %}
{% for method in column|split('.') if method != '' %}
{% set output = attribute(output, method) | default('') %}
{% endfor %}
{{ output }}
{% endfor %}
{% endfor %}
demo
I have the following code
<div>
{% for note in site.regnotes %}
{% if note.regulationno == page.regulationno %}
<p>
{{ note.regulationno }} - {{ note.url }}
</p>
{% endif %}
{% endfor %}
</div>
This code loops over the regnotes collection in a jekyll site, checks if the current note regulationno is the same as the page regulationno and if so displays the regulationno and url - that is the url of the current page. How do I change this code to include the url of the previous page, the current page and the next page. I'm looking for three urls - previous, current and next? - The "page.previous.url" variable within jekyll does not appear to work in collections.
This is what it might look like in other code
for i=1 to number of items in the regnotes collection
if current note == page note
print page[i].url //current page url
print page[i-1].url //previous page url
print page[i+1].url //next page url
end if
end for
I suppose what I'm trying todo is reference the items in the collection by their array index. just can't seem to get the syntax correct.
Since you are a programmer, you just need to know that you need to use forloop.index0 to know where you are in the for loop (https://docs.shopify.com/themes/liquid-documentation/objects/for-loops#index0).
The code will be something like:
<div>
{% for note in site.regnotes %}
{% assign current_index = forloop.index0 }}
{% assign next_index = current_index | plus: 1 %}
{% assign prev_index = current_index | minus: 1 %}
{% if note.regulationno == page.regulationno %}
<p>
{{ note.regulationno }} - {{ note.url }}
</p>
{% if site.regnotes[prev_index] %}prev{% endif %}
{% if site.regnotes[next_index] %}next{% endif %}
{% endif %}
{% endfor %}
</div>
I want to list all my posts within site.categories.projects as a comma separated sentence. There's documentation for displaying {{ site.tags }} as array_to_sentence_string but how can I use the filter with a for loop?
# empty array
{% assign postsTitlesArray = '' | split:':' %}
# pushing categorie posts title in our array
{% for post in site.categories.one %}
{% assign postsTitlesArray = postsTitlesArray | push: post.title %}
{% endfor %}
{{ postsTitlesArray | array_to_sentence_string }}