class Template(object):
Known subclasses: mako.template.DefTemplate
, mako.template.ModuleTemplate
Represents a compiled template.
.Template
includes a reference to the original
template source (via the .source
attribute)
as well as the source code of the
generated Python module (i.e. the .code
attribute),
as well as a reference to an actual Python module.
.Template
is constructed using either a literal string
representing the template text, or a filename representing a filesystem
path to a source file.
Parameters | |
text | textual template source. This argument is mutually exclusive versus the filename parameter. |
filename | filename of the source template. This argument is mutually exclusive versus the text parameter. |
buffer_filters | string list of filters to be applied to the output of %defs which are buffered, cached, or otherwise filtered, after all filters defined with the %def itself have been applied. Allows the creation of default expression filters that let the output of return-valued %defs "opt out" of that filtering via passing special attributes or objects. |
bytestring_passthrough | When True, and output_encoding is
set to None, and
New in version 0.4: Added to provide the same behavior as that of the previous series.
|
cache_args | Dictionary of cache configuration arguments that
will be passed to the .CacheImpl . See :ref:`caching_toplevel`. |
cache_dir |
Deprecated since version 0.6: Use the 'dir' argument in the cache_args dictionary.
See :ref:`caching_toplevel`.
|
cache_enabled | Boolean flag which enables caching of this template. See :ref:`caching_toplevel`. |
cache_impl | String name of a .CacheImpl caching
implementation to use. Defaults to 'beaker'. |
cache_type |
Deprecated since version 0.6: Use the 'type' argument in the cache_args dictionary.
See :ref:`caching_toplevel`.
|
cache_url |
Deprecated since version 0.6: Use the 'url' argument in the cache_args dictionary.
See :ref:`caching_toplevel`.
|
default_filters | List of string filter names that will be applied to all expressions. See :ref:`filtering_default_filters`. |
disable_unicode | Disables all awareness of Python Unicode objects. See :ref:`unicode_disabled`. |
enable_loop | When True, enable the loop context variable. This can be set to False to support templates that may be making usage of the name "loop". Individual templates can re-enable the "loop" context by placing the directive enable_loop="True" inside the <%page> tag -- see :ref:`migrating_loop`. |
encoding_errors | Error parameter passed to encode() when string encoding is performed. See :ref:`usage_unicode`. |
error_handler | Python callable which is called whenever compile or runtime exceptions occur. The callable is passed the current context as well as the exception. If the callable returns True, the exception is considered to be handled, else it is re-raised after the function completes. Is used to provide custom error-rendering functions. See Also :paramref:`.Template.include_error_handler` - include-specific error handler function |
format_exceptions | if True, exceptions which occur during
the render phase of this template will be caught and
formatted into an HTML error page, which then becomes the
rendered result of the .render call. Otherwise,
runtime exceptions are propagated outwards. |
imports | String list of Python statements, typically individual "import" lines, which will be placed into the module level preamble of all generated Python modules. See the example in :ref:`filtering_default_filters`. |
future_imports | String list of names to import from __future__ .
These will be concatenated into a comma-separated string and inserted
into the beginning of the template, e.g. futures_imports=['FOO',
'BAR'] results in from __future__ import FOO, BAR. If you're
interested in using features like the new division operator, you must
use future_imports to convey that to the renderer, as otherwise the
import will not appear as the first executed statement in the generated
code and will therefore not have the desired effect. |
include_error_handler | An error handler that runs when this template is included within another one via the <%include> tag, and raises an error. Compare to the :paramref:`.Template.error_handler` option.
New in version 1.0.6.
See Also :paramref:`.Template.error_handler` - top-level error handler function |
input_encoding | Encoding of the template's source code. Can be used in lieu of the coding comment. See :ref:`usage_unicode` as well as :ref:`unicode_toplevel` for details on source encoding. |
lookup | a .TemplateLookup instance that will be used
for all file lookups via the <%namespace>,
<%include>, and <%inherit> tags. See
:ref:`usage_templatelookup`. |
module_directory | Filesystem location where generated Python module files will be placed. |
module_filename | Overrides the filename of the generated Python module file. For advanced usage only. |
module_writer | A callable which overrides how the Python module is written entirely. The callable is passed the encoded source content of the module and the destination path to be written to. The default behavior of module writing uses a tempfile in conjunction with a file move in order to make the operation atomic. So a user-defined module writing function that mimics the default behavior would be: import tempfile import os import shutil def module_writer(source, outputpath): (dest, name) = \\ tempfile.mkstemp( dir=os.path.dirname(outputpath) ) os.write(dest, source) os.close(dest) shutil.move(name, outputpath) from mako.template import Template mytemplate = Template( filename="index.html", module_directory="/path/to/modules", module_writer=module_writer ) The function is provided for unusual configurations where certain platform-specific permissions or other special steps are needed. |
output_encoding | The encoding to use when .render
is called.
See :ref:`usage_unicode` as well as :ref:`unicode_toplevel`. |
preprocessor | Python callable which will be passed the full template source before it is parsed. The return result of the callable will be used as the template source code. |
lexer_cls | A
New in version 0.7.4.
|
strict_undefined | Replaces the automatic usage of
UNDEFINED for any undeclared variables not located in
the
New in version 0.3.6.
|
uri | string URI or other identifier for this template.
If not provided, the uri is generated from the filesystem
path, or from the in-memory identity of a non-file-based
template. The primary usage of the uri is to provide a key
within .TemplateLookup , as well as to generate the
file path of the generated Python module file, if
module_directory is specified. |
Method | get_def |
Return a def of this template as a .DefTemplate . |
Method | list_defs |
return a list of defs in the template. |
Method | render |
Render the output of this template as a string. |
Method | render_context |
Render this .Template with the given context. |
Method | render_unicode |
Render the output of this template as a unicode object. |
Method | __init__ |
Undocumented |
Method | _compile_from_file |
Undocumented |
Method | _get_def_callable |
Undocumented |
Method | _setup_cache_args |
Undocumented |
Method | has_def |
Undocumented |
Instance Variable | _code |
Undocumented |
Instance Variable | _source |
Undocumented |
Instance Variable | buffer_filters |
Undocumented |
Instance Variable | bytestring_passthrough |
Undocumented |
Instance Variable | cache_args |
Undocumented |
Instance Variable | cache_enabled |
Undocumented |
Instance Variable | cache_impl |
Undocumented |
Instance Variable | callable_ |
Undocumented |
Instance Variable | default_filters |
Undocumented |
Instance Variable | disable_unicode |
Undocumented |
Instance Variable | enable_loop |
Undocumented |
Instance Variable | encoding_errors |
Undocumented |
Instance Variable | error_handler |
Undocumented |
Instance Variable | filename |
Undocumented |
Instance Variable | format_exceptions |
Undocumented |
Instance Variable | future_imports |
Undocumented |
Instance Variable | imports |
Undocumented |
Instance Variable | include_error_handler |
Undocumented |
Instance Variable | input_encoding |
Undocumented |
Instance Variable | lexer_cls |
Undocumented |
Instance Variable | lookup |
Undocumented |
Instance Variable | module |
Undocumented |
Instance Variable | module_directory |
Undocumented |
Instance Variable | module_id |
Undocumented |
Instance Variable | module_writer |
Undocumented |
Instance Variable | output_encoding |
Undocumented |
Instance Variable | preprocessor |
Undocumented |
Instance Variable | strict_undefined |
Undocumented |
Instance Variable | uri |
Undocumented |
Property | cache |
Undocumented |
Property | cache_dir |
Undocumented |
Property | cache_type |
Undocumented |
Property | cache_url |
Undocumented |
Property | code |
Return the module source code for this .Template . |
Property | last_modified |
Undocumented |
Property | reserved_names |
Undocumented |
Property | source |
Return the template source code for this .Template . |
mako.template.DefTemplate
.DefTemplate
.Render the output of this template as a string.
If the template specifies an output encoding, the string
will be encoded accordingly, else the output is raw (raw
output uses cStringIO
and can't handle multibyte
characters). A .Context
object is created corresponding
to the given data. Arguments that are explicitly declared
by this template's internal rendering method are also
pulled from the given *args, **data members.
Render this .Template
with the given context.
The data is written to the context's buffer.
mako.template.DefTemplate
, mako.template.ModuleTemplate
Undocumented
Undocumented
mako.template.DefTemplate
, mako.template.ModuleTemplate
Undocumented
mako.template.DefTemplate
, mako.template.ModuleTemplate
Undocumented
mako.template.DefTemplate
, mako.template.ModuleTemplate
Undocumented
mako.template.DefTemplate
, mako.template.ModuleTemplate
Undocumented
mako.template.DefTemplate
, mako.template.ModuleTemplate
Undocumented