certainlyakey
7/12/2015 - 12:08 PM

Craft CMS - display entry thumbnail macro

Craft CMS - display entry thumbnail macro

{# Usage: {{ siteMacros.thumbnail( entry, false, "className" ) }} #}
{# in main layout: {% import '_partials/macros' as siteMacros %} #}
{% macro thumbnail(entryModel, isFigure, transformName="", className="thumbnail") %}
	{% set thumbnails = entryModel.thumbnail %} {# array #}

	{% if thumbnails | length %}
		{% set thumbnail = thumbnails.first() %} 

		{# assumes these handles for the fields #}
		{% set alt = thumbnail.alt %} 
		{% set caption = thumbnail.caption %} 

		{% if isFigure %}
			<figure class="{{ className }}">
		{% endif %}

		<img src="{{ thumbnail.getUrl(transformName) }}" width="{{ thumbnail.getWidth(transformName) }}" height="{{ thumbnail.getHeight(transformName) }}" alt="{{ alt }}"{{ (isFigure ? '' : ' class="' ~ className ~ '"')|raw }}>

		{% if isFigure %}
			{% if caption %}
				<figcaption>{{ caption }}</figcaption>
			{% endif %}
			</figure>
		{% endif %}

	{% endif %}

{% endmacro %}