# -*- 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 - Namespaces <%! filename = 'namespaces' %> ## This file is generated. Edit the .txt files instead of this one. <%call expr="formatting.section(path='namespaces',paged=paged,extension=extension,toc=toc)">

Namespaces are used to organize groups of components into categories, and also to "import" components from other files.

If the file <%text filter='h'>components.html defines these two components:

<%call expr="formatting.code()"><%text>## components.html <%def name="comp1()"> this is comp1 <%def name="comp2(x)"> this is comp2, x is ${x}

You can make another file, for example <%text filter='h'>index.html, that pulls those two components into a namespace called <%text filter='h'>comp:

<%call expr="formatting.code()"><%text>## index.html <%namespace name="comp" file="components.html"/> Heres comp1: ${comp.comp1()} Heres comp2: ${comp.comp2(x=5)}

The <%text filter='h'>comp variable above is an instance of <%text filter='h'>mako.runtime.Namespace, a proxy object which delivers method calls to the underlying template callable using the current context.

<%text filter='h'><%namespace> also provides an <%text filter='h'>import attribute which can be used to pull the names into the local namespace, removing the need to call it via the ".". When <%text filter='h'>import is used, the <%text filter='h'>name attribute is optional.

<%call expr="formatting.code()"><%text><%namespace file="components.html" import="comp1, comp2"/> Heres comp1: ${comp1()} Heres comp2: ${comp2(x=5)}

<%text filter='h'>import also supports the "*" operator:

<%call expr="formatting.code()"><%text><%namespace file="components.html" import="*"/> Heres comp1: ${comp1()} Heres comp2: ${comp2(x=5)}

The names imported by the <%text filter='h'>import attribute take precedence over any names that exist within the current context.

Note - in current versions of Mako, usage of "import='*'" is known to decrease performance of the template. This will be fixed in a future release.

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

There are essentially four ways to call a function from a namespace.

The "expression" format, as described previously. Namespaces are just Python objects with functions on them, and can be used in expressions like any other function:

<%call expr="formatting.code()"><%text>${mynamespace.somefunction('some arg1', 'some arg2', arg3='some arg3', arg4='some arg4')}

Synonymous with the "expression" format is the "custom tag" format, when a "closed" tag is used. This format, introduced in Mako 0.2.3, allows the usage of a "custom" Mako tag, with the function arguments passed in using named attributes:

<%call expr="formatting.code()"><%text><%mynamespace:somefunction arg1="some arg1" arg2="some arg2" arg3="some arg3" arg4="some arg4"/>

When using tags, the values of the arguments are taken as literal strings by default. To embed Python expressions as arguments, use the embedded expression format:

<%call expr="formatting.code()"><%text><%mynamespace:somefunction arg1="${someobject.format()}" arg2="${somedef(5, 12)}"/>

The "custom tag" format is intended mainly for namespace functions which recognize body content, which in Mako is known as a "def with embedded content":

<%call expr="formatting.code()"><%text><%mynamespace:somefunction arg1="some argument" args="x, y"> Some record: ${x}, ${y}

The "classic" way to call defs with embedded content is the <%text filter='h'><%call> tag:

<%call expr="formatting.code()"><%text><%call expr="mynamespace.somefunction(arg1='some argument')" args="x, y"> Some record: ${x}, ${y}

For information on how to construct defs that embed content from the caller, see <%call expr="nav.toclink(path='defs_defswithcontent',paged=paged,extension=extension,toc=toc)">.

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

Namespaces can also import regular Python functions from modules. These callables need to take at least one argument, <%text filter='h'>context:

A module file <%text filter='h'>some/module.py might contain the callable:

<%call expr="formatting.code(syntaxtype='python')"><%text> def my_tag(context): context.write("hello world") return ''

A template can use this module via:

<%call expr="formatting.code()"><%text><%namespace name="hw" module="some.module"/> ${hw.my_tag()}

Note that the <%text filter='h'>context argument is not needed in the call; the <%text filter='h'>namespace tag creates a locally-scoped callable which takes care of it. The <%text filter='h'>return '' is so that the def does not dump a <%text filter='h'>None into the output stream - the return value of any def is rendered after the def completes, in addition to whatever was passed to <%text filter='h'>context.write() within its body.

If your def is to be called in an "embedded content" context, that is as described in <%call expr="nav.toclink(path='defs_defswithcontent',paged=paged,extension=extension,toc=toc)">, you should use the <%text filter='h'>@supports_caller decorator, which will ensure that Mako will ensure the correct "caller" variable is available when your def is called, supporting embedded content:

