getgrav/grav · error · UnexpectedValueException
{% script in x %}: x is not a string
Error message
{% script in x %}: x is not a string What it means
The {% script %} tag compiles the `in <expr>` clause (asset group) into $group = <expr> plus an is_string($group) check that throws UnexpectedValueException at render time on non-strings. The group selects where the script is output; it defaults to 'head' when the clause is missing.
Source
Thrown at system/src/Grav/Common/Twig/Node/TwigNodeScript.php:78
->subcompile($this->getNode('attributes'))
->raw(';' . PHP_EOL)
->write('if (!is_array($attributes)) {' . PHP_EOL)
->indent()
->write("throw new UnexpectedValueException('{% {$this->tagName} with x %}: x is not an array');" . PHP_EOL)
->outdent()
->write('}' . PHP_EOL);
} else {
$compiler->write('$attributes = [];' . PHP_EOL);
}
if ($this->hasNode('group')) {
$compiler
->write('$group = ')
->subcompile($this->getNode('group'))
->raw(';' . PHP_EOL)
->write('if (!is_string($group)) {' . PHP_EOL)
->indent()
->write("throw new UnexpectedValueException('{% {$this->tagName} in x %}: x is not a string');" . PHP_EOL)
->outdent()
->write('}' . PHP_EOL);
} else {
$compiler->write('$group = \'head\';' . PHP_EOL);
}
if ($this->hasNode('priority')) {
$compiler
->write('$priority = (int)(')
->subcompile($this->getNode('priority'))
->raw(');' . PHP_EOL);
} else {
$compiler->write('$priority = 10;' . PHP_EOL);
}
$compiler->write("\$assets = \\Grav\\Common\\Grav::instance()['assets'];" . PHP_EOL);
$compiler->write("\$block = \$context['block'] ?? null;" . PHP_EOL);
View on GitHub (pinned to 6040efed04)
Solutions
- Pass a plain string: {% script 'app.js' in 'bottom' %}
- Default the expression: in group|default('bottom')
- Initialize variables used as groups before the tag: {% set group = 'bottom' %}
Example fix
{% set group = null %}
{# before: null passed to in #}
{% script 'app.js' in group %}
{# after: string group name with default #}
{% script 'app.js' in group|default('bottom') %} Defensive patterns
Strategy: validation
Validate before calling
{# in Twig, guard the group variable before the tag #}
{% set group = group is defined and group is string ? group : 'head' %}
{% script 'app.js' in group %} Try / catch
try { echo $twig->render($template, $data); } catch (UnexpectedValueException $e) { log_template_error($e); } Prevention
- Use plain string literals for asset groups
- Default group expressions with |default('head')
- Verify array lookups used as groups actually resolve (avoid null from missing keys)
When it happens
Trigger: {% script 'app.js' in 2 %}; {% script 'app.js' in groups.bottom %} where groups is a nested array (element missing → null); passing a mapping or list as the group.
Common situations: Group variables never initialized; refactors that turn group names into arrays; index typos yielding null from an array lookup.
Related errors
- {% script with x %}: x is not an array
- {% link with x %}: x is not an array
- {% link in x %}: x is not a string
- {% {$this->tagName} with x %}: x is not an array
- {% {$this->tagName} in x %}: x is not a string
AI-assisted analysis of getgrav/grav@6040efed04 (2026-08-17).
Data as JSON: /api/errors/f0b2ee664671b1a7.
Report an issue: GitHub.