2022 Developer Survey is open! Take survey.

Hot answers tagged

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 ...
user avatar
10 votes
Accepted

Need to access year/month variable when grouping entries by date

If I understand your example correctly, a simple hyphen would do the trick — "YYYY-MM" is a format the date filter recognizes. {% set entries = ... %} {% set entriesByMonth = entries|group('...
user avatar
  • 3,390
6 votes
Accepted

How can I group entries based on a dateTime field?

You're currently grouping on the myDate field, which essentially creates a new array index for each date (e.g. 2016-01-01) which is why you're getting your current output. To group by year, this ...
user avatar
6 votes
Accepted

group Matrix blocks and also order by Matrix field for a given block

This is a great chance to use the group filter. First, get all your matrix blocks as an array: {% set talkBlocks = entry.talks.find() %} Then group them by type: {% set talksByCategories = ...
user avatar
6 votes
Accepted

How can I group a list of tags into ranges of first letters (A–E, F–J, etc.)?

You can do this with the twigPCRE filter. Once you have your allTags, group them like this: {% set allTagsGrouped = allTags|group('name|first|upper|preg_replace("/[A-M]/", "A-M", 1)|preg_replace("/[N-...
user avatar
6 votes
Accepted

Is it possible to group entries by checkbox?

What Brad says, plus an example of grouping by selected checkbox: {% set entries = craft.entries.section('my_entries') %} {# get all checkbox options #} {% set options = entries.first().checkbox....
user avatar
  • 6,288
5 votes
Accepted

Order grouped entries

The probably most easy way to do this is to sort the grouped array by key, which unfortunatelly is only possible with a plugin, as Twig's sort filter only allows to sort by value. Here I'm using ...
user avatar
  • 35.8k
5 votes
Accepted

Group by Field in Matrix Field

You should be able to do this with Craft's handy group filter. {% set players = entry.myPlayersMatrix %} {% set playersGrouped = players|group('myPositionDropdown.label') %} {% for position, ...
user avatar
  • 35.8k
5 votes
Accepted

List Categories by 0-9 and A-Z

To group by the first letter you would simply use the slice filter within Craft's group filter. For all non letter characters you can now use Craft's replace filter (it does regex since Craft 2.2) ...
user avatar
  • 35.8k
5 votes
Accepted

Group entries by section

You can use the Twig group filter. {% set entries = allowedSearchableEntries.search(query).order('score').limit(100) %} {% set entriesBySection = entries|group('section') %} {% for section, ...
user avatar
5 votes
Accepted

Grouping entries by category - most efficient approach

One improvement I think could be made is to fetch only the ID's of your hotTubs entries at the start of your code. You're only using this to get the related categories and fetching ID's is faster than ...
user avatar
  • 1,456
4 votes
Accepted

Output entries sorted by category title and paginate

Not exactly what you're looking for, but it gets you closer. This should display the order you want, but it doesn't have any pagination (and honestly I'm not sure if that's possible using Craft's {% ...
user avatar
  • 66.4k
4 votes
Accepted

Entries grouped by Category and then Sub Category

You can get the categories to display hierarchically pretty efficiently using the {% nav %} tag: {% set categories = craft.categories.group('myGroup').find() %} <ul> {% nav category in ...
user avatar
4 votes
Accepted

Outputting Matrix blocks with grouped values based on Entries field id

Try something like: {% set breadGroups = entry.breads | group('bread.first.id') %} {% for breadId, blocks in breadGroups %} {% set sum = 0 %} {% for block in blocks %} {% set sum =...
user avatar
  • 3,390
4 votes

Need to access year/month variable when grouping entries by date

The year and month variables are not defined, so you'd first have to get these from the 'date' variable. You could do this by using Twig's split function and splitting date by the space in between: ...
user avatar
  • 6,288
4 votes
Accepted

Paginate Entries by Day

Instead of using pagination, you might try creating a custom route using tags for year/month/day. Those tags will be available as variables in the template that you can use to load that days entries ...
user avatar
4 votes
Accepted

How do I list the tag groups that are actually in use?

You could use the group filter to group your tags by tag groups: {% set entries = craft.entries.section('gallery').limit(null) %} <ul> {# Get all the tags related to our gallery entries ...
user avatar
  • 35.8k
4 votes

How Do I group entries by Category and retain fixed Category Order?

Depending on your needs, you may be able to ditch the group() filter and sort your list however you need. Get all your entries and categories. Then use the relatedTo functions to filter those lists. ...
user avatar
  • 2,563
4 votes
Accepted

Pagination / Custom Routes (Based on Entry Existing)

For generating the nav, this should work. {% set allEntriesByWeek = allEntries|group("postDate|date('Y-W')") %} {% for week, entry in allEntriesByWeek %} <li><a href="events/{{ week|...
user avatar
4 votes
Accepted

Group by day does strange exeptions

When you specifically group on dateField.day (in your case, the |group('datum.day') part), the group filter won't care about the month or year for the date field – only the day. This means that any ...
user avatar
3 votes
Accepted

Loop through category groups

I'm not seeing any obvious ways to access groups directly in Twig. There may be a more direct way, idk — but this should work. {% set categoryGroupNames = [] %} {% for category in craft.categories %} ...
user avatar
3 votes
Accepted

How Do I group entries by Category and retain fixed Category Order?

Twig is a templating language, and in my opinion this is pushing what it's comfortably meant to do. You might want to consider handling the heavy lifting w/ PHP by writing a small business logic ...
user avatar
3 votes
Accepted

Passing a variable to an include

You set the variable that your are passing in to your include to a string. And in your include you try to access it like an object. The variable which you’re passing in to your include is set to a ...
user avatar
  • 35.8k
3 votes
Accepted

Sort entries in a category via entry field

You are getting this error because the "entries" fields are always considered to be a query (ElementCriteriaModel) that can return more than one element. Therefore, if you know for sure that you will ...
user avatar
  • 496
2 votes
Accepted

Paginate Grouped Entries

That error is occurring at entries.limit(10), because at that point entries is already an array of fetched entries, rather than an ElementCriteriaModel. (The entries will get fetched as soon as the ...
user avatar
2 votes

Order grouped entries

Stumbled upon this looking to answer similar problem. You should be able to do this now without a plugin, using the multisort filter where you iterate through your profiles, e.g.: {% for profile in ...
user avatar
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]%} {% ...
user avatar
  • 1,517
2 votes
Accepted

Better way to group by category and include full category entry

Your actual code is pretty effective – the problem is that it's potentially going to result in a lot of queries. This is a good example of the so-called N+1 query problem, where each iteration over ...
user avatar
2 votes

Group entries by section, then order and combine sections

You could do this in a few steps (warning: this is untested code): 1) Define an array of the sections you cited, in the correct order: {# Define a list of the sections you want to display in the ...
user avatar
  • 496
2 votes
Accepted

Grouping a large number of entries

First, based on your example code I don't think you actually need to loop on the entries – you could simply group them on the brand name, and then loop on the keys for the grouped array (the keys ...
user avatar

Only top scored, non community-wiki answers of a minimum length are eligible