pylons mako 摘录笔记

pylons mako笔记

1.

在pylons中设置为utf-8编码

在pylons工程config/environment.py文件最后,添加

tmpl_options['mako.default_filters'] = ['decode.utf8']

2.

${}中间直接是python代码,显示其返回值

this is x: ${x}

pythagorean theorem:  ${pow(x,2) + pow(y,2)}

3.

${"this is some text" | u}

u : url编码this+is+some+text
h : html编码
x : xml编码
trim : whitespace trimming, provided by string.strip()
n : disable all default filtering; only filters specified in the local expression tag will be applied.

自定义filter
<%!
    def myescape(text):
        return "<TAG>" + text + "</TAG>"
%>


Heres some tagged text: ${"text" | myescape}

Or from any Python module:

<%!
    import myfilters
%>

Heres some tagged text: ${"text" | myfilters.tagfilter}

默认过滤器

<%page expression_filter="h"/>

Escaped text:  ${"<html>some html</html>"}

可以在配置中定义默认的filter
t = TemplateLookup(directories=['/tmp'],
    default_filters=['unicode', 'myfilter'],
    imports=['from mypackage import myfilter']

禁用其他,只用trim
${'myexpression' | n, trim}

函数也可以用

<%def name="foo()" filter="h, trim">
    <b>this is bold</b>
</%def>


4.

控制语句

% 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


直接编写python代码
<%
    x = db.get_resource('foo')
    y = [z.element for z in x if x.frobnizzle==5]
%>


模块级别的代码
<%!
    import mylib
    import re

    def filter(text):
        return re.sub(r'^@', '', text)
%>


5.

单行注释
## this is a comment.

多行注释
<%doc>
    these are comments
    more comments
</%doc>


6.
标签

包含,可以有变量
<%include file="foo.txt"/>
<%include file="/foo/bar/${myfile}.txt"/>

定义类似函数html文本段
${account()}

<%def name="account()">
    Account for ${username}:<br/>

    % for row in accountdata:
        Value: ${row}<br/>
    % endfor
</%def>

定义该页模板的参数
<%page cached="True" cache_type="memory"/>

名字空间
<%namespace file="functions.html" import="*"/>

<%namespace name="mystuff" file="mystuff.html"/>

${mystuff.somedef(x=5,y=7)}

The <%namespace> tag also supports some of the other semantics of Python's import statement, including pulling names into the local variable space, or using * to represent all names, using the import attribute:

<%namespace file="mystuff.html" import="foo, bar"/>


继承
## base.html
<html>
    <body>
        <div class="header">
            ${self.header()}
        </div>

        ${self.body()}

        <div class="footer">
            ${self.footer()}
        </div>
    </body>
</html>

<%def name="footer()">
    this is the footer
</%def>

this is the body content.

也可以用
    ${next.body()}
表示下一级继承页面

    ${parent.toolbar()}
访问上一层
----------------------------------

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

<%def name="header()">
    this is some header content
</%def>





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


8.
函数更多用法演示:

缓存
<%def name="somedef()" buffered="true">
    somedef's results
</%def>
----------------------------------
${account(accountname='john')}

<%def name="account(accountname, type='regular')">
    account name: ${accountname}, type ${type}
</%def>
----------------------------------
嵌套函数
<%def name="mydef()">
    <%def name="subdef()">
        a sub def
    </%def>

    im the def, and the subcomopnent is ${subdef()}
</%def>
----------------------------------
调用者
<%def name="buildtable()">
    <table>
        <tr><td>
            ${caller.body()}
        </td></tr>
    </table>
</%def>

<%call expr="buildtable">
    I am the table body.
</%call>

输出
<table>
    <tr><td>
        I am the table body.
    </td></tr>
</table>

----------------------------------

<%def name="lister(count)">
    % for x in range(1,count):
        ${caller.body()}
    % endfor
</%def>

<%call expr="lister(3)">
    hi
</%call>

输出
hi
hi
hi

----------------------------------

<%def name="conditional(expr)">
    % if expr:
        ${caller.body()}
    % endif
</%def>

<%call expr="conditional(4==4)">
    im the result
</%call>

----------------------------------


<%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>

<%call expr="layoutdata([[1,2,3],[4,5,6],[7,8,9]])" args="col">
    Body data: ${col}
</%call>
输出:

<table>
    <tr>
        <td>Body data: 1</td><td>Body data: 2</td><td>Body data: 3</td>
        <td>Body data: 4</td><td>Body data: 5</td><td>Body data: 6</td>
        <td>Body data: 7</td><td>Body data: 8</td><td>Body data: 9</td>
    </tr>
</table>

----------------------------------

<%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
<%call expr="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
</%call>

输出


<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>

你可能感兴趣的:(html,linux,python,Gmail,Pylons)