Edit on GitHub
Jump to docs navigation

Twig Components / Twig tests

Note: You are currently reading the documentation for Bolt 4.0. Looking for the documentation for Bolt 5.2 instead?

json

Use this test to determine if a given variable is JSON.

Examples:

{% if var is json %}
    JSON: {{ var }}
{% else %}
    JSON: {{ var|json_encode }}
{% endif %}
{% if var is json %}
    Decoded: {{ var|json_decode }}
{% endif %}

defined (for extensions)

Use this test to determine if a certain extension is available. You can use this in your themes, when it's not apparent whether or not the user will have a certain extension installed.

Examples:

{% if app.extensions.get('Bolt/FacebookComments') is defined %}
    {{ include(template_from_string("{{ facebookcomments() }}")) }}
{% endif %}

You can also use this to output a friendly warning to users of the template:

{% if app.extensions.get('Bolt/BoltForms') is defined %}
    {{ include(template_from_string("{{ boltforms('contact') }}")) }}
{% else %}
    <p>Warning: This theme suggests you install the 'BoltForms' extension.</p>
{% endif %}

Note: in the {% if %} tag you must use the vendorname and extensionname of the extension as a string, so be sure to use quotation marks. If you're not sure what the correct name is that you need to use, dump the installed extensions to find out:

{{ dump(app.extensions.all()) }}


Edit this page on GitHub
Couldn't find what you were looking for? We are happy to help you in the forum, on Slack or on Github.