Skip to content
Snippets Groups Projects
Commit d1e8233c authored by Mike Bayer's avatar Mike Bayer
Browse files

- UNDEFINED is a reserved word too, fix docs, add tests

parent 951d1508
No related branches found
Tags rel_0_7_0
No related merge requests found
...@@ -12,7 +12,7 @@ ...@@ -12,7 +12,7 @@
names, that is names which are never pulled names, that is names which are never pulled
from the context and cannot be passed to from the context and cannot be passed to
the template.render() method. Current names the template.render() method. Current names
are "context", "loop". are "context", "loop", "UNDEFINED".
- [feature] The html_error_template() will now - [feature] The html_error_template() will now
apply Pygments highlighting to the source apply Pygments highlighting to the source
......
...@@ -52,6 +52,8 @@ buffer stack. For this reason, just stick with ...@@ -52,6 +52,8 @@ buffer stack. For this reason, just stick with
``context.write()`` and content will always go to the topmost ``context.write()`` and content will always go to the topmost
buffer. buffer.
.. _context_vars:
Context Variables Context Variables
------------------ ------------------
...@@ -363,10 +365,10 @@ All the built-in names ...@@ -363,10 +365,10 @@ All the built-in names
A one-stop shop for all the names Mako defines. Most of these A one-stop shop for all the names Mako defines. Most of these
names are instances of :class:`.Namespace`, which are described names are instances of :class:`.Namespace`, which are described
in the next section, :ref:`namespaces_toplevel`. Also, most of in the next section, :ref:`namespaces_toplevel`. Also, most of
these names other than :class:`.Context` and ``UNDEFINED`` are these names other than ``context``, ``UNDEFINED``, and ``loop`` are
also present *within* the :class:`.Context` itself. There are only also present *within* the :class:`.Context` itself. The names
two names, ``context`` and ``loop``, that are themselves not defined ``context``, ``loop`` and ``UNDEFINED`` themselves can't be passed
in the context and can't be replaced - see the section :ref:`reserved_names`. to the context and can't be substituted - see the section :ref:`reserved_names`.
* ``context`` - this is the :class:`.Context` object, introduced * ``context`` - this is the :class:`.Context` object, introduced
at :ref:`context`. at :ref:`context`.
...@@ -409,12 +411,13 @@ in the context and can't be replaced - see the section :ref:`reserved_names`. ...@@ -409,12 +411,13 @@ in the context and can't be replaced - see the section :ref:`reserved_names`.
Reserved names Reserved names
-------------- --------------
Mako has two words that are considered to be "reserved" and can't be used Mako has a few names that are considered to be "reserved" and can't be used
as variable names. As of 0.7, Mako raises an error if these words are found as variable names. As of 0.7, Mako raises an error if these words are found
passed to the template as context arguments, whereas in previous versions they'd be silently passed to the template as context arguments, whereas in previous versions they'd be silently
ignored or lead to other error messages. ignored or lead to other error messages.
* ``context`` - see :ref:`context` * ``context`` - see :ref:`context`
* ``UNDEFINED`` - see :ref:`context_vars`
* ``loop`` - see :ref:`loop_context`. Note this can be disabled for legacy templates * ``loop`` - see :ref:`loop_context`. Note this can be disabled for legacy templates
via the ``enable_loop=False`` argument; see :ref:`migrating_loop`. via the ``enable_loop=False`` argument; see :ref:`migrating_loop`.
......
...@@ -17,7 +17,7 @@ MAGIC_NUMBER = 8 ...@@ -17,7 +17,7 @@ MAGIC_NUMBER = 8
# names which are hardwired into the # names which are hardwired into the
# template and are not accessed via the # template and are not accessed via the
# context itself # context itself
RESERVED_NAMES = set(['context', 'loop']) RESERVED_NAMES = set(['context', 'loop', 'UNDEFINED'])
def compile(node, def compile(node,
uri, uri,
......
...@@ -747,7 +747,38 @@ class UndefinedVarsTest(TemplateTest): ...@@ -747,7 +747,38 @@ class UndefinedVarsTest(TemplateTest):
result_lines(t.render(t="T")), result_lines(t.render(t="T")),
['t is: T', 'a,b,c'] ['t is: T', 'a,b,c']
) )
class ReservedNameTest(TemplateTest):
def test_names_on_context(self):
for name in ('context', 'loop', 'UNDEFINED'):
assert_raises_message(
exceptions.NameConflictError,
r"Reserved words passed to render\(\): %s" % name,
Template("x").render, **{name:'foo'}
)
def test_names_in_template(self):
for name in ('context', 'loop', 'UNDEFINED'):
assert_raises_message(
exceptions.NameConflictError,
r"Reserved words declared in template: %s" % name,
Template, "<%% %s = 5 %%>" % name
)
def test_exclude_loop_context(self):
self._do_memory_test(
"loop is ${loop}",
"loop is 5",
template_args=dict(loop=5),
enable_loop=False
)
def test_exclude_loop_template(self):
self._do_memory_test(
"<% loop = 12 %>loop is ${loop}",
"loop is 12",
enable_loop=False
)
class ControlTest(TemplateTest): class ControlTest(TemplateTest):
def test_control(self): def test_control(self):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment