Software: Apache. PHP/5.3.29 uname -a: Linux tardis23.nocplanet.net 4.18.0-553.lve.el8.x86_64 #1 SMP Mon May 27 15:27:34 UTC 2024
Safe-mode: OFF (not secure) /opt/alt/python37/share/doc/alt-python37-mako/doc/ drwxr-xr-x | |
| Viewing file: Select action/file-type: Mako 1.1.0 Documentation
Search:
Release: 1.1.0
Mako 1.1.0 Documentation
»
Defs and Blocks
Defs and BlocksDefs and Blocks¶
Using Defs¶The <%def name="hello()">
hello world
</%def>To invoke the the def: ${hello()}If the All defs, top level or not, have access to the current
contextual namespace in exactly the same way their containing
template does. Suppose the template below is executed with the
variables Hello there ${username}, how are ya. Lets see what your account says:
${account()}
<%def name="account()">
Account for ${username}:<br/>
% for row in accountdata:
Value: ${row}<br/>
% endfor
</%def>The Since defs are just Python functions, you can define and pass arguments to them as well: ${account(accountname='john')}
<%def name="account(accountname, type='regular')">
account name: ${accountname}, type: ${type}
</%def>When you declare an argument signature for your def, they are
required to follow normal Python conventions (i.e., all
arguments are required except keyword arguments with a default
value). This is in contrast to using context-level variables,
which evaluate to Calling Defs from Other Files¶Top level The remote To import another template, use the <%namespace name="mystuff" file="mystuff.html"/>The above tag adds a local variable Then, just call the defs off of ${mystuff.somedef(x=5,y=7)}The <%namespace file="mystuff.html" import="foo, bar"/>This is just a quick intro to the concept of a namespace, which is a central Mako concept that has its own chapter in these docs. For more detail and examples, see Namespaces. Calling Defs Programmatically¶You can call defs programmatically from any from mako.template import Template
template = Template("""
<%def name="hi(name)">
hi ${name}!
</%def>
<%def name="bye(name)">
bye ${name}!
</%def>
""")
print(template.get_def("hi").render(name="ed"))
print(template.get_def("bye").render(name="ed"))Defs within Defs¶The def model follows regular Python rules for closures.
Declaring <%def name="mydef()">
<%def name="subdef()">
a sub def
</%def>
i'm the def, and the subcomponent is ${subdef()}
</%def>Just like Python, names that exist outside the inner <%
x = 12
%>
<%def name="outer()">
<%
y = 15
%>
<%def name="inner()">
inner, x is ${x}, y is ${y}
</%def>
outer, x is ${x}, y is ${y}
</%def>Assigning to a name inside of a def declares that name as local to the scope of that def (again, like Python itself). This means the following code will raise an error: <%
x = 10
%>
<%def name="somedef()">
## error !
somedef, x is ${x}
<%
x = 27
%>
</%def>…because the assignment to Calling a Def with Embedded Content and/or Other Defs¶A flip-side to def within def is a def call with content. This is where you call a def, and at the same time declare a block of content (or multiple blocks) that can be used by the def being called. The main point of such a call is to create custom, nestable tags, just like any other template language’s custom-tag creation system – where the external tag controls the execution of the nested tags and can communicate state to them. Only with Mako, you don’t have to use any external Python modules, you can define arbitrarily nestable tags right in your templates. To achieve this, the target def is invoked using the form
When the target def is invoked, a variable <%def name="buildtable()">
<table>
<tr><td>
${caller.body()}
</td></tr>
</table>
</%def>
<%self:buildtable>
I am the table body.
</%self:buildtable>This produces the output (whitespace formatted): <table>
<tr><td>
I am the table body.
</td></tr>
</table>Using the older <%def name="buildtable()">
<table>
<tr><td>
${caller.body()}
</td></tr>
</table>
</%def>
<%call expr="buildtable()">
I am the table body.
</%call>The <%def name="lister(count)">
% for x in range(count):
${caller.body()}
% endfor
</%def>
<%self:lister count="${3}">
hi
</%self:lister>Produces: hi
hi
hiNotice above we pass A custom “conditional” tag: <%def name="conditional(expression)">
% if expression:
${caller.body()}
% endif
</%def>
<%self:conditional expression="${4==4}">
i'm the result
</%self:conditional>Produces: i'm the resultBut that’s not all. The <%def name="layoutdata(somedata)">
<table>
% for item in somedata:
<tr>
% for col in item:
<td>${caller.body(col=col)}</td>
% endfor
</tr>
% endfor
</table>
</%def>
<%self:layoutdata somedata="${[[1,2,3],[4,5,6],[7,8,9]]}" args="col">\
Body data: ${col}\
</%self:layoutdata>Produces: <table>
<tr>
<td>Body data: 1</td>
<td>Body data: 2</td>
<td>Body data: 3</td>
</tr>
<tr>
<td>Body data: 4</td>
<td>Body data: 5</td>
<td>Body data: 6</td>
</tr>
<tr>
<td>Body data: 7</td>
<td>Body data: 8</td>
<td>Body data: 9</td>
</tr>
</table>You don’t have to stick to calling just the <%def name="layout()">
## a layout def
<div class="mainlayout">
<div class="header">
${caller.header()}
</div>
<div class="sidebar">
${caller.sidebar()}
</div>
<div class="content">
${caller.body()}
</div>
</div>
</%def>
## calls the layout def
<%self:layout>
<%def name="header()">
I am the header
</%def>
<%def name="sidebar()">
<ul>
<li>sidebar 1</li>
<li>sidebar 2</li>
</ul>
</%def>
this is the body
</%self:layout>The above layout would produce: <div class="mainlayout">
<div class="header">
I am the header
</div>
<div class="sidebar">
<ul>
<li>sidebar 1</li>
<li>sidebar 2</li>
</ul>
</div>
<div class="content">
this is the body
</div>
</div>The number of things you can do with Using Blocks¶The New in version 0.4.1. An example of a block: <html>
<body>
<%block>
this is a block.
</%block>
</body>
</html>In the above example, we define a simple block. The block renders its content in the place that it’s defined. Since the block is called for us, it doesn’t need a name and the above is referred to as an anonymous block. So the output of the above template will be: <html>
<body>
this is a block.
</body>
</html>So in fact the above block has absolutely no effect. Its usefulness comes when we start using modifiers. Such as, we can apply a filter to our block: <html>
<body>
<%block filter="h">
<html>this is some escaped html.</html>
</%block>
</body>
</html>or perhaps a caching directive: <html>
<body>
<%block cached="True" cache_timeout="60">
This content will be cached for 60 seconds.
</%block>
</body>
</html>Blocks also work in iterations, conditionals, just like defs: % if some_condition:
<%block>condition is met</%block>
% endifWhile the block renders at the point it is defined in the template, the underlying function is present in the generated Python code only once, so there’s no issue with placing a block inside of a loop or similar. Anonymous blocks are defined as closures in the local rendering body, so have access to local variable scope: % for i in range(1, 4):
<%block>i is ${i}</%block>
% endforUsing Named Blocks¶Possibly the more important area where blocks are useful is when we
do actually give them names. Named blocks are tailored to behave
somewhat closely to Jinja2’s block tag, in that they define an area
of a layout which can be overridden by an inheriting template. In
sharp contrast to the <html>
<%block name="header">
<head>
<title>
<%block name="title">Title</%block>
</title>
</head>
</%block>
<body>
${next.body()}
</body>
</html>The above example has two named blocks “ Note above that named blocks don’t have any argument declaration the way defs do. They still implement themselves as Python functions, however, so they can be invoked additional times beyond their initial definition: <div name="page">
<%block name="pagecontrol">
<a href="">previous page</a> |
<a href="">next page</a>
</%block>
<table>
## some content
</table>
${pagecontrol()}
</div>The content referenced by To keep things sane, named blocks have restrictions that defs do not:
Using Page Arguments in Named Blocks¶A named block is very much like a top level def. It has a similar
restriction to these types of defs in that arguments passed to the
template via the <%page args="post"/>
<a name="${post.title}" />
<span class="post_prose">
<%block name="post_prose" args="post">
${post.content}
</%block>
</span>Where above, if the template is called via a directive like
Similarly, the <%block name="post_prose">
${pageargs['post'].content}
</%block>The
Previous:
Syntax
Next:
The Mako Runtime Environment
© Copyright the Mako authors and contributors.
Documentation generated using Sphinx 2.1.2
with Mako templates.
|
:: Command execute :: | |
--[ c99shell v.2.1 [PHP 7 Update] [1.12.2019] maintained by KaizenLouie and updated by cermmik | C99Shell Github (MySQL update) | Generation time: 0.1103 ]-- |