Template Tags

The tag pair is how you output Ansel images. Whether you are using the custom field tag within a Channel Entry tag pair, a Grid field tag pair, a Bloqs field tag pair, a Low Variables tag pair, or the stand alone tag pair, usage is almost identical across the tags. In fact, the only real difference is with the stand alone tag pair, which has just a few extra parameters available to it.

{exp:channel:entries channel="my_channel"}
    <!-- Number of images in field -->
    {my_ansel_field count="true"}

    <!-- Check if field has images -->
    {if "{my_ansel_field count='true'}" > 0}
        <!-- Do stuff if field has images -->
    {/if}

    <!-- Output images -->
    {my_ansel_field}
        {img:url}
        
        <!-- If using named crops output the entire <picture> tag -->
        {img:picture_tag}
    {/my_ansel_field}

    <!-- Output images from a grid field -->
    {grid_field}
        {grid_field:my_ansel_field}
            {img:url}
        {/grid_field:my_ansel_field}
    {/grid_field}
    
    <!-- Output images from a Relationship field -->
    {my_relationship_field}
        {my_ansel_field}
            {img:url}
        {/my_ansel_field}
        
        or
        
        {my_relationship_field:my_ansel_field}
            {img:url}
        {/my_relationship_field:my_ansel_field}
    {/my_relationship_field}
    
    <!-- Output images from nested Relationship fields across 2 entries -->
    {my_relationship_field}
        {my_relationship_field:child_relationship_field}
            {my_ansel_field}
                {img:url}
            {/my_ansel_field}
        {/my_relationship_field:child_relationship_field}
    {/my_relationship_field}

    <!-- Output images from a blocks field -->
    {blocks_field}
        {my_block}
            {my_ansel_block_field}
                {img:url}
            {/my_ansel_block_field}
        {/my_block}
    {/blocks_field}
{/exp:channel:entries}

<!-- Output images from a Low Variables Ansel field -->
{exp:low_variables:pair var="my_ansel_var"}
    {img:url:resize width="500"}
{/exp:low_variables:pair}

<!-- Output images with the stand alone tag -->
{exp:ansel:images content_id="102" field_id="33"}
    {if img:no_results}
        <!-- Do no results markup here -->
    {/if}
    {img:url:resize width="400"}
{/exp:ansel:images}

Tag Pair Parameters

The following parameters are unique to the stand-alone image tag:

Tag Pair Variables

Note all variable examples assume the default namespace of img. Use your custom namespace if you have set the namespace parameter.

These variables are available in the tag pair:

The variables starting with "original" reference the value from the source file. When Ansel saves an image, it creates a new instance of that image in your file manager (wether it was cropped or not). Each instance of a file can have it's own description, caption, title etc. If you want to always reference the original description, regardless of which cropped version you're displaying in the template, you can use a conditional. For example:

When you have made manipulations available with the parameter manipulations="true", they will be available as follows:

No Results

If your tag pair has no results, you can use the {if img:no_results} conditional to output content.

On the Fly Cropping and Resizing

Ansel can crop and resize images on the fly. Doing so is very efficient because the cropped/resized image is cached after the first request for it is made. Any request for that cropped/resized image thereafter comes straight from the cache on disk.

Cropping/resizing is available to the {img:url} variable.

Get Total

Sometimes you would like to display the total number of images in a field. Ansel makes this really easy with a parameter on the custom field tag:

Picture Tag

{img:picture_tag}

Outputs a complete <picture> element. Only named crops with a Media Query value are included as <source> elements. The fallback <img> always uses the main crop ({img:url}).

Example output:

Tag Parameters

Parameter
Default
Description

picture_tag_loading

lazy

Value of the loading attribute on the <img> element

picture_tag_class

(none)

CSS class applied to the <picture> element

Example:


Config Variable Substitution in Media Queries

Media query values in the field settings support {config_key} placeholders that are resolved at render time from EE's $config array. This avoids hard-coding breakpoints in the database and makes them easy to update site-wide.

In config/config.php:

In the field settings Media Query column:

At render time these resolve to their config values. Unrecognized placeholders are left as-is so typos are immediately visible in the rendered HTML.

Twig/Blade

If you're using Coilpack, Ansel supports Twig and Blade templates. All of the same parameters from the EE Template examples above are also available in Twig or Blade.

Last updated