Podcast #128: We chat with Kent C Dodds about why he loves React and discuss what life was like in the dark days before Git. Listen now.

Questions tagged [forloop]

For loop is a statement executing some code repeatedly (forever or till some condition exists it)

Filter by
Sorted by
Tagged with
10
votes
1answer
4k views

Advanced query/filter based on date and values

I have a channel of events with a field ‘eventDate’ for the date/time of the event. Within that there are ‘types’ of events ‘eventType’. What would be the most efficient way of outputting the NEXT (...
9
votes
2answers
11k views

Is it possible to break out of a loop in twig?

Is it possible to break out of a loop in twig? If it is, how would I do it?
9
votes
1answer
21k views

Use loop index number in a Twig variable

Let's say I have 5 Craft fields named "item1" thru "item5." Instead of writing out each variable... {{ entry.item1 }} {{ entry.item2 }} {{ entry.item3 }} {{ entry.item4 }} {{ entry.item5 }} How can ...
8
votes
4answers
5k views

Remove the last comma in an inline list of tags?

In a list of tags I'd like to remove the last comma. So is there something that works like the Ee backspace parameter?
8
votes
1answer
2k views

How do you filter matrix blocks by type?

Is there a way to filter a matrix by block type, or get a count of how many items there are of a certain type? Can't find a syntax example. I created a matrix with two blocks. In pseudo code, ...
7
votes
2answers
54k views

How do I set a variable in Twig if a value exists more than once in a loop?

How do I loop through entries and set a variable if there are any duplicates in the data I'm checking? In my case, I need to check if there are more than one entry per year. The variable I'm setting ...
7
votes
2answers
669 views

Add ordinal suffix to loop.index

I'd like to add an ordinal suffix to the loop.index variable within a for loop. For example: 1st 2nd 3rd
7
votes
1answer
3k views

Display entries if Field Date is past Now

I'm trying to make a upcoming and past event page. I can make an if statement for displaying the past or upcoming events but the problem is if I limit the amount of entries to be displayed I realised ...
6
votes
1answer
1k views

How do you return a message if no entries are found?

I thought this would be very easy but I'm having a hard time figuring it out. How do you output a warning/message if there are no entries to loop through? {% set entries = craft.entries.section('...
5
votes
2answers
3k views

How to correctly output a Table Field?

I have set up a Matrix field with three blocks, each contains a Table field. Each table has one column - a single line text field. When I call these tables into the template with this code, each row ...
5
votes
1answer
1k views

Group looped entries into blocks and wrap in markup every x number of loops

I am trying to loop through all the entries in a section and "group" them into items which are in blocks of 9 then wrap them with some html markup so there will be the following code: {% set ...
4
votes
2answers
10k views

twig for loop put every 2 elements in a new container

I have this loop: {% for div in myHandleHere %} <div> {{ block.text }} </div> {% endfor %} That actually outputs something like: <div> one </div> <div> two </...
4
votes
2answers
663 views

Fetch Single asset without for loop?

I've got a Assets field which is limited to 1 asset inside a Matrix field. Is there a better way to fetch the image without using a forloop? At the minute I've got: {% for image in block....
4
votes
1answer
1k views

How to create new variable from For Loop results

I need to get a list of all the "sizes" from my Table type field column. The list of sizes needs to be separated by a "|"... eg: small|medium|large My Table type field name is "productAttributes" and ...
4
votes
2answers
1k views

Is it possible to change a loop if mobile browser is detected?

I have a loop to go through my products which go in batches because they need to show detail info every third entry. When the Website is viewed from a mobile device, Tablet or Mobile I want it to loop ...
4
votes
1answer
380 views

How can I iterate more than 100 blocks in a matrix field for loop?

I am looping through a matrix field and it appears to stop looping when the index reaches 100 but I have more than 100 blocks here. How can I loop through the remaining blocks? {% for block in entry....
3
votes
1answer
3k views

How do I loop through entries for a specific entry type?

I would like to loop through entries in a channel but only the ones for a specific entry type. Is this Possible? I'm using the free version of craft on a test site, I have used the channel for my ...
3
votes
2answers
3k views

Outputting Matrix Loop in Template

I have looked at documentation and I still can't figure out how to output the matrix to my template. The Field handle name is: threeHomepageImages The Block type handle name is: images The Fields ...
3
votes
1answer
1k views

Check and remove repeat entries from loop

Is it possible to loop through the list of entries and filter out any that have the same ID as any of the other entries (duplicates)? I have a specific case where this is an issue. I want to keep the ...
3
votes
2answers
732 views

How do you use a for loop to output to JS?

I have a map generated in JS. I need to set the country-id and url for each country. Both 'country-idandurlare fields in my Craft categories groupcountries`. { "country-id": "FR", "url": "...
3
votes
1answer
432 views

How would I list assets from a specific subfolder?

In the case that I have an asset folder structure like this: /images /group-a /group-b /group-c How would I loop through all of the images in /group-b without having to assign them to ...
3
votes
2answers
165 views

Help developing point system for sports league (w/o plugins)

I'm in the middle of a very long project developing a league site for the National Collegiate Wrestling Association in my spare time (once finished, could be a great showcase for Craft). Currently, I'...
2
votes
2answers
5k views

Total sum within loop

I have simple loop: {% for total in thing %} {{ total.seatsKids }} {{ total.seatsParents }} {% endfor %} This results in a string of numbers eg. 1 3 4. I want the sum of all numbers. So how do I ...
2
votes
1answer
130 views

SET versus FIND

As I understand, I have (at least) the following two possibilities to list entries of a channel: Possibility 1: {% for entry in craft.entries.section('example').find() %} Possibility 2: {% set ...
2
votes
2answers
410 views

How Do I Select the second instance of a certain block type

We use a matrix field for the article content on our site. We have different blocks for different content (text, images, embed etc..) and we have optional fields that are outside of the matrix that ...
2
votes
2answers
1k views

Set dynamic class in for Loop

I have this simple element model and I'm trying to add dynamic class within loop. So every time a model outputs, It outputs dynamic class. Can't go for another loop within loop, in case I could, ...
2
votes
2answers
451 views

Using a loop in a loop, how do I check if child loop is empty to not display parent loop?

I'm trying to display a list of names alphabetically ordered. This is working fine, but I also want to check if the name is intoCollection or not (using a Lightswitch). If it is intoCollection, it ...
2
votes
1answer
428 views

Is there a better way than stacked if else

i am using a slider in my template, and depending on if the user filled out values in the craft matrix (slides can have up to 4 rows) i want to have twig compile only the necessary code. I am not sure ...
2
votes
1answer
163 views

calculating average ratings from a series of review fields

I've setup a section for reviews with several fields as numbers for a rating 0-5, I'm trying to figure out how to get the total of all reviews and sum of all reviews to provide a single average for an ...
2
votes
3answers
619 views

Making an entry grid with Batch 2,3 (2 in row then 3 in row)

I'm trying to make a News item grid. I'm looking for a solution to output 2 type of "rows". One that shows 2 entries and the other shows 3 entries. I tried playing around with batches already but can'...
2
votes
1answer
1k views

Sort / Order Entries on select or button click

I have a page on my website were I display alot of entries. Now I want those entries to be sorted by Date, Title etc. I know how to change that hard coded but I want to make it dynamic with a <...
2
votes
1answer
2k views

Why are variables persisting inside of my for loop?

Is anyone able to tell me why variables which I've defined inside of a for loop are persisting and retaining their values on into consecutive iterations? In order to overcome this, I've had to set ...
2
votes
1answer
1k views

How do I create a list of categories and display the entries relating to each of these categories that have at least one entry?

Here's a summary of what I try to do: I created a Single page with a specific template where I want to display a list of partners I created a group of Categories to define all types of partners All ...
2
votes
1answer
123 views

Looping out a lot of entries.

Ok, so I'm looping through a lot of entries. It's a large request, and I had to up php memory limit to 512M. But I'm sure there is a less memory consuming way to do this. Also, I want to cache the ...
2
votes
1answer
175 views

Combing an If Statement with Pagination

I'm trying to pull all the entries in a category that have a multi-selection selected and then paginate it. If I just do the below, it works perfectly fine. {% set entriesRed = craft.entries....
2
votes
2answers
335 views

Reverse count with paginated entries?

I have this loop {% paginate craft.entries.section('sermons').limit('10') as pageInfo, entries %} {% for sermon in entries %} ... {{ loop.revindex }} ... {% ...
2
votes
2answers
115 views

Loop index inside a switch content builder

How would one construct this? I have built a little content builder and wish now to change the layout HTML on the fly when there are more than one matrix block. In the example below, this is just one ...
2
votes
1answer
111 views

For loop with custom order

So I'm trying to loop through some entries and order them by in a specific way. For an example let's say I have the following entires: Example01 Example02 Example03 Example04 I now want to loop ...
2
votes
1answer
284 views

Get an image from a random entry

I have a section with an text field and an image field. the asset field may sometimes be left blank. in this case, in my template, i want to grab the image from another random entry. This is the code ...
2
votes
1answer
219 views

Get future multi-day event entries with custom date field

I have an event section with custom fields for eventDate and eventEndDate. I'm listing upcoming events in my template using: {% for event in craft.entries.section('events').eventDate('>='~now)....
2
votes
1answer
427 views

How to get first child URL within Craft/Twig Nav tags

I have a navigation structure as follows: {% set page = entry ?? category ?? null %} <ul class="nav menu--main"> {% nav entry in entries %} <li class="menu__item{% if page and ...
2
votes
1answer
584 views

Nesting level too deep when looping through matrix blocks

I'm running into a nesting level error when looping through matrix blocks, and I can't quite figure out what the problem is. The problem arises when I use a variable to track the type of the previous ...
2
votes
1answer
199 views

Load entries based on category field value while multiple categories are selected

I have an activity calendar (more like a weekly schedule) which filters per day and per category. Because there are so many activities I can't just load them all in at once so I use some sort of Ajax ...
2
votes
1answer
1k views

Limit the for loop returns with an if statement

I've got a for loop working perfect with a limit of 5. Then I throw in the if statement to filter those entries that correspond with a global variable I have set. The problem is the if statement is ...
2
votes
1answer
33 views

I want to display entries that have a field that is the same as a field in a global variable

There is a publishMonth (multiselect) and publishYear (radio buttons) field in each article. They do not correspond to an actual date. Each article is is associated with one year, or one or more ...
2
votes
1answer
235 views

Next / Prev and looping/cycling entries instead of reaching the end of the selected results

Is there a way to be able to use entry.getNext(params) to return back to the first entry when you're viewing the last entry in the parameters? With the result of looping/cycling through all entries ...
1
vote
2answers
951 views

Only display entries based on a field conditional

Hey I am trying to get 4 entries, but only want 4 that have a body length > 100. How would I accomplish this? {% for entry in craft.entries.section('artists').order('RAND()').limit(4) %} // only ...
1
vote
2answers
269 views

Loop based on date (with math)

I would like to loop through entries only if the current date/time is before 5am on the following day from when the entry's eventDate (custom field mind you)is. In other words I may have an event at ...
1
vote
2answers
998 views

Get last row in table field for loop

How can I determine if the row being returned from a table field in a for loop is the last row?
1
vote
1answer
298 views

Image(r) transform and for loop

I'm stuck with the .first() element and why the for-loop code won't work. <div id="header-slider"> <ul> {% set image = entry.bannerSlider.first() %} {% set ...