{"record":{"id":"038445bab23af8ad","repo":"getgrav/grav","slug":"link-with-x-x-is-not-an-array","errorCode":null,"errorMessage":"{% link 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/TwigNodeLink.php","lineNumber":67,"sourceCode":"     * @return void\n     * @throws LogicException\n     */\n    public function compile(Compiler $compiler): void\n    {\n        $compiler->addDebugInfo($this);\n        if (!$this->hasNode('file')) {\n            return;\n        }\n\n        $compiler->write('$attributes = [\\'rel\\' => \\'' . $this->getAttribute('rel') . '\\'];' . \"\\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        }\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","sourceCodeStart":49,"sourceCodeEnd":85,"githubUrl":"https://github.com/getgrav/grav/blob/6040efed04efa69b8209448ed81308e7c24147c2/system/src/Grav/Common/Twig/Node/TwigNodeLink.php#L49-L85","documentation":"The {% link %} tag's compile() method emits PHP that evaluates the `with <expr>` clause into $attributes and immediately checks is_array($attributes); a non-array value throws UnexpectedValueException at template render time. The `with` clause is for HTML attributes and must be a Twig mapping (or an array-valued variable). The sibling `in` clause has its own string check, and the whole check only compiles when a file node is present.","triggerScenarios":"{% link 'style.css' with defer %} where defer is a scalar variable; {% link 'x' with 'defer=true' %} (string instead of mapping); a `with` expression that evaluates to null because the variable was never set; passing an object instead of an array.","commonSituations":"Omitting the braces of the attribute mapping when handwriting the tag; variables expected to hold attribute arrays but left unset after refactors; copying tag examples from docs that show shorthand.","solutions":["Use a mapping literal: {% link 'style.css' with { rel: 'preload', as: 'style' } %}","If using a variable, ensure it is an array: {% set attrs = {defer: true} %} then {% link 'app.css' with attrs %}","Default null variables before the tag: {% set attrs = attrs ?? {} %}"],"exampleFix":"{% set defer = true %}\n{# before: scalar passed to with #}\n{% link 'app.css' with defer %}\n\n{# after: mapping literal #}\n{% link 'app.css' 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{% link 'app.css' with attrs %}","typeGuard":null,"tryCatchPattern":"try { echo $twig->render($template, $data); } catch (UnexpectedValueException $e) { // report template + line from the exception trace\n    log_template_error($e); }","preventionTips":["Always write the with clause as a mapping literal: with { key: value }","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"],"backgroundTag":"twig-tag-argument-type-mismatch","analyzedSha":"6040efed04efa69b8209448ed81308e7c24147c2","analyzedAt":"2026-08-17T05:07:31.593Z","schemaVersion":2},"datasetVersion":"2026-08-17T09:17:11.063Z"}