ZPT页面模板样例
ZPT页面模板样例
http://www.315ok.org/blogfolder/10
http://www.315ok.org/logo.png
ZPT页面模板样例
ZPT页面模板样例
TAL样例tal:content:输出:
<p tal:content="string: abc"> 段落内容 </p>
tal:replace:替换:
<p tal:replace="string: heloo">aaa</p>
tal:define:定义变量:
<p tal:define="varA python:1; varB string:abc" tal:content="string:$varA$varB"/>
tal:repeat:循环:
<ul> <li tal:repeat="i python:range(1,5)" tal:content="string:this is No. $i"/> </ul>
tal:on-error 异常处理:
<span tal:on-error="string:出错了" tal:content="xxxxx"/>
tal:attributes 修改属性:
<p tal:attributes="style string:color: blue">一段文字</p>
tal:condition条件:
<span tal:condition="python: 0"> 文字 </span>
查看调用传递的变量options:
<span tal:replace="options"/>
structure方式查看request变量:
<span tal:replace="structure request" />
属性默认值:attrs:
这段文本的风格是:
<span tal:content="attrs/style" style="color: red">这段文本的风格是: </span>
执行顺序:
<tal:block tal:repeat="i python:range(10)">
<span tal:condition="python: i <5" tal:content="i"> </span>
</tal:block>
i18n样例 i18n:translate:翻译(1):
<span i18n:domain="plone" i18n:translate="">title</span>
i18n:translate:翻译(2):
<span i18n:domain="plone" i18n:translate="help_description">username</span>
i18n:name 翻译中的变量:
<span i18n:domain="plone" i18n:translate="batch_previous_x_items" tal:omit-tag="">
Previous <span i18n:name="number" tal:content="string:12">n</span> items</span>
i18n:attributes 翻译属性:
<input type="submit" value="save" title="submit" i18n:domain="plone" i18n:attributes="value Save; title Submit" />
METAL样例 macros的定义和使用:
<span metal:define-macro="abc"> <table border="2">
<tr>
<td>aaa</td>
<td>aaa</td>
</tr>
<tr>
<td>aaa</td>
<td>aaa</td>
</tr>
</table>
</span>
<div metal:use-macro="template/macros/abc"/>google广告宏:
<tal:block tal:condition="nothing">
<div metal:define-macro="portlet">
<div class="portlet">
<script type="text/javascript">
<!– google_ad_client = "yourUniqueValue";
google_ad_width = 120;
google_ad_height = 600;
google_ad_format = "120x600_as"; //–>
</script>
<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"/>
</div>
</div>
</tal:block>
<div metal:use-macro="template/macros/portlet"/>slot的定义和使用:
<div metal:define-macro="macro-a">
<span>start text in macro-a</span>
<div metal:define-slot="slot-b"> default text in slot-b </div>
<span>end text in macro-a</span>
</div>
<div metal:use-macro="template/macros/macro-a"> can this line visible? </div>
<div metal:use-macro="template/macros/macro-a">
<div metal:fill-slot="slot-b"> can this line visible in slot-b? </div>
</div>
分析Plone的主模板的宏:
<span tal:replace="python:context.main_template.macros.keys()"/>
访问archetypes中的field显示Field的一个widget:
<metal:block use-macro="here/global_defines/macros/defines" />
<div tal:define="errors python:{}; ">
<input metal:use-macro="python: here.Members.widget('title', mode='edit')"
name="textfield"
type="text" size="30"/>
</div>
访问field的值:
<div tal:omit-tag="" tal:define="field python:here.Members.Schema()['title'];
accessor python:getattr(here.Members, field.accessor)"
tal:content="python:accessor()"> </div>
访问事件的类型字段(字典)(context应该是事件):
<div tal:define="field python:here.testeventvoc.Schema()['eventType'];
accessor python:getattr(here.testeventvoc, field.accessor);
vocab python:field.Vocabulary(here.testeventvoc)">
<span tal:repeat="value accessor" tal:content="python:vocab.getValue(value)"/>
</div>
<html>
<head>
<title tal:content="template/title">The title</title>
</head>
<body>
<h2><span tal:replace="here/title_or_id">content title or id</span>
<span tal:define="global title python:getattr(template, 'ti tle')"
tal:on-error="string:"
tal:condition="title"
tal:replace="template/title">optional title</span>
</h2>
This is Page Template <em tal:content="template/id">template id</em>.
<h2 tal:omit-tag="">request</h2>
<p tal:content="structure request"> request </p>
<p tal:repeat="i python: range(10)">
<input value="2" tal:condition="python: i > 5 "
tal:attributes="value python: i+100; style string:color: red;"/>
</p>
</body>
</html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<title tal:content="template/title">The title</title>
</head>
<body>
<h2><span tal:replace="here/title_or_id">content title or id</span>
<span tal:condition="template/title" tal:replace="template/title">optional template title</span>
</h2>
<div tal:define="portal context/portal_url/getPortalObject">
<p tal:repeat="content portal/listFolderContents"
tal:content="string:${repeat/content/number}: ${content/Title}(${content/id})">
</p>
</div>
</body>
</html>
ZPT调用ScriptScript得到网站会员清单(id: getSiteMembers):
membership = context.portal_membershipmembers = []
# 打印姓名和邮件
for m in membership.listMembers():
members.append({'id':m.getId(),
'fullname':m.getProperty('fullname'),
'email':m.getProperty('email'),
'roles':m.getRoles() })
return members
表格方式列举网站所有的成员信息:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<title tal:content="template/title">The title</title>
</head>
<body>
<h2><span tal:replace="here/title_or_id">content title or id</span>
<span tal:condition="template/title" tal:replace="template/title">optional template title</span>
</h2>
<table border="1">
<tr> <th>登录名</th> <th>姓名</th> <th>电子邮件</th> <th>角色</th> </tr>
<tr tal:repeat="member context/getSiteMembers">
<td tal:content="member/id"/>
<td tal:content="member/fullname"/>
<td>
<a tal:content="member/email"
tal:attributes="href string:mailto:${member/email};
title member/fullname">[email]aa@bb.com[/email]</a>
</td>
<td><span tal:content="python: ','.join(member['roles'])"/>
<span tal:condition="python:'Manager' in member['roles']">※※※</span>
</td>
</tr>
</table>
</body>
</html>
Script中调用ZPT脚本:
return context.CalledZPT(1,2,a=3,b=4)ZPT(id: CalledZPT):
<html>
<head>
<title tal:content="template/title">The title</title>
</head>
<body>
<h1>这个页面是Script(Python)调用的ZPT生成的</h1>
<span>传递的参数保存在options绑定变量中:</span>
<span tal:replace="options"/>
</body>
</html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<title tal:content="template/title">The title</title>
</head>
<body>
<h2><span tal:replace="here/title_or_id">content title or id</span>
<span tal:condition="template/title" tal:replace="template/title">optional template title</span>
</h2> <ol tal:define="folders python:here.portal_catalog.searchResults(portal_type='Folder')">
<li tal:repeat="content folders" tal:content="string:${content/Title}(${content/getPath})">
</li>
</ol>
</body>
</html>
定义一个Google广告宏macro_GoogleAD:
<div metal:define-macro="portlet">
<h3 metal:define-slot="title"> 广告 </h3>
<script type="text/javascript">
<!– google_ad_client = "yourUniqueValue";
google_ad_width = 120;
google_ad_height = 600;
google_ad_format = "120x600_as"; //–>
</script>
<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"/>
</div>
使用宏:
<div metal:use-macro="context/macro_GoogleAD/macros/portlet" />