{"record":{"id":"6bce39f87a35a82b","repo":"getgrav/grav","slug":"this-tagname-with-x-x-is-not-an-array","errorCode":null,"errorMessage":"{% {$this->tagName} with x %}: x is not an array","messagePattern":"(.+?) with x %\\}: x is not an array","errorType":"exception","errorClass":"UnexpectedValueException","httpStatus":null,"severity":"error","filePath":"system/src/Grav/Common/Twig/Node/TwigNodeStyle.php","lineNumber":63,"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":45,"sourceCodeEnd":81,"githubUrl":"https://github.com/getgrav/grav/blob/6040efed04efa69b8209448ed81308e7c24147c2/system/src/Grav/Common/Twig/Node/TwigNodeStyle.php#L45-L81","documentation":"The {% style %} tag's compile() emits the same guarded assignment as script/link: the `with <expr>` clause becomes $attributes and the compiled template throws UnexpectedValueException unless it is an array (default [] when absent). The clause holds link/style HTML attributes and must be a Twig mapping or array-valued expression; scalar or null values abort rendering.","triggerScenarios":"{% style 'theme.css' with media %} where media is a string variable; {% style 'a.css' with 'media=print' %} (attribute string instead of mapping); an attributes variable left null by surrounding logic.","commonSituations":"Handwriting style tags without braces on the attribute mapping; variables expected to be hashes but containing strings after refactors; copied examples that use shorthand syntax.","solutions":["Use a mapping literal: {% style 'theme.css' with { media: 'print' } %}","Ensure the variable is an array: {% set attrs = {media: 'print'} %} then {% style 'theme.css' with attrs %}","Default null variables: {% set attrs = attrs ?? {} %}"],"exampleFix":"{% set media = 'print' %}\n{# before: scalar passed to with #}\n{% style 'theme.css' with media %}\n\n{# after: mapping literal #}\n{% style 'theme.css' with { media: 'print' } %}","handlingStrategy":"validation","validationCode":"{# in Twig, guard the variable before the tag #}\n{% set attrs = attrs is defined and attrs is iterable ? attrs : {} %}\n{% style 'theme.css' 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 { media: 'print' }","Default attribute variables with ?? {} before the tag","Lint templates for `with` clauses that are not mappings in CI"],"tags":["twig","assets","template","type-error","tag","css"],"backgroundTag":"twig-tag-argument-type-mismatch","analyzedSha":"6040efed04efa69b8209448ed81308e7c24147c2","analyzedAt":"2026-08-17T05:07:31.593Z","schemaVersion":2},"datasetVersion":"2026-08-17T09:17:11.063Z"}