Twigeasy_svg_icon_exists()

easy_svg_icon_exists()

Check in a Timber or Twig template whether an icon really renders before you draw it, so a missing icon never leaves an empty wrapper behind.

Twig function · Easy SVG Pro · Status: Stable

Icons get renamed, sets get deleted, fields hold old values. This Twig function answers one question before you print anything: would this icon draw something? It is true only when the icon renders to real markup, from any collection on the site.

Usage

{% if easy_svg_icon_exists( 'collection/slug' ) %}
	...
{% endif %}

Registered in includes/twig/twig.php on the timber/twig filter. Available since Easy SVG Pro 1.7.0.

When to use it

  • You print wrapper markup around an icon (a badge, a circle, a link) and do not want an empty wrapper when the icon is missing.
  • You want a visible fallback: another icon, a letter, or nothing at all.
  • You debug a template and want to see which addresses resolve.

Do not use it when:

  • You print the icon right after the check. That renders it twice. Capture the output with a set block instead (see the pitfalls).
  • You want to check something in PHP. Call the free plugin's easy_svg_icon() and compare with an empty string.

Parameters

body
namestring
Required

An icon address collection/slug, or a bare slug from the free library. A non-string or empty value answers false.

Return value

returnbool

true when the icon renders to non-empty markup, false otherwise.

Default

There are no optional arguments.

Examples

Only print the badge when the icon is there

{% if easy_svg_icon_exists( 'acme/award' ) %}
	<span class="badge badge--award">
		{{ easy_svg_icon( 'acme/award', { label: 'Award winner' } ) }}
	</span>
{% endif %}

A fallback icon per post type

{% set ref = post.meta( 'easy_svg_icon' ) ?: 'acme/' ~ post.post_type %}
{% if not easy_svg_icon_exists( ref ) %}
	{% set ref = 'acme/document' %}
{% endif %}
{{ easy_svg_icon( ref ) }}

Agency: a template check page for the design team

{# templates/icon-check.twig, only linked for admins on staging #}
<table>
	{% for ref in [ 'acme/phone', 'acme/mail', 'acme/map-pin', 'acme/old-logo' ] %}
		<tr>
			<td><code>{{ ref }}</code></td>
			<td>{{ easy_svg_icon_exists( ref ) ? 'ok' : 'MISSING' }}</td>
		</tr>
	{% endfor %}
</table>

Run it after a rename or an import, and you see at once which addresses in your templates no longer resolve.

Defensive: check and draw with one render

{% set icon %}{{ easy_svg_icon( 'acme/award' ) }}{% endset %}
{% if icon|trim is not empty %}
	<span class="badge">{{ icon }}</span>
{% endif %}

This is not a use of easy_svg_icon_exists() at all, and that is the point: when you print the icon anyway, capturing it is cheaper than checking first.

How it works

The function calls the same renderer as easy_svg_icon() and returns whether the result is a non-empty string. It does not look in the free plugin's icon post type. That matters: Pro icon sets, theme folders, drop-in folders and code registrations exist only through the easy_svg_icon_markup filter, so a post type lookup would call all of them missing.

It is registered under the same conditions as the other Twig functions: active licence, Timber / Twig on under Icons → Features, and a Twig environment with addFunction().

Pitfalls

  • It costs a full render. Each call renders the icon, with every filter on easy_svg_icon_markup, including Styles & theme colors when that is on. Checking then printing renders twice. Use the capture pattern from the last example in loops.
  • Unknown function when the feature is off. If the licence lapses or the feature is switched off, this function is missing, and Twig fails to compile a template that calls it. Ironically, the check itself then breaks the page. See the Timber and Twig tutorial.
  • Exists means "renders here and now". An icon in a set that is a draft, a folder collection while Theme & folder sources is off, or a set while the Icon set editor is off all answer false, even though the files or posts exist.