Python Mako语法

博客原文地址http://www.yeolar.com/note/2012/08/28/mako-syntax/#id29

Flask 下使用Mako

pip install Flask-Mako
Flask-Mako 文档地址

#coding : utf-8
from flask import Flask
from flask.ext.mako import MakoTemplates
from flask.ext.mako import render_template

app = Flask(__name__, template_folder='templates')
#这一句不写后面直接用render_template会报错
mako = MakoTemplates(app)


@app.route('/hello/')
def hello_mako():
    return render_template('hello.html',
                           name='mako',
                           x=5,
                           y=3)

app.run(debug=True)

表达式 使用${ } hello, ${name}

在表达式中可以直接使用python内置函数
${flaot(a)}
${str(price)}

可以使用过滤函数 u h trim
${"some message" | u}
some+message

${"this include html tag <html> <p>"| h}
this include html tag <html><p>
${"this no include html tag <html> <p>"}
this no include html tag

${" <tag>some value</tag> " | h,trim}
&lt;tag&gt;some value&lt;/tag&gt;

使用控制语句 if

% if condition:
    condition is true
% endif

使用循环语句 for while

% for i in range(10):
    % if i%2 == 0:
         even number
    % else:
        odd number
    % endif
% endfor 

开头转义%
%% start with %
% start with %

循环时有个特殊的变量loop.index 指向循环位置

<ul>
% for i in range(10):
    <li>Item ${loop.index}: ${i}</li>
% endfor
</ul>

注释
1. ## this is a comment.
2. 使用标签注释
<%doc>
more comments
</%doc>

连接下一行
here is a line that goes onto \
another line.

here is a line that goes onto another line.

Python代码块

this is a template
<%
    print('testing')
    a = pow(x, 2)
%>

下面相当于全局 一般用于导入模块和定义一些函数

<%!
    import random

    def get_randnumber():
        return random.randInt(1, 100)
%>

标签

<%include file="foo.txt"/>

<%def name="foo" buffered="True">
    this is a def
</%def>

导入文件可以存在变量
<%include file="/foo/bar/${filename}.txt />

<%page args="x, y, z='default'"/>
<%page cached="True" cache_type="memory"/>

<%include file="header.html"/>

    hello world

<%include file="footer.html"/>

它还接受 <%page> 标签的参数,应用到导入的模板上

<%include file="toolbar.html" args="current_section='members', username='ed'"/>

%def 标签定义一个Python函数,函数可以在模板的其他位置调用。基本的思想很简单:


<%def name="myfunc(x)">
    this is myfunc, x is ${x}
</%def>

${myfunc(7)}

${myfunc(7)}也可以放到<%def … >前面,也会正确执行
导入其他文件中的函数
<%namespace name="mystuff" file="mystuff.html"/>
${mystuff.somedef(x=5,y=7)}
<%namespace>也支持python类似的语法
<%namespace file="mystuff.html" import="foo, bar"/>

<%namespace file="mystuff.html" import="*"/>
<%block> 标签和 %def 类似,但它会在根作用域立即执行,而且可以是匿名的:

<%block filter="h">
    some <html> stuff.
</%block>

借鉴了Jinja2的块,有名字的块实现了一种很方便的继承方法:

<html>
    <body>
    <%block name="header">
        <h2><%block name="title"/></h2>
    </%block>
    ${self.body()}
    </body>
</html>

<%inherit>
通过继承可以实现模板的继承链。它和其他模板语言的概念类似。

<%inherit file="base.html"/>

当使用 %inherit 标签时,控制首先被传递到继承模板的顶层模板,由它来决定如何处理调用部分。Mako在这部分实现的非常灵活,包括动态继承、内容封装、多态方法调用.

<%nsname:defname>
可以通过 <%:> 来在一个名字空间中自定义一个标签。它的单标签和双标签形式分别对应行内表达式和 <%call> 标签。

<%mynamespace:somedef param="some value">
    this is the body
</%mynamespace:somedef>
<%call>

%call 标签是用户定义标签的传统形式,大致和上面的 <%namespacename:defname> 等价
%doc 标签处理多行注释:

<%doc>
    these are comments
    more comments
</%doc>

<%text>
该标签使Mako词法分析器跳过该部分的处理,返回整个内容。主要用于写Mako的文档:

<%text filter="h">
    heres some fake mako ${syntax}
    <%def name="x()">${x}</%def>
</%text>

从模板中提前返回
有时你想在模板或 <%def> 方法的中间停止处理,只用当前得到的结果。可以在Python代码块中使用 return 语句来实现。

% if not len(records):
    No records found.
    <% return %>
% endif

或:

<%
    if not len(records):
        return
%>
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值