# -*- coding: utf-8 -*- <%inherit file="content_layout.html"/> <%page args="toc, extension, paged"/> <%namespace name="formatting" file="formatting.html"/> <%namespace name="nav" file="nav.html"/> <%def name="title()">Mako Documentation - The Mako Runtime Environment <%! filename = 'runtime' %> ## This file is generated. Edit the .txt files instead of this one. <%call expr="formatting.section(path='runtime',paged=paged,extension=extension,toc=toc)">

This section describes a little bit about the objects and built-in functions that are available in templates.

<%call expr="formatting.section(path='runtime_context',paged=paged,extension=extension,toc=toc)">

The <%text filter='h'>Context is the central object that is created when a template is first executed, and is responsible for handling all communication with the outside world. This includes two major components, one of which is the output buffer, which is a file-like object such as Python's <%text filter='h'>StringIO or similar, and the other a dictionary of variables that can be freely referenced within a template; this dictionary is a combination of the arguments sent to the <%text filter='h'>template.render() function and some built-in variables provided by Mako's runtime environment.

<%call expr="formatting.section(path='runtime_context_buffer',paged=paged,extension=extension,toc=toc)">

The buffer is stored within the <%text filter='h'>Context, and writing to it is achieved by calling <%text filter='h'>context.write(). You usually don't need to care about this as all text within a template, as well as all expressions provided by <%text filter='h'>${}, automatically send everything to this method. The cases you might want to be aware of its existence are if you are dealing with various filtering/buffering scenarios, which are described in <%call expr="nav.toclink(path='filtering',paged=paged,extension=extension,toc=toc)">, or if you want to programmatically send content to the output stream, such as within a <%text filter='h'><% %> block.

<%call expr="formatting.code()"><%text><% context.write("some programmatic text") %>

The actual buffer may or may not be the original buffer sent to the <%text filter='h'>Context object, as various filtering/caching scenarios may "push" a new buffer onto the context's underlying buffer stack. For this reason, just stick with <%text filter='h'>context.write() and content will always go to the topmost buffer.

<%call expr="formatting.section(path='runtime_context_variables',paged=paged,extension=extension,toc=toc)">

When your template is compiled into a Python module, the body content is enclosed within a Python function called <%text filter='h'>render_body. Other top-level defs defined in the template are defined within their own function bodies which are named after the def's name with the prefix <%text filter='h'>render_ (i.e. <%text filter='h'>render_mydef). One of the first things that happens within these functions is that all variable names that are referenced within the function which are not defined in some other way (i.e. such as via assignment, module level imports, etc.) are pulled from the <%text filter='h'>Context object's dictionary of variables. This is how you're able to freely reference variable names in a template which automatically correspond to what was passed into the current <%text filter='h'>Context.

Another facet of the <%text filter='h'>Context is that its dictionary of variables is immutable. Whatever is set when <%text filter='h'>template.render() is called is what stays. Of course, since its Python, you can hack around this and change values in the context's internal dictionary, but this will probably will not work as well as you'd think. The reason for this is that Mako in many cases creates copies of the <%text filter='h'>Context object, which get sent to various elements of the template and inheriting templates used in an execution. So changing the value in your local <%text filter='h'>Context will not necessarily make that value available in other parts of the template's execution. Examples of where Mako creates copies of the <%text filter='h'>Context include within top-level def calls from the main body of the template (the context is used to propagate locally assigned variables into the scope of defs; since in the template's body they appear as inlined functions, Mako tries to make them act that way), and within an inheritance chain (each template in an inheritance chain has a different notion of <%text filter='h'>parent and <%text filter='h'>next, which are all stored in unique <%text filter='h'>Context instances).

Running the template looks like:

<%call expr="formatting.code(syntaxtype='python')"><%text> output = template.render(attributes={})

Within a template, just reference the dictionary:

<%call expr="formatting.code()"><%text><% attributes['foo'] = 'bar' %> 'foo' attribute is: ${attributes['foo']} <%call expr="formatting.section(path='runtime_context_accessors',paged=paged,extension=extension,toc=toc)">

Significant members off of <%text filter='h'>Context include:

<%call expr="formatting.section(path='runtime_builtins',paged=paged,extension=extension,toc=toc)">

A one-stop shop for all the names Mako defines. Most of these names are instances of <%text filter='h'>Namespace, which are described in the next section, <%call expr="nav.toclink(path='namespaces',paged=paged,extension=extension,toc=toc)">. Also, most of these names other than <%text filter='h'>context and <%text filter='h'>UNDEFINED are also present within the <%text filter='h'>Context itself.