<p>I haven't tested this, so I'm actually not sure whether it would work...</p>
<p>What if you moved the <code>{{ sprig('template') }}</code> line to a separate included template?</p>
<pre><code>{% if craft.app.plugins.isPluginEnabled('sprig')) %}
    {% include 'template/using/sprig' %}
{% endif %}
</code></pre>
<p>In theory, the Sprig line would never need to be parsed if the Twig include were never called.</p>
<hr />
<p>Side note, you don't need to check both <code>isPluginInstalled</code> and <code>isPluginEnabled</code> separately. Both checks will be done by the <code>isPluginEnabled</code> function.</p>

CC BY-SA 4.0
2023-06-08T23:19:02.907Z
2023-06-08T23:19:02.907Z
Lindsey D
<blockquote>
<p>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.</p>
</blockquote>
<p>This can only mean that a <code>sprig()</code> call is being executed elsewhere in your templates. You should search your entire <code>templates</code> directory (and the codebase of any custom modules/plugins) for <code>sprig</code>. Also, be sure to clear the cache to ensure that compiled templates are cleared out before retesting.</p>

CC BY-SA 4.0
2023-06-09T11:30:18.663Z
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.
CC BY-SA 4.0
2023-06-10T16:34:31.370Z
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
CC BY-SA 4.0
2023-06-10T20:30:33.360Z
Good catch, Lindsey!
Ben Croker
CC BY-SA 4.0
2023-06-09T06:51:02.233Z
2023-06-09T06:51:02.233Z
Ben Croker
<p>I am using the <a href="https://putyourlightson.com/plugins/sprig" rel="nofollow noreferrer">Sprig plugin</a>, and for some reason I'm disabling it in some environments.</p>
<p>In <code>config/general.php</code> I have added: <code>'disabledPlugins' =&gt; ['sprig']</code></p>
<p>And in my twig template, I have a condition like the following:</p>
<pre><code>{% if craft.app.plugins.isPluginInstalled('sprig') and craft.app.plugins.isPluginEnabled('sprig')) %}
    {{ sprig('template') }}
{% endif %}
</code></pre>
<p>However, even if the condition is <code>false</code>, I get this error:</p>
<pre><code>Twig Syntax Error – Twig\Error\SyntaxError
Unknown &quot;sprig&quot; function.
</code></pre>
<p>The same happen if I dump and die before <code>{{ sprig('template') }}</code>, 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.</p>
<p><a href="https://i.sstatic.net/0ma5H.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/0ma5H.png" alt="Screenshot of error" /></a></p>
<p>How to make the condition working to prevent <code>Twig Syntax Error</code> within an invalidated condition?</p>

<templating><plugins><config><conditional><plugin-sprig>
How to prevent Twig Syntax Error when a plugin is disabled?
# How to prevent Twig Syntax Error when a plugin is disabled?

(Asked by: username_0 on 2023-06-08)

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.

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

**Tags:** templating, plugins, config, conditional, plugin-sprig

---

## 2 answers

### Answer by username_1 on 2023-06-08. Score: 2 | Accepted answer

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.


---

### Answer by username_2 on 2023-06-09. Score: 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.

> **Comments:**
> **username_0**: 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.  
> **username_1**: 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).  
> **username_2**: Good catch, Lindsey!  

---

