If you use Cheetah, please support the project and register by clicking here:


5.4 Are all those dollar signs really necessary?

$ is a ``smart variable prefix''. When Cheetah sees $, it determines both the variable's position and whether it's a searchList value or a non-searchList value, and generates the appropriate Python code.

In top-level position, the $ is required. Otherwise there's nothing to distinguish the variable from ordinary text, and the variable name is output verbatim.

In expression position, the $ is required if the value comes from the searchList or a ``#set global'' variable, recommended for local/global/builtin variables, and not necessary for the special constants None, True and False. This works because Cheetah generates a function call for a searchList placeholder, but a bare variable name for a local/global/builtin variable.

In LVALUE position, the $ is recommended. Cheetah knows where an LVALUE is expected, so it can handle your variable name whether it has $ or not.

EXCEPTION: Do not use the $ prefix for intermediate variables in a Python list comprehensions. This is a limitation of Cheetah's parser; it can't tell which variables in a list comprehension are the intermediate variables, so you have to help it. For example:

#set $theRange = [x ** 2 for x in $range(10)]
$theRange is a regular #set variable. $range is a Python built-in function. But x is a scratch variable internal to the list comprehension: if you type $x, Cheetah will miscompile it.

If you use Cheetah, please support the project and register by clicking here: