Hello everyone,
I am currently reviewing the themes I maintain to ensure they are fully compatible with Grav 2, and I have noticed a behavior that seems different from Grav 1.7.x regarding Twig conditional statements.
As an example, Quark (and Quark2) contains several conditional blocks in the sidebar that display content depending on whether a plugin is enabled. For example:
{% if config.plugins.archives.enabled %}
<div class="sidebar-content">
<h4>{{ 'THEME_QUARK2.SIDEBAR.ARCHIVES.HEADLINE'|t }}</h4>
{% include 'partials/archives.html.twig' with {'base_url': new_base_url} %}
</div>
{% endif %}
In Grav 1.7.x, if the Archives plugin was not installed, execution would not enter the if block.
However, in Grav 2 (current beta), the condition appears to evaluate as true even when the plugin is not installed. As a result, the <h4> element is rendered and Twig attempts to process the code inside the block.
This becomes problematic when a theme relies on filters or functions provided by optional plugins. For example, I have code similar to:
{% if config.plugins['translate-date'].enabled %}
{{ page.date|td(lang, format) }}
{% endif %}
If the translate-date plugin is not installed, I would expect the condition to evaluate to false and skip the block entirely. Instead, Twig enters the block and then throws an error because the td filter does not exist.
I have also tried several alternative checks, including:
{% if config.plugins.archives.enabled is not empty %}
{% if config.plugins.archives.enabled is not null %}
{% if attribute(config.plugins, 'archives') is not null %}
{% if attribute(config.plugins, 'archives').enabled|default(false) %}
but the behavior seems unchanged: the condition still evaluates as true and execution enters the block.
My questions are:
- Is this expected behavior in Grav 2 / Twig 3?
- Has the way plugin configuration objects are exposed to Twig changed compared to Grav 1.7?
- What is the recommended way to detect whether a plugin is actually installed and available before using filters, functions, or templates provided by that plugin?
I would appreciate any guidance, as I am currently updating several themes and want to follow the recommended approach for Grav 2.
Thank you.
