Tag Lookup

Search for and retrieve tags in you twig templates using the tag lookup. Perform a lookup by calling enact.tag and defining a method chain of property/value pairs specifying the conditions under which to lookup tags. Every property defined by the Tag Wrapper is available as a method on the lookup, call the property you want to use as a condition, passing it a value. By default each value is compared to the property by being exactly equal (in SQL just property=value), but you can prefix your values with the below comparators to specify a different comparison (always include a space after the comparator):

  • > - Where property is greater than the value.

  • < - Where property is less than the value.

  • >= - Where property is greater than or equal to the value.

  • <= - Where property is less than or equal to the value.

  • ! - Where the property does not equal the value.

  • LIKE - Where the property contains the value.

Basic example

//get all the tags in the products features section

{% set tags = enact.tag.sectionHandle('productFeatures').fetch %}

When you are done specifying your conditions, call the method fetch to return the array of matching tags, each represented as a \Enact\template\wrapper\Tag. You may also use the first and last method to retrieve the first or last tag returned from the lookup. Both methods use the fetch method internally.

Loop the results

<p>{{ tags|length }} product features</p>

<ul>

{% for tag in tags %}

    <li>{{ tag.getLinkHtml }} - {{ tag.usedTimes }}</li>

{% endfor %}

</ul>

More examples

//get tags in 'Product Features' section that were created within the last month

{% set oneMonthAgo = 'now'|date_modify('-1 month')|date('Y-m-d') %}

{% set tags = enact.tag.sectionName('Product Features').createdOn('>= ' ~ oneMonthAgo).fetch %}


//find the tag with the handle 'xxLarge'

{% set tag = enact.tag.handle('xxLarge').first %}
Search method

Use the search method to perform a LIKE comparison on both the title and handle properties simultaneously.

{% set tags = enact.tag.search('blue').fetch %}
Order method

Use the order method to sort the result of the lookup, passing a property name and and optional sort type (ASC, DESC), by default ASC is used.

{% set tags = enact.tag.search('chrome').order('usedTimes DESC').fetch %}
Limit method

Limit the number of tags returned by the lookup

{% set tags = enact.tag.sectionHandle('productFeatures').limit(10).fetch %}
Page method

Use the page method in conjunction with the limit method to return a specific number of tags from a specific page of the results.

// will return tags 20-40

{% set tags = enact.tag.sectionHandle('productFeatures').limit(20).page(2).fetch %}
Total method

Use the total method to force the lookup to return an integer value specifying the number of tags that matched the condition. The same can be accomplished using the twig length filter, however, this prevents the actual tags from being fetched and returned, saving database resources.

{% set totalTags = enact.tag.sectionHandle('productFeatures').total.fetch %}