What's the deal with Deno? We talk with a major contributor to find out. Listen now.

Hot answers tagged

15

You could first set a variable to test against and then change it the first time the block type appears. {% set firstTime = true %} {% for block in entry.matrixField %} {% if block.type == "text" and firstTime == true %} ...Do something once... {% set firstTime = false %} {% endif %} {% endfor %}


14

While your conditional is complete personally preference I would do the following as I find it slightly easier to read. Note the use of the {%- if -%} to trim whitespace: {% for category in entry.mediaType %} <b>{{ category.title }}</b> {%- if not loop.last -%} , {% endif %} {% endfor %}


14

There is a special tag inside for loops you can use for this {% for category in entry.mediaType %} <b>{{ category.title }}</b>, {% if loop.last %} <b>{{ category.title }}</b> {% endif %} {% endfor %} Full list here


14

{% set entries = craft.entries.section('pages').type('blogPost') %} {% for entry in entries %} {{ entry.title }} {% endfor %} Is what I have cobbled together... any pointers on improvements would be welcome.


8

I found the answer in the Twig documentation: http://twig.sensiolabs.org/doc/templates.html#variables You can use a dot (.) to access attributes of a variable (methods or properties of a PHP object, or items of a PHP array), or the so-called "subscript" syntax ([]): {{ foo.bar }} {{ foo['bar'] }} When using the subscript syntax, you can concatenate the ...


7

for is part of the native Twig language. for is for general usage... It's the basic, general looping structure that you'll use 99% of the time. You'll find that it closely mirrors for and foreach in other programming languages. nav is defined by Craft, and doesn't exist natively in Twig. nav is special... You'll only need it to loop through entries in a ...


7

