Asset Lookup

Search for and retrieve assets in you twig templates using the asset lookup. Perform a lookup by calling enact.asset and defining a method chain of property/value pairs specifying the conditions under which to lookup assets. Every property defined by the Asset 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 assets that are gifs and have a width less than 300px

{% set gifs = enact.asset.extension('gif').width('< 300').fetch %}

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

Loop the results

<p>{{ gifs | length }} GIFs found</p>

{% for gif in gifs %}

    <div class='gif'>

        {{ gif.getImgHtml }}

        <div class='meta'>{{ gif.title }} - {{ gif.fileSize }}</div>

    </div>

{% endfor %}

More examples

//get all assets in the Art group

{% set art = enact.asset.groupHandle('art').fetch %}


//get all PDF assets uploaded by the user with id 33

{% set pdfs = enact.asset.extension('pdf').userId(33).fetch %}


//get all word documents that are bigger than 5mbs

{% set docs = enact.asset.type('word').fileSizeInt('>= 5000000').fetch %}


//get all assets where the file name contains the word 'tree'

{% set assets = enact.asset.fileName('LIKE tree').fetch %}
Search method

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

{% set flowers = enact.asset.search('flower').type('image').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 images = enact.asset.type('image').order('title DESC').fetch %}
Limit method

Limit the number of assets returned by the lookup

{% set pdfs = enact.asset.type('pdf').limit(22).fetch %}
Page method

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

// will return assets 20-40

{% set images = enact.asset.type('image').limit(20).page(2).fetch %}
Total method

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

{% set totalJpegs = enact.asset.extension('jpeg').total.fetch %}