Search

Using Modifiers in Antlers Dynamic Bindings

November 7, 2021 —John Koster

Throughout this article we will be taking a closer look at passing variables to parameters and modifiers by using dynamic binding. Dynamic binding is a technique to supply a value to a tag or modifier by referencing it's variable name. For example, the following Antlers would reference a variable from the template's front matter when invoking the collection tag:

1---
2collection_handle: 'pages'
3---
4 
5{{ collection :from="view:collection_handle" }}
6 {{# Tag body here... #}}
7{{ /collection }}

Dynamic bindings are easily recognizable since the parameter names will always start with :. The following example would supply the literal string value view:collection_handle as the parameter's value, since it not a dynamic binding:

1---
2collection_handle: 'pages'
3---
4 
5{{ collection from="view:collection_handle" }}
6 {{# Tag body here... #}}
7{{ /collection }}

Passing values through the use of dynamic binding is different than the behavior of passing a variable using string interpolation, in the vast majority of cases. String interpolation syntax generally prefers to attempt to convert a value to a string when passed as a parameter. In contrast, dynamic bindings will always supply the original value (such as an asset, array, or other complex type).

#Using Modifiers in Dynamic Bindings

A little known capability of dynamic bindings when working with Antlers is that they can evaluate modifier expressions. The following example will convert a string into an array using the explode modifier and then use that array as the value for the foreach tag:

1{{ foreach :array="'one,two,three' | explode:," }}
2 {{ value }}<br />
3{{ /foreach }}

When the above code example has executed, the output would be similar to the following:

1one
2two
3three

This technique works because the Antlers parser will evaluate modifiers when they are inside dynamic bindings. At the time of writing, this technique cannot be extended to other language constructs such as variable fallback, ternary expressions, etc.

#Iterating a String With foreach

The following example utilizes the explode modifier to iterate a string using the foreach tag:

1{{ foreach :array="'one,two,three' | explode:," }}
2 {{ value }}<br />
3{{ /foreach }}

#Chaining Modifiers in Dynamic Bindings

The following example demonstrates chaining multiple modifiers together when supplying a value for dynamic bindings:

1---
2products: [
3 { name: 'Desk', price: 32, category: 'Office' },
4 { name: 'Plant', price: 9, category: 'Home' },
5 { name: 'Lamp', price: 15, category: 'Office' }
6]
7---
8 
9{{ foreach :array="view:products|pluck:name|reverse|limit:2" }}
10 {{ value }}
11{{ /foreach }}

#Dynamically Filtering Collection Dates

The following example calculates a dynamic date to filter a collection, without the need for temporary variables:

1{{ collection:blog :since="now|modify_date:-2 weeks" }}
2 
3{{ /collection:blog }}

#Conclusion

This article covered advanced usage of Antlers dynamic bindings by also making use of modifiers. At the time of writing (Statamic 3.2 and below), dynamic bindings do not support complex expressions such as variable fallback, or ternaries. Using modifiers within dynamic bindings has the potential to allow for interesting, and complex interactions with other Antlers features.

Some absolutely amazing
people

The following amazing people help support this site and my open source projects ♥️
If you're interesting in supporting my work and want to show up on this list, check out my GitHub Sponsors Profile.