{"record":{"id":"f0b2ee664671b1a7","repo":"getgrav/grav","slug":"script-in-x-x-is-not-a-string","errorCode":null,"errorMessage":"{% script in x %}: x is not a string","messagePattern":"(.+?): x is not a string","errorType":"exception","errorClass":"UnexpectedValueException","httpStatus":null,"severity":"error","filePath":"system/src/Grav/Common/Twig/Node/TwigNodeScript.php","lineNumber":78,"sourceCode":"                ->subcompile($this->getNode('attributes'))\n                ->raw(';' . PHP_EOL)\n                ->write('if (!is_array($attributes)) {' . PHP_EOL)\n                ->indent()\n                ->write(\"throw new UnexpectedValueException('{% {$this->tagName} with x %}: x is not an array');\" . PHP_EOL)\n                ->outdent()\n                ->write('}' . PHP_EOL);\n        } else {\n            $compiler->write('$attributes = [];' . PHP_EOL);\n        }\n\n        if ($this->hasNode('group')) {\n            $compiler\n                ->write('$group = ')\n                ->subcompile($this->getNode('group'))\n                ->raw(';' . PHP_EOL)\n                ->write('if (!is_string($group)) {' . PHP_EOL)\n                ->indent()\n                ->write(\"throw new UnexpectedValueException('{% {$this->tagName} in x %}: x is not a string');\" . PHP_EOL)\n                ->outdent()\n                ->write('}' . PHP_EOL);\n        } else {\n            $compiler->write('$group = \\'head\\';' . PHP_EOL);\n        }\n\n        if ($this->hasNode('priority')) {\n            $compiler\n                ->write('$priority = (int)(')\n                ->subcompile($this->getNode('priority'))\n                ->raw(');' . PHP_EOL);\n        } else {\n            $compiler->write('$priority = 10;' . PHP_EOL);\n        }\n\n        $compiler->write(\"\\$assets = \\\\Grav\\\\Common\\\\Grav::instance()['assets'];\" . PHP_EOL);\n        $compiler->write(\"\\$block = \\$context['block'] ?? null;\" . PHP_EOL);\n","sourceCodeStart":60,"sourceCodeEnd":96,"githubUrl":"https://github.com/getgrav/grav/blob/6040efed04efa69b8209448ed81308e7c24147c2/system/src/Grav/Common/Twig/Node/TwigNodeScript.php#L60-L96","documentation":"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.","triggerScenarios":"{% 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.","commonSituations":"Group variables never initialized; refactors that turn group names into arrays; index typos yielding null from an array lookup.","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' %}"],"exampleFix":"{% set group = null %}\n{# before: null passed to in #}\n{% script 'app.js' in group %}\n\n{# after: string group name with default #}\n{% script 'app.js' in group|default('bottom') %}","handlingStrategy":"validation","validationCode":"{# in Twig, guard the group variable before the tag #}\n{% set group = group is defined and group is string ? group : 'head' %}\n{% script 'app.js' in group %}","typeGuard":null,"tryCatchPattern":"try { echo $twig->render($template, $data); } catch (UnexpectedValueException $e) { log_template_error($e); }","preventionTips":["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)"],"tags":["twig","assets","template","type-error","tag","javascript"],"backgroundTag":"twig-tag-argument-type-mismatch","analyzedSha":"6040efed04efa69b8209448ed81308e7c24147c2","analyzedAt":"2026-08-17T05:07:31.593Z","schemaVersion":2},"datasetVersion":"2026-08-17T09:17:11.063Z"}