{"record":{"id":"ab0367f07fe4f2d1","repo":"twigphp/Twig","slug":"the-template-references-in-a-use-statement-must-be-a-string","errorCode":null,"errorMessage":"The template references in a \"use\" statement must be a string.","messagePattern":"The template references in a \"use\" statement must be a string\\.","errorType":"exception","errorClass":"SyntaxError","httpStatus":null,"severity":"error","filePath":"src/TokenParser/UseTokenParser.php","lineNumber":43,"sourceCode":" *\n *    {% use \"blocks.html\" %}\n *\n *    {% block title %}{% endblock %}\n *    {% block content %}{% endblock %}\n *\n * @see https://twig.symfony.com/doc/templates.html#horizontal-reuse for details.\n *\n * @internal\n */\nfinal class UseTokenParser extends AbstractTokenParser\n{\n    public function parse(Token $token): Node\n    {\n        $template = $this->parser->parseExpression();\n        $stream = $this->parser->getStream();\n\n        if (!$template instanceof ConstantExpression) {\n            throw new SyntaxError('The template references in a \"use\" statement must be a string.', $stream->getCurrent()->getLine(), $stream->getSourceContext());\n        }\n\n        $targets = [];\n        if ($stream->nextIf('with')) {\n            while (true) {\n                $name = $stream->expect(Token::NAME_TYPE)->getValue();\n\n                $alias = $name;\n                if ($stream->nextIf('as')) {\n                    $alias = $stream->expect(Token::NAME_TYPE)->getValue();\n                }\n\n                $targets[$name] = new ConstantExpression($alias, -1);\n\n                if (!$stream->nextIf(Token::PUNCTUATION_TYPE, ',')) {\n                    break;\n                }\n            }","sourceCodeStart":25,"sourceCodeEnd":61,"githubUrl":"https://github.com/twigphp/Twig/blob/a414c3a491defb5a60f2fc88ef79ff37c90010cd/src/TokenParser/UseTokenParser.php#L25-L61","documentation":"The {% use %} statement requires its template reference to be a literal string (ConstantExpression). Dynamic template names (variables, expressions) cannot be used because imports are resolved at compile time for horizontal reuse.","triggerScenarios":"Writing '{% use some_var %}' or '{% use templates[\"blocks\"] %}' — parse throws when parseExpression() returns anything other than a ConstantExpression.","commonSituations":"Trying to parameterize which trait/block template is imported based on config or request; confusing {% use %} with {% include %}, which does accept dynamic names.","solutions":["Replace the dynamic name with a literal string, e.g. '{% use \"blocks/layout.html.twig\" %}'","Use {% include %} or {% import %} with a dynamic name if runtime selection is required","Handle the variation with an if/else around multiple literal {% use %} statements (in separate branches/templates)"],"exampleFix":"// before\n{% use templates.get('blocks') %}\n// after\n{% use \"blocks/default.html.twig\" %}","handlingStrategy":"validation","validationCode":"// use statements need string literals\nforeach (tokenizeTemplateUseStatements($src) as $useExpr) {\n    if (!preg_match('/^\\s*([\\'\\\"]).+\\1\\s*$/', $useExpr)) {\n        throw new InvalidArgumentException('use requires a quoted template string.');\n    }\n}","typeGuard":null,"tryCatchPattern":"try {\n    $twig->render($template, $ctx);\n} catch (SyntaxError $e) {\n    if (str_contains($e->getMessage(), '\"use\" statement')) {\n        // replace dynamic use with include/import or literal\n    }\n    throw $e;\n}","preventionTips":["Always quote template names in {% use %}","Use {% include %}/dynamic import when the template must be chosen at runtime","Keep horizontal-reuse templates as fixed, versioned includes"],"tags":["twig","use","syntax-error","template"],"backgroundTag":"invalid-argument-value","analyzedSha":"a414c3a491defb5a60f2fc88ef79ff37c90010cd","analyzedAt":"2026-09-13T15:10:46.849Z","contentChangedAt":"2026-09-13T15:10:46.849Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}