2

I am using the Sprig plugin, and for some reason I'm disabling it in some environments.

In config/general.php I have added: 'disabledPlugins' => ['sprig']

And in my twig template, I have a condition like the following:

{% if craft.app.plugins.isPluginInstalled('sprig') and craft.app.plugins.isPluginEnabled('sprig')) %}
    {{ sprig('template') }}
{% endif %}

However, even if the condition is false, I get this error:

Twig Syntax Error – Twig\Error\SyntaxError
Unknown "sprig" function.

The same happen if I dump and die before {{ sprig('template') }}, so even if the condition is not completed. The same happen if I dump and die before the condition, while the function is not used somewhere else.

Screenshot of error

How to make the condition working to prevent Twig Syntax Error within an invalidated condition?

2 Answers 2

2

I haven't tested this, so I'm actually not sure whether it would work...

What if you moved the {{ sprig('template') }} line to a separate included template?

{% if craft.app.plugins.isPluginEnabled('sprig')) %}
    {% include 'template/using/sprig' %}
{% endif %}

In theory, the Sprig line would never need to be parsed if the Twig include were never called.


Side note, you don't need to check both isPluginInstalled and isPluginEnabled separately. Both checks will be done by the isPluginEnabled function.

1

The same happen if I dump and die before {{ sprig('template') }}, so even if the condition is not completed. The same happen if I dump and die before the condition, while the function is not used somewhere else.

This can only mean that a sprig() call is being executed elsewhere in your templates. You should search your entire templates directory (and the codebase of any custom modules/plugins) for sprig. Also, be sure to clear the cache to ensure that compiled templates are cleared out before retesting.

3
  • I don't have any other sprig function in any of my others templates. The only other one I have is {{ sprig.script }} which if wrapped inside a craft.app.plugins.isPluginEnabled('sprig') condition. In my module, I don't have any reference to sprig, just the Craft::$app->plugins->isPluginEnabled('sprig') condition. I have updated my question with a screenshot of the error in context.
    – Romain P.
    Jun 9, 2023 at 11:30
  • I think the error is happening when Twig is parsed to PHP... not when that PHP is actually being executed. So in Romain's original example, Twig would still be trying to parse the sprig() function into its PHP equivalent (regardless of whether it will be hit when the PHP executes).
    – Lindsey D
    Jun 10, 2023 at 16:34
  • Good catch, Lindsey!
    – Ben Croker
    Jun 10, 2023 at 20:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.