{"record":{"id":"04ad209f1811c646","repo":"getgrav/grav","slug":"script-with-x-x-is-not-an-array","errorCode":null,"errorMessage":"{% script with x %}: x is not an array","messagePattern":"(.+?): x is not an array","errorType":"exception","errorClass":"UnexpectedValueException","httpStatus":null,"severity":"error","filePath":"system/src/Grav/Common/Twig/Node/TwigNodeScript.php","lineNumber":64,"sourceCode":"    /**\n     * Compiles the node to PHP.\n     *\n     * @param Compiler $compiler A Twig Compiler instance\n     * @return void\n     * @throws LogicException\n     */\n    public function compile(Compiler $compiler): void\n    {\n        $compiler->addDebugInfo($this);\n\n        if ($this->hasNode('attributes')) {\n            $compiler\n                ->write('$attributes = ')\n                ->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);","sourceCodeStart":46,"sourceCodeEnd":82,"githubUrl":"https://github.com/getgrav/grav/blob/6040efed04efa69b8209448ed81308e7c24147c2/system/src/Grav/Common/Twig/Node/TwigNodeScript.php#L46-L82","documentation":"The {% script %} tag's compile() emits PHP that assigns the `with <expr>` clause to $attributes and throws UnexpectedValueException unless it is an array (when the clause is absent, $attributes defaults to []). The clause carries script tag attributes, so it must be a Twig mapping or array-valued expression; anything scalar/null aborts the compiled template at render time.","triggerScenarios":"{% script 'app.js' in 'bottom' with defer %} where defer is a boolean variable; {% script 'app.js' with 'async' %} (bare string); a `with` variable that is null because the surrounding logic never populated it.","commonSituations":"Handwritten asset tags missing the attribute braces; attribute variables unset after template refactors; mixing up argument order so a non-array expression lands in the `with` slot.","solutions":["Use a mapping literal: {% script 'app.js' in 'bottom' with { defer: true, async: false } %}","Ensure any variable used is an array: {% set attrs = {defer: true} %} then {% script 'app.js' with attrs %}","Default null variables: {% set attrs = attrs ?? {} %}"],"exampleFix":"{% set defer = true %}\n{# before: scalar passed to with #}\n{% script 'app.js' in 'bottom' with defer %}\n\n{# after: mapping literal #}\n{% script 'app.js' in 'bottom' with { defer: true } %}","handlingStrategy":"validation","validationCode":"{# in Twig, guard the variable before the tag #}\n{% set attrs = attrs is defined and attrs is iterable ? attrs : {} %}\n{% script 'app.js' in 'bottom' with attrs %}","typeGuard":null,"tryCatchPattern":"try { echo $twig->render($template, $data); } catch (UnexpectedValueException $e) { log_template_error($e); }","preventionTips":["Always write the with clause as a mapping literal: with { defer: true }","Default attribute variables with ?? {} before the tag","Review script tags after refactors that move attributes into variables"],"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"}