13
votes
Accepted
Total sum within loop
To do math in twig, you will need to use the set tag to do assignments, and the + operator to do addition. When doing math inside a for loop, you will need to keep in mind that
loops are scoped in ...
11
votes
Accepted
Display entries if Field Date is past Now
You can combine a for and if statement in twig, so you don't necessarily need to use search.
Something like this might get you a bit closer:
{% for entry in craft.entries.section('event') if entry....
6
votes
Accepted
Check and remove repeat entries from loop
I'd probably try to filter those out in your initial query, but if you've already got them, something like this should work:
{% set existingIds = [] %}
{% for event in events %}
{% if event.id ...
6
votes
Accepted
Load More Entries Button
The Sprig plugin will save you a lot of trouble, and dynamically loading more entries is a perfect use-case for it. If you prefer to use jQuery, then by all means do so, but keep in mind that it is ...
5
votes
Accepted
Is there a better way than stacked if else
There is certainly a lot of repeated code here so we can definitely improve on that! There are so many ways you could handle your Twig templates but this is how I would do it:
<div id="slider-with-...
5
votes
Set dynamic class in for Loop
I can't comment yet to reply directly to Mats Mikkel Rummelhoff's comment above, but just in case someone comes across this and is looking to alternate styles instead of basing it on the loop.index ...
4
votes
Accepted
Create a new row every 3 items in a matrix
To my understanding this should work for a matrix field (called imageLinkBoxes) that contains only one type of block (imageLinkBox) that has 3 fields (imageLinkBox.linkText, imageLinkBox.linkUrl and ...
4
votes
Accepted
Only display entries based on a field conditional
I highly recommend to use the fantastic Preparse plugin to save to length of your body text in a hidden field.
Create and add a new "bodyLength" Preparse field to your "Artists" entry type. Use this ...
4
votes
Accepted
Reverse count with paginated entries?
You already pass the array keys into the loop as a variable pageInfo. There's a couple of properties you can access from it, see the Craft docs:
Templating Reference → {% paginate %}
Of interest for ...
4
votes
Accepted
Looping out a lot of entries.
There isn't really a good way to extract large chunks of Craft entries using the core features in Twig. Every entry you fetch is a EntryModel and those take up some memory due to relations and other ...
4
votes
Accepted
For loop does not display all entries in section
Craft has a default limit of a 100 elements returned via an ElementCriteriaModel. To pull all elements, you'll need to explicitly set limit to null:
{% for post in craft.entries.section('posts')....
4
votes
Accepted
Display all entry titles on one line during FOR loop
This is because you're using an html list <li> element, which by default puts each element on a new line. You could style away the newline I suppose, but why use it at all...there are a bunch ...
4
votes
Accepted
Image(r) transform and for loop
When using the for loop you don't need to set image as it is set in the for loop. You will also need to define transformedImage on every iteration, so move it within the for loop.
Give the below a go,...
4
votes
Accepted
Next block inside of a loop
Try doing something like:
{% set nextBlock = block.getNext() %}
https://craftcms.com/docs/templating/matrixblockmodel#getNext
4
votes
Load More Entries Button
Here's a cut-down version of what I'm using on https://cld.agency/showcase
The quick example below uses jQuery, but I have a more complete updated version using vanilla JS here: https://github.com/cld-...
3
votes
Display entries if Field Date is past Now
Craft has changed since the above answer was given. The now object must be a part of the string, rather than concatenated as an object.
so instead of:
{% set futureEntries = craft.entries.section('...
3
votes
Accepted
How do I make my variable available inside an assets loop?
Your problem is a scoping issue. You've defined theUrl inside the scope of a for loop, so it only ever lives inside of that loop.
You can do this to have it exist outside of the loop, too.
{% set ...
3
votes
Accepted
Combing an If Statement with Pagination
To get the pagination to work as expected, you need to incorporate the if into your craft.entries query. There may be a more elegant solution to do this, but looking at the database, the selected ...
3
votes
Accepted
Streamlining a loop with a bunch of conditions
I'm not sure if this is better or not but one thing I'd do here is to set some defaults in my include. This also means you don't need to worry about checking whether it's defined or not. You're just ...
3
votes
Accepted
Filtering entries by multiple categories with multiple filters
Passing the category as a comma delineated string and using craft.request.getParam('category') the second time will not grab the second variable 4rooms. First you need to parse that string into an ...
3
votes
Accepted
Correct way to merge craft.user field arrays and use outside the loop?
It looks like you have a typo in your merge, and the structure of the merge is incorrect.
{% set merged = specialisms|merge(specialisms) %}
This is saying set merged to the array specialisms merged ...
3
votes
Only display entries based on a field conditional
You could do something like this:
{# Grab all entries with a body > 100 %}
{% for entry in craft.entries.section('artists').order('RAND()').find() if entry.body|length > 100 %}
{# Limit output ...
3
votes
Get future multi-day event entries with custom date field
I think the simplest solution would be to make the eventEndDate field required even for single day events. Then you could do something like this:
{% for event in craft.entries({
section:'events',
...
3
votes
Sort entry titles alphabetically excluding 'The'?
According to SUBSTRING, the first character starts at 1. So if you wanted to start at the position immediate after The (with one space), you'd need to start at position 5.
Just did a test locally ...
3
votes
Accepted
loop.index is not returning correct number inside nested loop with inline if conditional
I'm guessing you want to display the loop index of the outer for loop.
To do that, Twig provides a loop.parent context.
{% for order in orders %}
{% for refund in order.transactions if refund....
3
votes
Accepted
Add conditional in a for loop
{% for type in entry.buildType %}
{{ type }}
{% if not loop.last %}/{% endif %}
{% endfor %}
See: https://twig.symfony.com/doc/2.x/tags/for.html#the-loop-variable
3
votes
Alternating Forloop items across Content Blocks
In the second inner loop, you need to add the number of items in the first panel to the current index to get a consistent count.
Might be an off-by-one-error in the following example, make sure to ...
2
votes
Can't get checkboxfield values to show
If you want a comma-separated list of selected options, try this.
{% for option in offer.offerMinimumPrice.options|filter(v => v.selected) %}
{{ option.label }}{% if loop.last==false %}, {% ...
2
votes
Accepted
Can't get checkboxfield values to show
The problem was that I used this code within an entry, so I had to use the tag of that entry (offer) instead of entry.
The code that works now is:
{% for option in offer.offerMinimumPrice.options %}...
2
votes
Total sum within loop
You could use some twig math, found at the bottom of the page here:
Twig Templating
Not sure without seeing the rest of your code, but you could do something like this:
{% set totals = [3,4]%}
{% ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
forloop × 131templating × 47
loop × 21
matrix × 17
entries × 15
conditional × 9
craft3 × 7
assets × 7
categories × 6
datetime × 6
variable × 6
entry × 5
field × 5
filter × 5
limit × 5
search × 4
tags × 3
javascript × 3
pagination × 3
array × 3
plugin-supertable × 3
table-field × 3
group × 3
math × 3
relations × 2