{"record":{"id":"e042fa40540730e2","repo":"aosabook/500lines","slug":"s-r","errorCode":null,"errorMessage":"%s: %r","messagePattern":"%s: %r","errorType":"exception","errorClass":"TempliteSyntaxError","httpStatus":null,"severity":"error","filePath":"template-engine/code/templite.py","lineNumber":217,"sourceCode":"        if \"|\" in expr:\n            pipes = expr.split(\"|\")\n            code = self._expr_code(pipes[0])\n            for func in pipes[1:]:\n                self._variable(func, self.all_vars)\n                code = \"c_%s(%s)\" % (func, code)\n        elif \".\" in expr:\n            dots = expr.split(\".\")\n            code = self._expr_code(dots[0])\n            args = \", \".join(repr(d) for d in dots[1:])\n            code = \"do_dots(%s, %s)\" % (code, args)\n        else:\n            self._variable(expr, self.all_vars)\n            code = \"c_%s\" % expr\n        return code\n\n    def _syntax_error(self, msg, thing):\n        \"\"\"Raise a syntax error using `msg`, and showing `thing`.\"\"\"\n        raise TempliteSyntaxError(\"%s: %r\" % (msg, thing))\n\n    def _variable(self, name, vars_set):\n        \"\"\"Track that `name` is used as a variable.\n\n        Adds the name to `vars_set`, a set of variable names.\n\n        Raises an syntax error if `name` is not a valid name.\n\n        \"\"\"\n        if not re.match(r\"[_a-zA-Z][_a-zA-Z0-9]*$\", name):\n            self._syntax_error(\"Not a valid name\", name)\n        vars_set.add(name)\n\n    def render(self, context=None):\n        \"\"\"Render this template by applying it to `context`.\n\n        `context` is a dictionary of values to use in this rendering.\n","sourceCodeStart":199,"sourceCodeEnd":235,"githubUrl":"https://github.com/aosabook/500lines/blob/fba689d101eb5600f5c8f4d7fd79912498e950e2/template-engine/code/templite.py#L199-L235","documentation":"Raised by Templite._syntax_error, the single chokepoint for all template-compilation errors. It formats a human-readable msg and the offending token via '%s: %r'. TempliteSyntaxError subclasses ValueError and is raised during Templite() construction (compile time), never at render time. Possible messages come from the parser: invalid variable names, malformed if/for/endif/endfor tags, too many ends, mismatched end tags, unknown tags, and unmatched action tags.","triggerScenarios":"Constructing Templite(text) where text contains: a variable name failing the regex [_a-zA-Z][_a-zA-Z0-9]*$ (e.g. 'user-name'); an {% if %} without exactly one expression; a {% for %} not matching 'for var in expr'; extra {% endif %}/{% endfor %}; an {% endfor %} after an {% if %}; an unsupported {% tag %}; or a missing end tag at EOF.","commonSituations":"Hand-written templates with typos; copy-pasting Django syntax this nano-subset does not support (no {% else %}, no filter arguments); forgetting to close an {% if %}/{% for %}; using hyphens, dots, or spaces in variable names.","solutions":["Read the message: it names the problem ('Not a valid name', 'Don't understand for', etc.) and shows the token via %r.","Make variable names match [_a-zA-Z][_a-zA-Z0-9]* (no hyphens, dots, or spaces).","Match every {% if %}/{% for %} with exactly one {% endif %}/{% endfor %} of the matching type and add no extra ends.","Remember only if/for/endif/endfor are supported; remove unsupported constructs."],"exampleFix":"// before\nTemplite('Hi {{user-name}}')              # Not a valid name: 'user-name'\nTemplite('{% if a b %}x{% endif %}')      # Don't understand if\n// after\nTemplite('Hi {{user_name}}')\nTemplite('{% if a %}x{% endif %}')","handlingStrategy":"try-catch","validationCode":"import re\nNAME_RE = re.compile(r'[_a-zA-Z][_a-zA-Z0-9]*$')\n\ndef looks_valid(template):\n    for m in re.findall(r'{{(.+?)}}', template):\n        for part in m.split('|'):\n            head = part.split('.')[0].strip()\n            if head and not NAME_RE.match(head):\n                return False, head\n    return True, None","typeGuard":null,"tryCatchPattern":"try:\n    t = Templite(template_text)\nexcept TempliteSyntaxError as e:\n    log.error('bad template: %s', e)\n    t = None","preventionTips":["Validate template strings in tests before deployment.","Restrict variable names to [_a-zA-Z][_a-zA-Z0-9]* (no hyphens/dots/spaces).","Remember only if/for/endif/endfor are supported; avoid unsupported Django tags.","Pair every {% if %}/{% for %} with the matching end tag."],"tags":["template","syntax-error","parser","python","compile-time"],"backgroundTag":null,"analyzedSha":"fba689d101eb5600f5c8f4d7fd79912498e950e2","analyzedAt":"2026-08-13T06:26:32.792Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}