4. 语法 a. 表达式,和jsp,velocity很像:
this is x: ${x}
但比velocity智能,数学运算很方便有木有:
${pow(x,2) + pow(y,2)}
the contents within the ${} tag are evaluated by Python directly, so full expressions are OK.
还有比java先进的,看看这个:
${"this is some text" | u}
| 后面这个u是什么意思?
Mako 内置了多个escaping filter ,包括 HTML, URL, XML escaping,trim(),这些filter可以通过| 后面跟表达式来表示。
u : URL escaping, provided by urllib.quote_plus(string.encode('utf-8'))
h : HTML escaping, provided by markupsafe.escape(string)
x : XML escaping
trim : whitespace trimming, provided by string.strip()
entity : produces HTML entity references for applicable strings, derived from htmlentitydefs
unicode (str on Python 3): produces a Python unicode string (this function is applied by default)
decode.<some encoding> : decode input into a Python unicode with the specified encoding
n : disable all default filtering; only filters specified in the local expression tag will be applied.
多个filter通过逗号间隔:
${" <tag>some value</tag> " | h,trim}
上面这段code将被解析成: <tag>some value</tag> 更多: http://docs.makotemplates.org/en/latest/filtering.html b. 流程控制
% if x==5:
this is some output
% endif
% 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
c. python block:
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