<%call expr="formatting.code(syntaxtype='python')"><%text> from mako.runtime import supports_caller @supports_caller def my_tag(context): context.write("
") context['caller'].body() context.write("
") return ''

Capturing of output is available as well, using the outside-of-templates version of the <%text filter='h'>capture() function, which accepts the "context" as its first argument:

<%call expr="formatting.code(syntaxtype='python')"><%text> from mako.runtime import supports_caller, capture @supports_caller def my_tag(context): return "
%s
" % \ capture(context, context['caller'].body, x="foo", y="bar") <%call expr="formatting.section(path='namespaces_declaring',paged=paged,extension=extension,toc=toc)">

The <%text filter='h'><%namespace> tag supports the definition of <%text filter='h'><%defs> directly inside the tag. These defs become part of the namespace like any other function, and will override the definitions pulled in from a remote template or module:

<%call expr="formatting.code()"><%text>## define a namespace <%namespace name="stuff"> <%def name="comp1()"> comp1 ## then call it ${stuff.comp1()} <%call expr="formatting.section(path='namespaces_body',paged=paged,extension=extension,toc=toc)">

Every namespace that is generated from a template contains a method called <%text filter='h'>body(). This method corresponds to the main body of the template, and plays its most important roles when using inheritance relationships as well as def-calls-with-content.

Since the <%text filter='h'>body() method is available from a namespace just like all the other defs defined in a template, what happens if you send arguments to it ? By default, the <%text filter='h'>body() method accepts no positional arguments, and for usefulness in inheritance scenarios will by default dump all keyword arguments into a dictionary called <%text filter='h'>pageargs. But if you actually want to get at the keyword arguments, Mako recommends you define your own argument signature explicitly. You do this via using the <%text filter='h'><%page> tag:

<%call expr="formatting.code()"><%text><%page args="x, y, someval=8, scope='foo', **kwargs"/>

A template which defines the above signature requires that the variables <%text filter='h'>x and <%text filter='h'>y are defined, defines default values for <%text filter='h'>someval and <%text filter='h'>scope, and sets up <%text filter='h'>**kwargs to receive all other keyword arguments. If <%text filter='h'>**kwargs or similar is not present, the argument <%text filter='h'>**pageargs gets tacked on by Mako. When the template is called as a top-level template (i.e. via <%text filter='h'>template.render()) or via the <%text filter='h'><%include> tag, the values for these arguments will be pulled from the <%text filter='h'>Context. In all other cases, i.e. via calling the <%text filter='h'>body() method, the arguments are taken as ordinary arguments from the method call. So above, the body might be called as:

<%call expr="formatting.code()"><%text>${self.body(5, y=10, someval=15, delta=7)}

The <%text filter='h'>Context object also supplies a <%text filter='h'>kwargs accessor, for cases when youd like to pass along whatever is in the context to a <%text filter='h'>body() callable:

<%call expr="formatting.code()"><%text>${next.body(**context.kwargs)}

The usefulness of calls like the above become more apparent when one works with inheriting templates. For more information on this, as well as the meanings of the names <%text filter='h'>self and <%text filter='h'>next, see <%call expr="nav.toclink(path='inheritance',paged=paged,extension=extension,toc=toc)">.

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

The <%text filter='h'>Namespace class includes helpful accessors and methods:

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

The namespace is so great that Mako gives your template one (or two) for free. The names of these namespaces are <%text filter='h'>local and <%text filter='h'>self. Other built-in namespaces include <%text filter='h'>parent and <%text filter='h'>next, which are optional and are described in <%call expr="nav.toclink(path='inheritance',paged=paged,extension=extension,toc=toc)">.

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

The <%text filter='h'>local namespace is basically the namespace for the currently executing template. This means that all of the top level defs defined in your template, as well as your template's <%text filter='h'>body() function, are also available off of the <%text filter='h'>local namespace.

The <%text filter='h'>local namespace is also where properties like <%text filter='h'>uri, <%text filter='h'>filename, and <%text filter='h'>module and the <%text filter='h'>get_namespace method can be particularly useful.

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

The <%text filter='h'>self namespace, in the case of a template that does not use inheritance, is synonomous with <%text filter='h'>local. If inheritance is used, then <%text filter='h'>self references the topmost template in the inheritance chain, where it is most useful for providing the ultimate form of various "method" calls which may have been overridden at various points in an inheritance chain. See <%call expr="nav.toclink(path='inheritance',paged=paged,extension=extension,toc=toc)">.

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

The <%text filter='h'><%namespace> tag includes an optional attribute <%text filter='h'>inheritable="True", which will cause the namespace to be attached to the <%text filter='h'>self namespace. Since <%text filter='h'>self is globally available throughout an inheritance chain (described in the next section), all the templates in an inheritance chain can get at the namespace imported in a super-template via <%text filter='h'>self.

<%call expr="formatting.code()"><%text>## base.html <%namespace name="foo" file="foo.html" inheritable="True"/> ${next.body()} ## somefile.html <%inherit file="base.html"/> ${self.foo.bar()}

This allows a super-template to load a whole bunch of namespaces that its inheriting templates can get to, without them having to explicitly load those namespaces themselves.

The <%text filter='h'>import="*" part of the <%text filter='h'><%namespace> tag doesn't yet interact with the <%text filter='h'>inheritable flag, so currently you have to use the explicit namespace name off of <%text filter='h'>self, followed by the desired function name. But more on this in a future release.