# Writing templates If you arrive from Jinja, Chameleon will feel upside down for about an hour. Jinja puts its logic in the text, between `{% %}` and `{ }` markers, and the HTML flows around it. Chameleon puts its logic on the elements, as `tal:` attributes, and the HTML stays HTML. After the hour it feels inevitable: there is no element you can forget to close, because the template is a document, and the browser will tell you when it isn't. This page is the working subset. Every snippet on it was rendered with the Chameleon release the library installs, so the output shown is the output you get. The full language is documented at [chameleon.readthedocs.io](https://chameleon.readthedocs.io/). # Interpolation: `${...}` Values from the model drop in through `${...}`, and the expression inside is ordinary Python: ``` html

Hello, ${user.name.title()}!

You have ${len(items)} item(s).

${'Welcome back' if user else 'Please sign in'}

``` Everything is HTML-escaped on the way in. A model value of `bold` renders as `bold`, which is what you want for anything a user typed. When a value is already safe HTML, say a rendered Markdown document, prefix the expression with `structure:` to insert it verbatim: ``` html
article goes here
``` A name that is not in the model raises a `NameError` at render time, naming the missing variable. If a name is genuinely optional, give it a fallback with the `|` operator: ``` html

${subtitle | 'No subtitle'}

${tracking_id | nothing}

``` `nothing` is Chameleon's spelling of `None`; it renders as an empty string. # Loops: `tal:repeat` `tal:repeat="item items"` repeats the element it sits on once per item: ``` html ``` Inside the loop a `repeat` object tracks where you are. `repeat.item.index` counts from zero, `repeat.item.number` from one, and `repeat.item.odd` and `repeat.item.even` are the booleans you reach for when striping rows: ``` html ${repeat.row.number} ${row.title} ``` An empty sequence renders nothing, not even the element, so an empty `