mako-语法
变量
变量替换,可以做计算
this is x: ${x}
pythagorean theorem:  ${pow(x,2) + pow(y,2)}
变量在render中传入
# -*- coding:utf-8 -*-
from mako.template import Template
mytemplate = Template("hello, ${name}!")
print(mytemplate.render(name="jack"))
转义
${"this is some text" | u}
u代表rul转义
h代表html转义
x代表xml转义
trim代表trim函数
参考:http://docs.makotemplates.org/en/latest/filtering.html
控制结构
if
% if x==5:
    this is some output
% endif
for
% for a in ['one', 'two', 'three', 'four', 'five']:
    % if a[0] == 't':
    its two or three
    % elif a[0] == 'f':
    four/five
    % else:
    one
    % endif
% endfor
循环上下文
<ul>
% for a in ("one", "two", "three"):
    <li>Item ${loop.index}: ${a}</li>
% endfor
</ul>
提供循环的额外信息:
http://docs.makotemplates.org/en/latest/runtime.html#loop-context
注释
单行
## this is a comment.
多行
<%doc>
    these are comments
    more comments
</%doc>
行拼接
here is a line that goes onto \
another line.
python块
用 <% %> 可以写python块
this is a template
<%
    x = db.get_resource('foo')
    y = [z.element for z in x if x.frobnizzle==5]
%>
% for elem in y:
    element: ${elem}
% endfor
模块级的代码
this is a template
<%
    x = db.get_resource('foo')
    y = [z.element for z in x if x.frobnizzle==5]
%>
% for elem in y:
    element: ${elem}
% endfor
Tags
类似xml的tag属性
<%include file="foo.txt"/>
<%def name="foo" buffered="True">
    this is a def
</%def>
有些属性需要参数,因为可以在tag属性中插入表达式${}
<%include file="/foo/bar/${myfile}.txt"/>
<%page>
<%page args="x, y, z='default'"/>
<%page cached="True" cache_type="memory"/>
暂时先记住,一个模板里只有一个 <%page>标签
<%include>
%include可以插入其他文件的内容
<%include file="header.html"/>
    hello world
<%include file="footer.html"/>
可以使用<%page>输入参数
<%include file="toolbar.html" args="current_section='members', username='ed'"/>
<%def>
%def标签定义了一个python函数
<%def name="myfunc(x)">
    this is myfunc, x is ${x}
</%def>
${myfunc(7)}
参考:
http://docs.makotemplates.org/en/latest/defs.html
<%block>
可以是匿名的
<%block filter="h">
    some <html> stuff.
</%block>
block可以继承
<html>
    <body>
    <%block name="header">
        <h2><%block name="title"/></h2>
    </%block>
    ${self.body()}
    </body>
</html>
详细参考:
http://docs.makotemplates.org/en/latest/defs.html#blocks
http://docs.makotemplates.org/en/latest/inheritance.html
<%namespace>
和python的import类似
<%namespace file="functions.html" import="*"/>
namespcae
http://docs.makotemplates.org/en/latest/namespaces.html
<%inherit>
<%inherit file="base.html"/>
更多参考:
http://docs.makotemplates.org/en/latest/inheritance.html
<%nsname:defname>
用户定义的tag可以
使用<%
分别使用 <%call>标签
<%mynamespace:somedef param="some value">
    this is the body
</%mynamespace:somedef>
为了创建一个特定的标签,能够接收body,参考:
http://docs.makotemplates.org/en/latest/defs.html#defs-with-content
<%call>
参考
http://docs.makotemplates.org/en/latest/defs.html#defs-with-content
<%doc>
处理多行注释
<%doc>
    these are comments
    more comments
</%doc>
<%text>
延迟
<%text filter="h">
    heres some fake mako ${syntax}
    <%def name="x()">${x}</%def>
</%text>
Exiting Early from a Template
% if not len(records):
    No records found.
    <% return STOP_RENDERING %>
% endif
<%
    if not len(records):
        return STOP_RENDERING
%>
旧版本,空串也能代替STOP_RENDERING符号
<% return '' %>
 
                    
                     
                    
                 
                    
                
 
 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号