You can use a custom counter instead of using the loop.index value. {% set step = 0 %} {% for block in entry.matrixBlocks %} {% if block.type == 'stepBlockType' %} {% set step = step + 1 %} Current step: {{ step }} {% elseif block.type == 'otherBlockType' %} {# Don't increase step count #} {% endif %} {% endfor %}


6

Here's another good example for working with categories that may be useful to you. This will loop through all categories (that have entries) and show their entries: {% set entries = craft.entries.section('someSection').limit(null) %} {% for category in craft.categories.group('someCategoryGroup').relatedTo(entries) %} <h2>{{ category }}</h2> ...


6

This example assumes you have your Category Group setup to use custom URLs. In the template used to display your categories, the category variable will be available and represent your CategoryModel for that page. The key is to pass the CategoryModel (in our example below category) to the relatedTo parameter of the Entries tag. {{ category.title }} {% ...


6

As I already wrote in my comment 3 days ago something like this should work {% paginate craft.entries.section('mySection').limit(10) as pageInfo, pageEntries %} {% for page, url in pageInfo.getPrevUrls(3) %} <a href="{{ url }}">{{ page }}</a> {% endfor %} <span class="current">{{ pageInfo.currentPage }}</span> {% for page, url ...


5

In order to display all other entries by an author except the current one, you will need to perform another step — either by filtering out the current entry from the query or adding a conditional. {% set authorId = entry.author.id %} {% set allEntryIds = craft.entries.section('blog').authorId(authorId).limit(null).ids() %} {% set otherEntryIds = allEntryIds|...


5

That’s definitely the way to do it! I would set the URL segments to their own variables, to clean it up a little bit. {% set entrySlug = craft.request.getSegment(2) %} {% set imageOffset = craft.request.getSegment(4) - 1 %} Also, probably a good idea to include the section param when fetching the entry, in case you have another entry in another section ...


5

Update Sorry, I originally thought you were building nav and just showing simplified code. If you just want to list the categories then this should work: {% nav category in categories %} {{- category.title -}} {% if not loop.last %}, {% endif %} {% children %} {% endnav %} If your trying to build a nav system, then this should work. In either ...


5

Forgive me if I'm wrong... by using loop.index0 is divisible by(2) are you trying to determine if the instance is odd or even? 1st - returns false 2nd - returns true 3rd - returns false 4th - returns true If so Twig does support checking if the loop index is even or odd. Perhaps that would be better to use? {% if loop.index is even %}Even{% endif %} EDIT ...


5

As you'll only ever be working with one entry I wouldn't bother with the for loop for the first part of your question. Just set a variable with your featured news article. Something like this (not tested though): {% set featuredArticle = craft.entries.section('news').first() %} <div class="container" id="featured-news"> <div class=col-md-6> ...


4

This is totally normal and expected behavior. In fact, it's critical for some looping situations, and pretty standard across programming languages. Wouldn't you expect to find this same behavior in PHP? Think about situations where previous loops build on each other. Here's an example of a situation where loop values need to be retained... {% set totalPets ...


4

{% for category in entry.mediaType %} <b>{{ category.title }}</b>{{ "," if not loop.last }} {% endfor %}


4

You'd further filter your returned artist entries down like this: {% set artists = craft.entries.section('artists').intoCollection('1') %} Then remove all the unnecessary "intoCollection" conditionals in your loop.


4

What you want is not the loop variable of the inner loop around the image, but the loop variable of the outer loop. To get to the outer loop, use loop.parent. Try this: {% if loop.parent.loop.index0 is divisible by(2) %}last{% endif %}


4

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 of ways of doing this, but this is one simple one: {% set developers = entry.GamesDeveloper %} {% if developers|length %} Developer: {% for developer in ...


4

You can select elements by class and give your html elements custom data attributes. When this is your clickable html element with the corresponding div <button data-id="dimsTarget4" class="myButton"> <div class="myContainer" id="dimsTarget4"></div> Your jquery could be $('.myButton').click(function(){ var containerId = $(this).data(...


3

element.type is not a simple string (add {{dump(element.type)}} to your loop to see this). What you should use is element.type.handle. So the assignment is: {% set prevElementType = element.type.handle %} And the comparison (now of two strings, so no recursion) is: element.type.handle == prevElementType


3

Your dump from outside the loop is being dumped BEFORE the variable is set within the loop. Move it to after the loop and try it again.


3

I'm not sure what {% for image in craft.assets.kind('image') %} is all about in your code since the image variable isn't being used anywhere. But here's the basic structure on how you could loop through 3 separate Asset fields in 3 different single sections inside of a Matrix block: {% for block in entry.homeMatrix %} {% if block.type == "homeMatrix" %}...


3

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 %} {% set groupName = category.getGroup.name %} {% if groupName not in categoryGroupNames %} {% set categoryGroupNames = categoryGroupNames|merge([...


3

Without understanding your use case or having the context of your full template and/or Matrix loop, it's very difficult to provide a good answer. If you only want to "wrap all outputted blocks" in a container, it's as easy as putting the Matrix loop in a container, e.g.: <div class="wrapper"> {% for block in entry.myMatrixFieldHandle %} {%...


3

You need to define the block previous/next variables outside of your for loop. Also you should check for the last element in the loop. You can find a great working example on the happy lager demo website (https://github.com/pixelandtonic/HappyLager). The relevant code part is here: https://github.com/pixelandtonic/HappyLager/blob/master/craft/templates/...


3

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 choices is saved as an array-like string: ["blue","red"] This means that you can search for the value in the field like this: craft.entries.section('eventRentals'...


3

product.gallery returns an ElementCriteriaModel, which you can think of as a list of elements. product.gallery.first() will return the first element in this list. What you want is to loop over all the elements: {% for asset in product.gallery %} <img src="{{ asset.url }}"> {% endfor %} (Note that this will work if there are no elements in the list ...


3

I still haven't found a succinct way to exclude a particular address by ID within the commerce customer addresses tags, but I did come up with the following as a way to get a nice clean set of addresses I want to iterate over. Rather than use a conditional in my loop, I decided to create the collection first, with the address I want to exclude being kept ...


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