Plone4 模板结构

Plone4 模板结构
[align=center][size=14.0pt]Plone4 [/size][size=14.0pt]模板结构[/size][/align][align=center] [/align]1. 主模板1.1 位置Products.CMFPlone包的skins目录下,plone_templates ->main_template.pt # useradd -u plone -g plone
用Plone账号登陆

1.2 内容:[align=left][size=14.0pt]<[/size][size=14.0pt]metal[/size][size=14.0pt]:[/size][size=14.0pt]page define-macro[/size][size=14.0pt]=[/size][size=14.0pt]"master"[/size][size=14.0pt]> [/size][size=14.0pt]将整个[/size][size=14.0pt]main_template.pt[/size][size=14.0pt]的输出定义为一个名为“[/size][size=14.0pt]master[/size][size=14.0pt]”的宏[/size][/align][align=left][size=14.0pt]<[/size][size=14.0pt]metal[/size][size=14.0pt]:[/size][size=14.0pt]block define-slot[/size][size=14.0pt]=[/size][size=14.0pt]"top_slot" [/size][size=14.0pt]/>[/size][/align][align=left][size=14.0pt]留一个名为[/size][size=14.0pt]"top_slot"[/size][size=14.0pt]的槽位,方便调用[/size][size=14.0pt]"master"[/size][size=14.0pt]宏的其他页面填充[/size][/align][align=left][size=14.0pt]<[/size][size=14.0pt]tal[/size][size=14.0pt]:[/size][size=14.0pt]doctype tal:replace[/size][size=14.0pt]=[/size][size=14.0pt]"structure string:&lt;!DOCTYPEhtml&gt;" [/size][size=14.0pt]/>[/size][/align][align=left][size=14.0pt]申明[/size][size=14.0pt]HTML5 DOCTYPE[/size][/align][align=left][size=14.0pt]<[/size][size=14.0pt]html xmlns[/size][size=14.0pt]=[/size][size=14.0pt]"http://www.w3.org/1999/xhtml"[/size][/align][align=left][size=14.0pt] [/size][size=14.0pt]xmlns:tal[/size][size=14.0pt]=[/size][size=14.0pt]"http://xml.zope.org/namespaces/tal"[/size][/align][align=left][size=14.0pt] [/size][size=14.0pt]xmlns:metal[/size][size=14.0pt]=[/size][size=14.0pt]"http://xml.zope.org/namespaces/metal"[/size][/align][align=left][size=14.0pt] [/size][size=14.0pt]xmlns[/size][size=14.0pt]:[/size][size=14.0pt]i18n[/size][size=14.0pt]=[/size][size=14.0pt]"http://xml.zope.org/namespaces/i18n"[/size][/align][align=left][size=14.0pt] [/size][size=14.0pt]xml:lang[/size][size=14.0pt]=[/size][size=14.0pt]"en"[/size][/align][align=left][size=14.0pt] [/size][size=14.0pt]lang[/size][size=14.0pt]=[/size][size=14.0pt]"en"[/size][/align][align=left][size=14.0pt] [/size][size=14.0pt]tal:define[/size][size=14.0pt]=[/size][size=14.0pt]"portal_statecontext/@@plone_portal_state;[/size][/align][align=left][size=14.0pt] context_statecontext/@@plone_context_state;[/size][/align][align=left][size=14.0pt] plone_view context/@@plone;[/size][/align][align=left][size=14.0pt] lang portal_state/language;[/size][/align][align=left][size=14.0pt] view nocall:view | nocall:plone_view;[/size][/align][align=left][size=14.0pt] dummy python: plone_view.mark_view(view);[/size][/align][align=left][size=14.0pt] portal_urlportal_state/portal_url;[/size][/align][align=left][size=14.0pt] checkPermission nocall:context/portal_membership/checkPermission;[/size][/align][align=left][size=14.0pt] site_propertiescontext/portal_properties/site_properties;[/size][/align][align=left][size=14.0pt] ajax_load request/ajax_load |nothing;[/size][/align][align=left][size=14.0pt] ajax_include_headrequest/ajax_include_head | nothing;[/size][/align][align=left][size=14.0pt] dummypython:request.RESPONSE.setHeader('X-UA-Compatible', 'IE=edge,chrome=1');" [/size][size=14.0pt]定义全局变量[/size][/align][align=left][size=14.0pt] [/size][size=14.0pt]tal:attributes[/size][size=14.0pt]=[/size][size=14.0pt]"lang lang;[/size][/align][align=left][size=14.0pt] xml:lang lang"[/size][size=14.0pt]>[/size][/align][align=left][size=14.0pt] [/size][/align][align=left][size=14.0pt] [/size][size=14.0pt]<[/size][size=14.0pt]metal[/size][size=14.0pt]:[/size][size=14.0pt]cache use-macro[/size][size=14.0pt]=[/size][size=14.0pt]"context/global_cache_settings/macros/cacheheaders"[/size][size=14.0pt]>[/size][/align][align=left][size=14.0pt] Get the global cache headers located inglobal_cache_settings.[/size][/align][align=left][size=14.0pt] [/size][size=14.0pt]</[/size][size=14.0pt]metal[/size][size=14.0pt]:[/size][size=14.0pt]cache[/size][size=14.0pt]>[/size][/align][align=left][size=14.0pt]调用[/size][size=14.0pt]global_cache_settings.pt[/size][size=14.0pt]模板的[/size][size=14.0pt]cacheheaders[/size][size=14.0pt]宏[/size][/align][align=left][size=14.0pt] [/size][size=14.0pt]<[/size][size=14.0pt]head[/size][size=14.0pt]>[/size][/align][align=left][size=14.0pt] [/size][/align][align=left][size=14.0pt] [/size][size=14.0pt]<[/size][size=14.0pt]meta http-equiv[/size][size=14.0pt]=[/size][size=14.0pt]"Content-Type" [/size][size=14.0pt]content[/size][size=14.0pt]=[/size][size=14.0pt]"text/html; charset=utf-8" [/size][size=14.0pt]/>[/size][/align][align=left][size=14.0pt] [/size][/align][align=left][size=14.0pt] [/size][size=14.0pt]<[/size][size=14.0pt]meta name[/size][size=14.0pt]=[/size][size=14.0pt]"generator" [/size][size=14.0pt]content[/size][size=14.0pt]=[/size][size=14.0pt]"Plone - http://plone.org" [/size][size=14.0pt]/>[/size][/align][align=left][size=14.0pt] [/size][/align][align=left][size=14.0pt] [/size][size=14.0pt]<[/size][size=14.0pt]metal[/size][size=14.0pt]:[/size][size=14.0pt]baseslot define-slot[/size][size=14.0pt]=[/size][size=14.0pt]"base"[/size][size=14.0pt]>[/size][/align][align=left][size=14.0pt] [/size][size=14.0pt]<[/size][size=14.0pt]base tal:attributes[/size][size=14.0pt]=[/size][size=14.0pt]"hrefplone_view/renderBase" [/size][size=14.0pt]/>[/size][size=14.0pt]<!–[if ltIE 7]></base><![endif]–>[/size][/align][align=left][size=14.0pt] [/size][size=14.0pt]</[/size][size=14.0pt]metal[/size][size=14.0pt]:[/size][size=14.0pt]baseslot[/size][size=14.0pt]>[/size][/align][align=left][size=14.0pt] [/size][/align][align=left][size=14.0pt] [/size][size=14.0pt]<[/size][size=14.0pt]tal[/size][size=14.0pt]:[/size][size=14.0pt]notajax tal:condition[/size][size=14.0pt]=[/size][size=14.0pt]"python:not ajax_load orajax_include_head"[/size][size=14.0pt]> [/size][size=14.0pt]非[/size][size=14.0pt]ajax load[/size][size=14.0pt]页面则加载[/size][size=14.0pt] [/size][size=14.0pt]各种[/size][size=14.0pt]Plone[/size][size=14.0pt]的[/size][size=14.0pt]header [/size][size=14.0pt]资源[/size][/align][align=left][size=14.0pt] [/size][size=14.0pt]<[/size][size=14.0pt]div tal:replace[/size][size=14.0pt]=[/size][size=14.0pt]"structure provider:plone.htmlhead" [/size][size=14.0pt]/>[/size][/align][align=left][size=14.0pt] [/size][/align][align=left][size=14.0pt] [/size][size=14.0pt]<[/size][size=14.0pt]link tal:replace[/size][size=14.0pt]=[/size][size=14.0pt]"structure provider:plone.htmlhead.links" [/size][size=14.0pt]/>[/size][/align][align=left][size=14.0pt] [/size][/align][align=left][size=14.0pt] [/size][size=14.0pt]<[/size][size=14.0pt]tal[/size][size=14.0pt]:[/size][size=14.0pt]comment replace[/size][size=14.0pt]=[/size][size=14.0pt]"nothing"[/size][size=14.0pt]>[/size][size=14.0pt] A slotwhere you can insert elements in the header from a template [/size][size=14.0pt]</[/size][size=14.0pt]tal[/size][size=14.0pt]:[/size][size=14.0pt]comment[/size][size=14.0pt]>[/size][/align][align=left][size=14.0pt] [/size][size=14.0pt]<[/size][size=14.0pt]metal[/size][size=14.0pt]:[/size][size=14.0pt]headslot define-slot[/size][size=14.0pt]=[/size][size=14.0pt]"head_slot" [/size][size=14.0pt]/>[/size][size=14.0pt] [/size][size=14.0pt]定义[/size][size=14.0pt]head_slot[/size][size=14.0pt]槽[/size][/align][align=left][size=14.0pt] [/size][/align][align=left][size=14.0pt] [/size][size=14.0pt]<[/size][size=14.0pt]tal[/size][size=14.0pt]:[/size][size=14.0pt]comment replace[/size][size=14.0pt]=[/size][size=14.0pt]"nothing"[/size][size=14.0pt]>[/size][size=14.0pt] A slotwhere you can insert CSS in the header from a template [/size][size=14.0pt]</[/size][size=14.0pt]tal[/size][size=14.0pt]:[/size][size=14.0pt]comment[/size][size=14.0pt]>[/size][/align][align=left][size=14.0pt] [/size][size=14.0pt]<[/size][size=14.0pt]metal[/size][size=14.0pt]:[/size][size=14.0pt]styleslot define-slot[/size][size=14.0pt]=[/size][size=14.0pt]"style_slot" [/size][size=14.0pt]/> [/size][size=14.0pt]定义[/size][size=14.0pt]css style [/size][size=14.0pt]槽[/size][/align][align=left][size=14.0pt] [/size][/align][align=left][size=14.0pt] [/size][size=14.0pt]<[/size][size=14.0pt]tal[/size][size=14.0pt]:[/size][size=14.0pt]comment replace[/size][size=14.0pt]=[/size][size=14.0pt]"nothing"[/size][size=14.0pt]>[/size][size=14.0pt] This isdeprecated, please use style_slot instead. [/size][size=14.0pt]</[/size][size=14.0pt]tal[/size][size=14.0pt]:[/size][size=14.0pt]comment[/size][size=14.0pt]>[/size][/align][align=left][size=14.0pt] [/size][size=14.0pt]<[/size][size=14.0pt]metal[/size][size=14.0pt]:[/size][size=14.0pt]cssslot define-slot[/size][size=14.0pt]=[/size][size=14.0pt]"css_slot" [/size][size=14.0pt]/>[/size][/align][align=left][size=14.0pt] [/size][/align][align=left][size=14.0pt] [/size][size=14.0pt]<[/size][size=14.0pt]tal[/size][size=14.0pt]:[/size][size=14.0pt]comment replace[/size][size=14.0pt]=[/size][size=14.0pt]"nothing"[/size][size=14.0pt]>[/size][size=14.0pt] A slotwhere you can insert javascript in the header from a template [/size][size=14.0pt]</[/size][size=14.0pt]tal[/size][size=14.0pt]:[/size][size=14.0pt]comment[/size][size=14.0pt]>[/size][/align][align=left][size=14.0pt] [/size][size=14.0pt]<[/size][size=14.0pt]metal[/size][size=14.0pt]:[/size][size=14.0pt]javascriptslot define-slot[/size][size=14.0pt]=[/size][size=14.0pt]"javascript_head_slot" [/size][size=14.0pt]/> [/size][size=14.0pt]定义[/size][size=14.0pt]js [/size][size=14.0pt]槽[/size][/align][align=left][size=14.0pt] [/size][size=14.0pt]</[/size][size=14.0pt]tal[/size][size=14.0pt]:[/size][size=14.0pt]notajax[/size][size=14.0pt]>[/size][/align][align=left][size=14.0pt] [/size][/align][align=left][size=14.0pt] [/size][size=14.0pt]</[/size][size=14.0pt]head[/size][size=14.0pt]>[/size][/align][align=left][size=14.0pt] [/size][/align][align=left][size=14.0pt] [/size][size=14.0pt]<[/size][size=14.0pt]body tal:define[/size][size=14.0pt]=[/size][size=14.0pt]"isRTL portal_state/is_rtl;[/size][/align][align=left][size=14.0pt] slpython:plone_view.have_portlets('plone.leftcolumn', view); [/size][size=14.0pt]是否有左列[/size][/align][align=left][size=14.0pt] srpython:plone_view.have_portlets('plone.rightcolumn', view); [/size][size=14.0pt]是否有右列[/size][/align][align=left][size=14.0pt] body_classpython:plone_view.bodyClass(template, view);"[/size][/align][align=left][size=14.0pt] [/size][size=14.0pt]tal:attributes[/size][size=14.0pt]=[/size][size=14.0pt]"class body_class;[/size][/align][align=left][size=14.0pt] dir python: isRTL and'rtl' or 'ltr'"[/size][size=14.0pt]>[/size][/align][align=left][size=14.0pt] [/size][size=14.0pt]<[/size][size=14.0pt]div id[/size][size=14.0pt]=[/size][size=14.0pt]"visual-portal-wrapper"[/size][size=14.0pt]>[/size][/align][align=left][size=14.0pt] [/size][/align][align=left][size=14.0pt] [/size][size=14.0pt]<[/size][size=14.0pt]div id[/size][size=14.0pt]=[/size][size=14.0pt]"portal-top" [/size][size=14.0pt]i18n[/size][size=14.0pt]:[/size][size=14.0pt]domain[/size][size=14.0pt]=[/size][size=14.0pt]"plone"[/size][size=14.0pt]>[/size][/align][align=left][size=14.0pt] [/size][size=14.0pt]<[/size][size=14.0pt]div tal:replace[/size][size=14.0pt]=[/size][size=14.0pt]"structure provider:plone.portaltop" [/size][size=14.0pt]tal:condition[/size][size=14.0pt]=[/size][size=14.0pt]"not:ajax_load" [/size][size=14.0pt]/> [/size][size=14.0pt]非[/size][size=14.0pt]ajax[/size][size=14.0pt]页面则加载[/size][size=14.0pt] portaltop viewletmanager[/size][/align][align=left][size=14.0pt] [/size][size=14.0pt]</[/size][size=14.0pt]div[/size][size=14.0pt]>[/size][/align][align=left][size=14.0pt] [/size][/align][align=left][size=14.0pt] [/size][size=14.0pt]<[/size][size=14.0pt]div class[/size][size=14.0pt]=[/size][size=14.0pt]"visualClear" [/size][size=14.0pt]id[/size][size=14.0pt]=[/size][size=14.0pt]"clear-space-before-wrapper-table"[/size][size=14.0pt]>[/size][size=14.0pt]<!– –>[/size][size=14.0pt]</[/size][size=14.0pt]div[/size][size=14.0pt]>[/size][/align][align=left][size=14.0pt] [/size][/align][align=left][size=14.0pt] [/size][size=14.0pt]<[/size][size=14.0pt]table id[/size][size=14.0pt]=[/size][size=14.0pt]"portal-columns"[/size][size=14.0pt]>[/size][/align][align=left][size=14.0pt] [/size][size=14.0pt]<[/size][size=14.0pt]tbody[/size][size=14.0pt]>[/size][/align][align=left][size=14.0pt] [/size][size=14.0pt]<[/size][size=14.0pt]tr[/size][size=14.0pt]>[/size][/align][align=left][size=14.0pt] [/size][size=14.0pt]<[/size][size=14.0pt]tal[/size][size=14.0pt]:[/size][size=14.0pt]notajax tal:condition[/size][size=14.0pt]=[/size][size=14.0pt]"not:ajax_load"[/size][size=14.0pt]>[/size][/align][align=left][size=14.0pt] [/size][size=14.0pt]<[/size][size=14.0pt]tal[/size][size=14.0pt]:[/size][size=14.0pt]comment replace[/size][size=14.0pt]=[/size][size=14.0pt]"nothing"[/size][size=14.0pt]>[/size][size=14.0pt] Start ofthe left column [/size][size=14.0pt]</[/size][size=14.0pt]tal[/size][size=14.0pt]:[/size][size=14.0pt]comment[/size][size=14.0pt]>[/size][/align][align=left][size=14.0pt] [/size][size=14.0pt]<[/size][size=14.0pt]td id[/size][size=14.0pt]=[/size][size=14.0pt]"portal-column-one"[/size][/align][align=left][size=14.0pt] [/size][size=14.0pt]metal:define-slot[/size][size=14.0pt]=[/size][size=14.0pt]"column_one_slot"[/size][/align][align=left][size=14.0pt] [/size][size=14.0pt]tal:condition[/size][size=14.0pt]=[/size][size=14.0pt]"sl"[/size][size=14.0pt]>[/size][/align][align=left][size=14.0pt] [/size][size=14.0pt]<[/size][size=14.0pt]div class[/size][size=14.0pt]=[/size][size=14.0pt]"visualPadding"[/size][size=14.0pt]>[/size][/align][align=left][size=14.0pt] [/size][size=14.0pt]<[/size][size=14.0pt]metal[/size][size=14.0pt]:[/size][size=14.0pt]portlets define-slot[/size][size=14.0pt]=[/size][size=14.0pt]"portlets_one_slot"[/size][size=14.0pt]>[/size][/align][align=left][size=14.0pt] [/size][size=14.0pt]<[/size][size=14.0pt]tal[/size][size=14.0pt]:[/size][size=14.0pt]block replace[/size][size=14.0pt]=[/size][size=14.0pt]"structure provider:plone.leftcolumn" [/size][size=14.0pt]/> [/size][size=14.0pt]用名为[/size][size=14.0pt]leftcolumn[/size][size=14.0pt]的[/size][size=14.0pt]portletmanager[/size][size=14.0pt]填充[/size][/align][align=left][size=14.0pt] [/size][size=14.0pt]</[/size][size=14.0pt]metal[/size][size=14.0pt]:[/size][size=14.0pt]portlets[/size][size=14.0pt]>[/size][/align][align=left][size=14.0pt] [/size][size=14.0pt]&nbsp;[/size][/align][align=left][size=14.0pt] [/size][size=14.0pt]</[/size][size=14.0pt]div[/size][size=14.0pt]>[/size][/align][align=left][size=14.0pt] [/size][size=14.0pt]</[/size][size=14.0pt]td[/size][size=14.0pt]>[/size][/align][align=left][size=14.0pt] [/size][size=14.0pt]<[/size][size=14.0pt]tal[/size][size=14.0pt]:[/size][size=14.0pt]comment replace[/size][size=14.0pt]=[/size][size=14.0pt]"nothing"[/size][size=14.0pt]>[/size][size=14.0pt] End of theleft column [/size][size=14.0pt]</[/size][size=14.0pt]tal[/size][size=14.0pt]:[/size][size=14.0pt]comment[/size][size=14.0pt]>[/size][/align][align=left][size=14.0pt] [/size][size=14.0pt]</[/size][size=14.0pt]tal[/size][size=14.0pt]:[/size][size=14.0pt]notajax[/size][size=14.0pt]>[/size][/align][align=left][size=14.0pt] [/size][/align][align=left][size=14.0pt] [/size][size=14.0pt]<[/size][size=14.0pt]tal[/size][size=14.0pt]:[/size][size=14.0pt]comment replace[/size][size=14.0pt]=[/size][size=14.0pt]"nothing"[/size][size=14.0pt]>[/size][size=14.0pt] Start ofmain content block [/size][size=14.0pt]</[/size][size=14.0pt]tal[/size][size=14.0pt]:[/size][size=14.0pt]comment[/size][size=14.0pt]>[/size][/align][align=left][size=14.0pt] [/size][size=14.0pt]<[/size][size=14.0pt]td id[/size][size=14.0pt]=[/size][size=14.0pt]"portal-column-content"[/size][size=14.0pt]>[/size][/align][align=left][size=14.0pt] [/size][/align][align=left][size=14.0pt] [/size][size=14.0pt]<[/size][size=14.0pt]div id[/size][size=14.0pt]=[/size][size=14.0pt]"viewlet-above-content" [/size][size=14.0pt]tal:content[/size][size=14.0pt]=[/size][size=14.0pt]"structureprovider:plone.abovecontent" [/size][size=14.0pt]/> [/size][size=14.0pt]用名为[/size][size=14.0pt]"abovecontent"[/size][size=14.0pt]的[/size][size=14.0pt]viewletmanager[/size][size=14.0pt]填充[/size][/align][align=left][size=14.0pt] [/size][/align][align=left][size=14.0pt] [/size][size=14.0pt]<[/size][size=14.0pt]metal[/size][size=14.0pt]:[/size][size=14.0pt]block define-slot[/size][size=14.0pt]=[/size][size=14.0pt]"content"[/size][size=14.0pt]> [/size][size=14.0pt]定义[/size][size=14.0pt]content[/size][size=14.0pt]槽[/size][/align][align=left][size=14.0pt] [/size][size=14.0pt]<[/size][size=14.0pt]div metal:define-macro[/size][size=14.0pt]=[/size][size=14.0pt]"content"[/size][/align][align=left][size=14.0pt] [/size][size=14.0pt]tal:define[/size][size=14.0pt]=[/size][size=14.0pt]"show_border context/@@plone/showEditableBorder"[/size][/align][align=left][size=14.0pt] [/size][size=14.0pt]tal:attributes[/size][size=14.0pt]=[/size][size=14.0pt]"class python:show_border and 'documentEditable' or''"[/size][size=14.0pt]>[/size][/align][align=left][size=14.0pt] [/size][/align][align=left][size=14.0pt] [/size][size=14.0pt]<[/size][size=14.0pt]div id[/size][size=14.0pt]=[/size][size=14.0pt]"edit-bar"[/size][/align][align=left][size=14.0pt] [/size][size=14.0pt]tal:condition[/size][size=14.0pt]=[/size][size=14.0pt]"show_border"[/size][/align][align=left][size=14.0pt] [/size][size=14.0pt]tal:content[/size][size=14.0pt]=[/size][size=14.0pt]"structureprovider:plone.contentviews" [/size][size=14.0pt]/> [/size][size=14.0pt]填充[/size][size=14.0pt]plone.contentviews viewletmanager[/size][/align][align=left][size=14.0pt] [/size][/align][align=left][size=14.0pt] [/size][size=14.0pt]<[/size][size=14.0pt]span id[/size][size=14.0pt]=[/size][size=14.0pt]"contentTopLeft"[/size][size=14.0pt]></[/size][size=14.0pt]span[/size][size=14.0pt]>[/size][/align][align=left][size=14.0pt] [/size][size=14.0pt]<[/size][size=14.0pt]span id[/size][size=14.0pt]=[/size][size=14.0pt]"contentTopRight"[/size][size=14.0pt]></[/size][size=14.0pt]span[/size][size=14.0pt]>[/size][/align][align=left][size=14.0pt] [/size][/align][align=left][size=14.0pt] [/size][size=14.0pt]<[/size][size=14.0pt]div metal:use-macro[/size][size=14.0pt]=[/size][size=14.0pt]"context/global_statusmessage/macros/portal_message"[/size][size=14.0pt]> [/size][size=14.0pt]用[/size][size=14.0pt] global_statusmessage[/size][size=14.0pt]模板的[/size][size=14.0pt]portal_message[/size][size=14.0pt]宏填充[/size][/align][align=left][size=14.0pt] Portal status message[/size][/align][align=left][size=14.0pt] [/size][size=14.0pt]</[/size][size=14.0pt]div[/size][size=14.0pt]>[/size][/align][align=left][size=14.0pt] [/size][/align][align=left][size=14.0pt] [/size][size=14.0pt]<[/size][size=14.0pt]metal[/size][size=14.0pt]:[/size][size=14.0pt]slot define-slot[/size][size=14.0pt]=[/size][size=14.0pt]"body"[/size][size=14.0pt]>[/size][/align][align=left][size=14.0pt] [/size][size=14.0pt]<[/size][size=14.0pt]div id[/size][size=14.0pt]=[/size][size=14.0pt]"content"[/size][size=14.0pt]>[/size][/align][align=left][size=14.0pt] [/size][size=14.0pt]<[/size][size=14.0pt]metal[/size][size=14.0pt]:[/size][size=14.0pt]header define-slot[/size][size=14.0pt]=[/size][size=14.0pt]"header" [/size][size=14.0pt]tal:content[/size][size=14.0pt]=[/size][size=14.0pt]"nothing"[/size][size=14.0pt]> [/size][size=14.0pt]定义一个[/size][size=14.0pt]header[/size][size=14.0pt]槽[/size][/align][align=left][size=14.0pt] Visual Header[/size][/align][align=left][size=14.0pt] [/size][size=14.0pt]</[/size][size=14.0pt]metal[/size][size=14.0pt]:[/size][size=14.0pt]header[/size][size=14.0pt]>[/size][/align][align=left][size=14.0pt] [/size][size=14.0pt]<[/size][size=14.0pt]metal[/size][size=14.0pt]:[/size][size=14.0pt]bodytext define-slot[/size][size=14.0pt]=[/size][size=14.0pt]"main"[/size][size=14.0pt]>[/size][/align][align=left][size=14.0pt]定义一个[/size][size=14.0pt]main[/size][size=14.0pt]槽[/size][/align][align=left][size=14.0pt] [/size][size=14.0pt]<[/size][size=14.0pt]metal[/size][size=14.0pt]:[/size][size=14.0pt]main define-macro[/size][size=14.0pt]=[/size][size=14.0pt]"main"[/size][size=14.0pt]>[/size][/align][align=left][size=14.0pt] [/size][size=14.0pt]<[/size][size=14.0pt]div tal:replace[/size][size=14.0pt]=[/size][size=14.0pt]"structure provider:plone.abovecontenttitle" [/size][size=14.0pt]/> [/size][size=14.0pt]用名为[/size][size=14.0pt]" plone.abovecontenttitle "[/size][size=14.0pt]的[/size][size=14.0pt]viewletmanager[/size][size=14.0pt]填充[/size][/align][align=left][size=14.0pt] [/size][size=14.0pt]<[/size][size=14.0pt]metal[/size][size=14.0pt]:[/size][size=14.0pt]title define-slot[/size][size=14.0pt]=[/size][size=14.0pt]"content-title"[/size][size=14.0pt]>[/size][/align][align=left][size=14.0pt] [/size][size=14.0pt]<[/size][size=14.0pt]metal[/size][size=14.0pt]:[/size][size=14.0pt]comment tal:content[/size][size=14.0pt]=[/size][size=14.0pt]"nothing"[/size][size=14.0pt]>[/size][/align][align=left][size=14.0pt] If youwrite a custom title always use[/size][/align][align=left][size=14.0pt] [/size][size=14.0pt]<[/size][size=14.0pt]h1 class[/size][size=14.0pt]=[/size][size=14.0pt]"documentFirstHeading"[/size][size=14.0pt]></[/size][size=14.0pt]h1[/size][size=14.0pt]>[/size][size=14.0pt] for it.[/size][/align][align=left][size=14.0pt] [/size][size=14.0pt]</[/size][size=14.0pt]metal[/size][size=14.0pt]:[/size][size=14.0pt]comment[/size][size=14.0pt]>[/size][/align][align=left][size=14.0pt] [/size][size=14.0pt]<[/size][size=14.0pt]h1 metal:use-macro[/size][size=14.0pt]=[/size][size=14.0pt]"context/kss_generic_macros/macros/generic_title_view"[/size][size=14.0pt]>[/size][/align][align=left][size=14.0pt] Generic KSSTitle. Is rendered with class="documentFirstHeading".[/size][/align][align=left][size=14.0pt] [/size][size=14.0pt]</[/size][size=14.0pt]h1[/size][size=14.0pt]>[/size][/align][align=left][size=14.0pt] [/size][size=14.0pt]</[/size][size=14.0pt]metal[/size][size=14.0pt]:[/size][size=14.0pt]title[/size][size=14.0pt]>[/size][/align][align=left][size=14.0pt] [/size][size=14.0pt]<[/size][size=14.0pt]div tal:replace[/size][size=14.0pt]=[/size][size=14.0pt]"structure provider:plone.belowcontenttitle" [/size][size=14.0pt]/>[/size][size=14.0pt]用名为[/size][size=14.0pt]" plone.belowcontenttitle "[/size][size=14.0pt]的[/size][size=14.0pt]viewletmanager[/size][size=14.0pt]填充[/size][/align][align=left][size=14.0pt] [/size][/align][align=left][size=14.0pt] [/size][/align][align=left][size=14.0pt] [/size][size=14.0pt]<[/size][size=14.0pt]metal[/size][size=14.0pt]:[/size][size=14.0pt]description define-slot[/size][size=14.0pt]=[/size][size=14.0pt]"content-description"[/size][size=14.0pt]>[/size][/align][align=left][size=14.0pt] [/size][size=14.0pt]<[/size][size=14.0pt]metal[/size][size=14.0pt]:[/size][size=14.0pt]comment tal:content[/size][size=14.0pt]=[/size][size=14.0pt]"nothing"[/size][size=14.0pt]>[/size][/align][align=left][size=14.0pt] If youwrite a custom description always use[/size][/align][align=left][size=14.0pt] [/size][size=14.0pt]<[/size][size=14.0pt]div class[/size][size=14.0pt]=[/size][size=14.0pt]"documentDescription"[/size][size=14.0pt]></[/size][size=14.0pt]div[/size][size=14.0pt]>[/size][size=14.0pt] for it.[/size][/align][align=left][size=14.0pt] [/size][size=14.0pt]</[/size][size=14.0pt]metal[/size][size=14.0pt]:[/size][size=14.0pt]comment[/size][size=14.0pt]>[/size][/align][align=left][size=14.0pt] [/size][size=14.0pt]<[/size][size=14.0pt]div metal:use-macro[/size][size=14.0pt]=[/size][size=14.0pt]"context/kss_generic_macros/macros/generic_description_view"[/size][size=14.0pt]>[/size][/align][align=left][size=14.0pt] Generic KSSDescription. Is rendered with class="documentDescription".[/size][/align][align=left][size=14.0pt] [/size][size=14.0pt]</[/size][size=14.0pt]div[/size][size=14.0pt]>[/size][/align][align=left][size=14.0pt] [/size][size=14.0pt]</[/size][size=14.0pt]metal[/size][size=14.0pt]:[/size][size=14.0pt]description[/size][size=14.0pt]>[/size][/align][align=left][size=14.0pt] [/size][/align][align=left][size=14.0pt] [/size][size=14.0pt]<[/size][size=14.0pt]div tal:replace[/size][size=14.0pt]=[/size][size=14.0pt]"structure provider:plone.abovecontentbody" [/size][size=14.0pt]/>[/size][/align][align=left][size=14.0pt] [/size][size=14.0pt]<[/size][size=14.0pt]div id[/size][size=14.0pt]=[/size][size=14.0pt]"content-core"[/size][size=14.0pt]>[/size][/align][align=left][size=14.0pt] [/size][size=14.0pt]<[/size][size=14.0pt]metal[/size][size=14.0pt]:[/size][size=14.0pt]text define-slot[/size][size=14.0pt]=[/size][size=14.0pt]"content-core" [/size][size=14.0pt]tal:content[/size][size=14.0pt]=[/size][size=14.0pt]"nothing"[/size][size=14.0pt]> [/size][size=14.0pt]定义一个[/size][size=14.0pt] content-core[/size][size=14.0pt]的槽[/size][/align][align=left][size=14.0pt] Page bodytext[/size][/align][align=left][size=14.0pt] [/size][size=14.0pt]</[/size][size=14.0pt]metal[/size][size=14.0pt]:[/size][size=14.0pt]text[/size][size=14.0pt]>[/size][/align][align=left][size=14.0pt] [/size][size=14.0pt]</[/size][size=14.0pt]div[/size][size=14.0pt]>[/size][/align][align=left][size=14.0pt] [/size][size=14.0pt]<[/size][size=14.0pt]div tal:replace[/size][size=14.0pt]=[/size][size=14.0pt]"structure provider:plone.belowcontentbody" [/size][size=14.0pt]/>[/size][/align][align=left][size=14.0pt] [/size][size=14.0pt]</[/size][size=14.0pt]metal[/size][size=14.0pt]:[/size][size=14.0pt]main[/size][size=14.0pt]>[/size][/align][align=left][size=14.0pt] [/size][/align][align=left][size=14.0pt] [/size][size=14.0pt]</[/size][size=14.0pt]metal[/size][size=14.0pt]:[/size][size=14.0pt]bodytext[/size][size=14.0pt]>[/size][/align][align=left][size=14.0pt] [/size][size=14.0pt]</[/size][size=14.0pt]div[/size][size=14.0pt]>[/size][/align][align=left][size=14.0pt] [/size][size=14.0pt]</[/size][size=14.0pt]metal[/size][size=14.0pt]:[/size][size=14.0pt]slot[/size][size=14.0pt]>[/size][/align][align=left][size=14.0pt] [/size][/align][align=left][size=14.0pt] [/size][size=14.0pt]<[/size][size=14.0pt]metal[/size][size=14.0pt]:[/size][size=14.0pt]sub define-slot[/size][size=14.0pt]=[/size][size=14.0pt]"sub" [/size][size=14.0pt]tal:content[/size][size=14.0pt]=[/size][size=14.0pt]"nothing"[/size][size=14.0pt]>[/size][/align][align=left][size=14.0pt] This slot is here forbackwards-compatibility only.[/size][/align][align=left][size=14.0pt] Don't copy it in customtemplates.[/size][/align][align=left][size=14.0pt] [/size][size=14.0pt]</[/size][size=14.0pt]metal[/size][size=14.0pt]:[/size][size=14.0pt]sub[/size][size=14.0pt]>[/size][/align][align=left][size=14.0pt] [/size][/align][align=left][size=14.0pt] [/size][size=14.0pt]<[/size][size=14.0pt]span id[/size][size=14.0pt]=[/size][size=14.0pt]"contentBottomLeft"[/size][size=14.0pt]></[/size][size=14.0pt]span[/size][size=14.0pt]>[/size][/align][align=left][size=14.0pt] [/size][size=14.0pt]<[/size][size=14.0pt]span id[/size][size=14.0pt]=[/size][size=14.0pt]"contentBottomRight"[/size][size=14.0pt]></[/size][size=14.0pt]span[/size][size=14.0pt]>[/size][/align][align=left][size=14.0pt] [/size][/align][align=left][size=14.0pt] [/size][size=14.0pt]</[/size][size=14.0pt]div[/size][size=14.0pt]>[/size][/align][align=left][size=14.0pt] [/size][size=14.0pt]</[/size][size=14.0pt]metal[/size][size=14.0pt]:[/size][size=14.0pt]block[/size][size=14.0pt]>[/size][/align][align=left][size=14.0pt] [/size][/align][align=left][size=14.0pt] [/size][size=14.0pt]<[/size][size=14.0pt]div id[/size][size=14.0pt]=[/size][size=14.0pt]"viewlet-below-content" [/size][size=14.0pt]tal:content[/size][size=14.0pt]=[/size][size=14.0pt]"structureprovider:plone.belowcontent" [/size][size=14.0pt]/>[/size][size=14.0pt]用名为[/size][size=14.0pt]" plone.belowcontent "[/size][size=14.0pt]的[/size][size=14.0pt]viewletmanager[/size][size=14.0pt]填充[/size][/align][align=left][size=14.0pt] [/size][/align][align=left][size=14.0pt] [/size][/align][align=left][size=14.0pt] [/size][size=14.0pt]</[/size][size=14.0pt]td[/size][size=14.0pt]>[/size][/align][align=left][size=14.0pt] [/size][size=14.0pt]<[/size][size=14.0pt]tal[/size][size=14.0pt]:[/size][size=14.0pt]comment replace[/size][size=14.0pt]=[/size][size=14.0pt]"nothing"[/size][size=14.0pt]>[/size][size=14.0pt] End of maincontent block [/size][size=14.0pt]</[/size][size=14.0pt]tal[/size][size=14.0pt]:[/size][size=14.0pt]comment[/size][size=14.0pt]>[/size][/align][align=left][size=14.0pt] [/size][/align][align=left][size=14.0pt] [/size][size=14.0pt]<[/size][size=14.0pt]tal[/size][size=14.0pt]:[/size][size=14.0pt]notajax tal:condition[/size][size=14.0pt]=[/size][size=14.0pt]"not:ajax_load"[/size][size=14.0pt]>[/size][/align][align=left][size=14.0pt] [/size][size=14.0pt]<[/size][size=14.0pt]tal[/size][size=14.0pt]:[/size][size=14.0pt]comment replace[/size][size=14.0pt]=[/size][size=14.0pt]"nothing"[/size][size=14.0pt]>[/size][size=14.0pt] Start ofright column [/size][size=14.0pt]</[/size][size=14.0pt]tal[/size][size=14.0pt]:[/size][size=14.0pt]comment[/size][size=14.0pt]>[/size][/align][align=left][size=14.0pt] [/size][size=14.0pt]<[/size][size=14.0pt]td id[/size][size=14.0pt]=[/size][size=14.0pt]"portal-column-two"[/size][/align][align=left][size=14.0pt] [/size][size=14.0pt]metal:define-slot[/size][size=14.0pt]=[/size][size=14.0pt]"column_two_slot"[/size][/align][align=left][size=14.0pt] [/size][size=14.0pt]tal:condition[/size][size=14.0pt]=[/size][size=14.0pt]"sr"[/size][size=14.0pt]>[/size][/align][align=left][size=14.0pt] [/size][size=14.0pt]<[/size][size=14.0pt]div class[/size][size=14.0pt]=[/size][size=14.0pt]"visualPadding"[/size][size=14.0pt]>[/size][/align][align=left][size=14.0pt] [/size][size=14.0pt]<[/size][size=14.0pt]metal[/size][size=14.0pt]:[/size][size=14.0pt]portlets define-slot[/size][size=14.0pt]=[/size][size=14.0pt]"portlets_two_slot"[/size][size=14.0pt]>[/size][/align][align=left][size=14.0pt] [/size][size=14.0pt]<[/size][size=14.0pt]tal[/size][size=14.0pt]:[/size][size=14.0pt]block replace[/size][size=14.0pt]=[/size][size=14.0pt]"structure provider:plone.rightcolumn" [/size][size=14.0pt]/>[/size][size=14.0pt]用名为[/size][size=14.0pt]" plone.rightcolumn "[/size][size=14.0pt]的[/size][size=14.0pt]portletmanager[/size][size=14.0pt]填充[/size][/align][align=left][size=14.0pt] [/size][/align][align=left][size=14.0pt] [/size][size=14.0pt]</[/size][size=14.0pt]metal[/size][size=14.0pt]:[/size][size=14.0pt]portlets[/size][size=14.0pt]>[/size][/align][align=left][size=14.0pt] [/size][size=14.0pt]&nbsp;[/size][/align][align=left][size=14.0pt] [/size][size=14.0pt]</[/size][size=14.0pt]div[/size][size=14.0pt]>[/size][/align][align=left][size=14.0pt] [/size][size=14.0pt]</[/size][size=14.0pt]td[/size][size=14.0pt]>[/size][/align][align=left][size=14.0pt] [/size][size=14.0pt]<[/size][size=14.0pt]tal[/size][size=14.0pt]:[/size][size=14.0pt]comment replace[/size][size=14.0pt]=[/size][size=14.0pt]"nothing"[/size][size=14.0pt]>[/size][size=14.0pt] End of theright column [/size][size=14.0pt]</[/size][size=14.0pt]tal[/size][size=14.0pt]:[/size][size=14.0pt]comment[/size][size=14.0pt]>[/size][/align][align=left][size=14.0pt] [/size][size=14.0pt]</[/size][size=14.0pt]tal[/size][size=14.0pt]:[/size][size=14.0pt]notajax[/size][size=14.0pt]>[/size][/align][align=left][size=14.0pt] [/size][size=14.0pt]</[/size][size=14.0pt]tr[/size][size=14.0pt]>[/size][/align][align=left][size=14.0pt] [/size][size=14.0pt]</[/size][size=14.0pt]tbody[/size][size=14.0pt]>[/size][/align][align=left][size=14.0pt] [/size][size=14.0pt]</[/size][size=14.0pt]table[/size][size=14.0pt]>[/size][/align][align=left][size=14.0pt] [/size][size=14.0pt]<[/size][size=14.0pt]tal[/size][size=14.0pt]:[/size][size=14.0pt]comment replace[/size][size=14.0pt]=[/size][size=14.0pt]"nothing"[/size][size=14.0pt]>[/size][size=14.0pt] end column wrapper [/size][size=14.0pt]</[/size][size=14.0pt]tal[/size][size=14.0pt]:[/size][size=14.0pt]comment[/size][size=14.0pt]>[/size][/align][align=left][size=14.0pt] [/size][/align][align=left][size=14.0pt] [/size][size=14.0pt]<[/size][size=14.0pt]div class[/size][size=14.0pt]=[/size][size=14.0pt]"visualClear" [/size][size=14.0pt]id[/size][size=14.0pt]=[/size][size=14.0pt]"clear-space-before-footer"[/size][size=14.0pt]>[/size][size=14.0pt]<!– –>[/size][size=14.0pt]</[/size][size=14.0pt]div[/size][size=14.0pt]>[/size][/align][align=left][size=14.0pt] [/size][/align][align=left][size=14.0pt] [/size][size=14.0pt]<[/size][size=14.0pt]metal[/size][size=14.0pt]:[/size][size=14.0pt]block i18n[/size][size=14.0pt]:[/size][size=14.0pt]domain[/size][size=14.0pt]=[/size][size=14.0pt]"plone" [/size][size=14.0pt]tal:condition[/size][size=14.0pt]=[/size][size=14.0pt]"not:ajax_load"[/size][size=14.0pt]>[/size][/align][align=left][size=14.0pt] [/size][/align][align=left][size=14.0pt] [/size][size=14.0pt]<[/size][size=14.0pt]div tal:replace[/size][size=14.0pt]=[/size][size=14.0pt]"structure provider:plone.portalfooter" [/size][size=14.0pt]/>[/size][/align][align=left][size=14.0pt]用名为[/size][size=14.0pt]" plone.portalfooter "[/size][size=14.0pt]的[/size][size=14.0pt]viewletmanager[/size][size=14.0pt]填充[/size][/align][align=left][size=14.0pt] [/size][/align][align=left][size=14.0pt] [/size][size=14.0pt]</[/size][size=14.0pt]metal[/size][size=14.0pt]:[/size][size=14.0pt]block[/size][size=14.0pt]>[/size][/align][align=left][size=14.0pt] [/size][/align][align=left][size=14.0pt] [/size][size=14.0pt]<[/size][size=14.0pt]div class[/size][size=14.0pt]=[/size][size=14.0pt]"visualClear"[/size][size=14.0pt]>[/size][size=14.0pt]<!– –>[/size][size=14.0pt]</[/size][size=14.0pt]div[/size][size=14.0pt]>[/size][/align][align=left][size=14.0pt] [/size][size=14.0pt]</[/size][size=14.0pt]div[/size][size=14.0pt]>[/size][/align][align=left][size=14.0pt]</[/size][size=14.0pt]body[/size][size=14.0pt]>[/size][/align][align=left][size=14.0pt]</[/size][size=14.0pt]html[/size][size=14.0pt]>[/size][/align][align=left][size=14.0pt]</[/size][size=14.0pt]metal[/size][size=14.0pt]:[/size][size=14.0pt]page[/size][size=14.0pt]>[/size][/align]2. TAL概述模板属性语言(TAL)标准是一种属性语言,用于创建动态模板。它可以实现文档元素的替换、重复或忽略。
TAL中的语句是XML属性。这些属性可以在XMl或HTML中使用,从而实现页面模板的功能。
TAL语句由属性和数值组成。例如,content语句可以这样:
tal:content="string:Hello"大多数的语句需要表达式,但表达式句法并不属于TAL,而是属于TALES。
2.1 TAL 名称空间TAL名称空间URI定义为:
xmlns:tal="http://xml.zope.org/namespaces/tal"当在Zope中创建模板时,不需要用内容类型text/html声明XML名称空间,对于其它内容类型时才需要声明。
2.2 1.2. TAL 语句TAL语句包括:
  • tal:attributes - 动态更改元素属性。
  • tal:define - 定义变量。
  • tal:condition - 测试条件。
  • tal:content - 替换元素中的内容。
  • tal:omit-tag - 忽略一个元素,保留元素内容。
  • tal:on-error - 处理错误。
  • tal:repeat - 重复一个元素。
  • tal:replace - 替换元素的内容,删除元素,保留内容。
语句中的表达式可以返回任何类型的数值,大多数语句只使用字符型数据。表达式语言必须定义一个名为nothing的数值,它不是字符串。这个数值适用于删除元素或属性。
2.3 1.3. 执行顺序如果每个元素中只有一个TAL语句,执行的顺序很简单。从根元素开始,顺序执行每个元素语句。
在同一元素中可以结合多个语句。但content和replace不能一起使用。
由于TAL把语句看作是XML属性,即使在HTML文档中,不能按照编写的顺序来决定执行的顺序。因此需要对执行的顺序进行规定。
当一个元素中含有多个语句时,按照以下顺序执行:
  1. define
  2. condition
  3. repeat
  4. content or replace
  5. attributes
  6. omit-tag
由于on-error语句只有发生错误时才用到,因此没有在此列出来。
按照这样的顺序执行,原因是:由于其它语句会用到变量,因此define先执行。接下来需要判断是否需要使用当前的元素,因此接下来是condition;另外condition依赖于刚才定义的变量,所以define后边是condition。由于通过循环的方式用不同值替换某些元素更具有价值,因此接下来是repeat。由于替换属性没有更多的意义,因此放在后边。
2.4 1.4. 参见TALES 概述
METAL 概述
tal:attributes
tal:define
tal:condition
tal:content
tal:omit-tag
tal:on-error
tal:repeat
tal:replace
3. 2.attributes: 替换元素属性3.1 2.1. 句法tal:attributes 句法:
argument ::= attribute_statement [';' attribute_statement]*attribute_statement ::= attribute_name expressionattribute_name ::= [namespace-prefix ':'] Namenamespace-prefix ::= Name
  • 注意:如果你想在表达式中包含分号(;),必须通过使用两个符号(;;)来使用。
3.2 2.2. 描述tal:attributes语句用一个动态数值替换(或创建一个属性)属性值。如果你通过多个名称空间生成XML文档,需要在属性中加上名称空间前缀,比如 html:table。如果需要的话,每个表达式的值会转换成字符串。
如果表达式结果为nothing,那么就从语句元素中删除。如果表达式结果为默认值,则属性保持不变。每个属性是独立的,因此可以在同一语句中处理多个属性。
如果在一个元素中使用了tal:attributes语句,还使用了tal:replace,则tal:attributes将忽略。
如果同时使用了tal:attributes和tal:repeat语句,每次循环都进行属性替换。
3.3 2.3. 例子替换一个链接:
<a href="/sample/link.html" tal:attributes="href here/sub/absolute_url">替换两个属性:
<textarea rows="80" cols="20" tal:attributes="rows request/rows;cols request/cols">4. 3.condition: 根据条件插入或删除元素4.1 3.1. 句法tal:condition 句法:
argument ::= expression4.2 3.2. 描述根据tal:condition语句,如果符合条件,则在模板中插入元素,否则忽略。如果它的表达式求值为真,则继续处理元素,否则立即从模板中删除。nothing为假,default等同于返回真。
注意:Zope认为缺少变量,None,0,空字符串和空序列为假,其它的值为真。
4.3 3.3. 例子在插入变量前先测试(第一个例子测试是否存在并求值,第二个例子只测试是否存在):
<p tal:condition="request/message | nothing" tal:content="request/message">message goes here</p> <p tal:condition="exists:request/message" tal:content="request/message">message goes here</p>测试多个条件:
<div tal:repeat="item python:range(10)"> <p tal:condition="repeat/item/even">Even</p> <p tal:condition="repeat/item/odd">Odd</p></div>5. 4. content: 替换元素内容5.1 4.1. 句法tal:content 句法:
argument ::= (['text'] | 'structure') expression5.2 4.2. 描述不是替换整个元素,而是在元素内容部分用tal:content替换,插入文本或结构(structure)。这个语句的参数和tal:replace相同。如果表达式结果为nothing,内容部分为空。如果表达式结果为默认值,则内容部分保持不变。
默认的替换方式是文本替换。通过使用structure关键字,可以保持文本不变,即可以插入HTML/XML标记符。
5.3 4.3. 例子插入用户名称:
<p tal:content="user/getUserName">Fred Farkas</p>插入HTML/XML:
<p tal:content="structure here/getStory">marked <b>up</b> content goes here.</p>5.4 4.4. 参见tal:replace
6. 5. define: 定义变量6.1 5.1. 句法tal:define 句法:
argument ::= define_scope [';' define_scope]*define_scope ::= (['local'] | 'global') define_vardefine_var ::= variable_name expressionvariable_name ::= Name
  • 注意:如果你想在表达式中包含分号(;),必须使用两个分号(;;)。
6.2 5.2. 描述tal:define用于定义变量。你可以定义两种类型的TAL变量:本地变量和全局变量。当你在一个元素中定义了一个本地变量,则只能在那个元素和所包含的元素中使用。如果在所包含的元素中重新定义本地变量,那么新的定义替换原来的定义。当你定义了一个全局变量,就可以在定义以后在任何位置使用变量。如果你重新定义了全局变量,从重新定义的位置开始有效。
注意:默认为本地变量。
如果表达式求值结果为nothing,那么变量的值为nothing。如果求值结果为默认值,则变量的值为默认值。
6.3 5.3. 例子定义一个全局变量:
tal:define="global company_name string:Zope Corp, Inc."定义两个变量,第二个变量依赖于第一个:
tal:define="mytitle template/title; tlen python:len(mytitle)"7. 6. omit-tag:删除元素,保留内容7.1 6.1. 句法tal:omit-tag 句法:
argument ::= [ expression ]7.2 6.2. 描述tal:omit-tag语句保留元素中的内容,同时删除起始和结尾部分的标记符。
如果表达式求值结果为假,则继续处理,标记符不删除。如果表达式求值结果为真,或没有提供表达式,则替换元素中的内容。
Zope认为空字符串、空序列、0、None和nothing为假,其它值为真,包括默认值。
7.3 6.3. 例子无条件忽略一个标记符:
<div tal:omit-tag="" comment="This tag will be removed"> <i>…but this text will remain.</i></div>有条件忽略一个标记符:
<b tal:omit-tag="not:bold">I may be bold.</b>上边的例子中,如果变量bold为假,则忽略b标记符。
创建10个p标记符,没有合拢标记符:
<span tal:repeat="n python:range(10)" tal:omit-tag=""> <p tal:content="n">1</p></span>8. 7. on-error:处理错误8.1 7.1. 句法tal:on-error 句法:
argument ::= (['text'] | 'structure') expression8.2 7.2. 描述tal:on-error语句用于处理错误。当一个TAL语句产生错误时,会搜索tal:on-error语句,调用第一个找到的tal:on-error。使用方法如同tal:content。
本地变量error有三个属性:
type
异常的类型
value
异常实例
traceback
回溯对象
常见的错误是文字错误或表达式为nothing。更复杂一点的处理错误的方式是调用一个脚本。
8.3 7.3. 例子显示简单的错误消息:
<b tal:on-error="string: Username is not defined!" tal:content="here/getUsername">Ishmael</b>删除元素:
<b tal:on-error="nothing" tal:content="here/getUsername">Ishmael</b>调用一个脚本:
<div tal:on-error="structure here/errorScript"> …</div>处理错误的脚本可以这样:
## Script (Python) "errHandler"##bind namespace=_##error=_['error']if error.type==ZeroDivisionError: return "<p>Can't divide by zero.</p>"else return """<p>An error ocurred.</p> <p>Error type: %s</p> <p>Error value: %s</p>""" % (error.type, error.value)注意这里的 namespace=_ 可在Script(Python)对象的Bingings标签页面中设置。 namespace表示调用方提供的名字空间。
8.4 7.4. 参见Python Tutorial: Errors and Exceptions
Python Built-in Exceptions
9. 8. repeat: 重复元素9.1 8.1. 句法tal:repeat 句法:
argument ::= variable_name expressionvariable_name ::= Name9.2 8.2. 描述tal:repeat语句根据序列中的每个数据项重复处理元素。表达式求值结果应该为一个序列。如果序列为空,那么这个语句所在的元素被删除,否则重复处理序列中的每一项。如果表达式为默认值,那么不发生改变,并且没有新定义的变量。
9.3 8.3. 循环变量使用循环变量可访问当前循环相关信息,比如循环的序号。循环变量和本地变量一样,但是只能通过内建的变量repeat来调用。
循环变量包括以下信息:
  • index - 循环的序号,从0开始。
  • number - 循环的序号,从1开始。
  • even - 对于偶数项为真(0, 2, 4, …)。
  • odd - 对于奇数项为真(1, 3, 5, …)。
  • start - 对于起始循环为真(index 0)。
  • end - 对于最后的循环为真。
  • first - 对于组中的某个数据第一次出现的时候为真。参见下面的注意。
  • last - 对于组中的某个数据最后一次出现的时候为真。参见下面的注意。
  • length - 序列的长度,即循环总数。
  • letter - 用小写字母表示的循环序号。比如"a" - "z", "aa" - "az", "ba" - "bz", …, "za" - "zz", "aaa" - "aaz",等等。
  • Letter - 用大写字母表示的循环序号。
  • roman - 以小写罗马数字表示的循环序号,比如:"i", "ii", "iii", "iv", "v", 等等。
  • Roman - 以大写罗马数字表示的循环序号。
你可以通过路径表达式或Python表达式访问循环变量。在路径表达式中,需要用三部分组成名称,即repeat、语句变量名称和要访问的变量名称。比如:
repeat/item/start在Python表达式中,使用字典方法来访问信息,比如:
python:repeat['item'].start除了start、end和index,所有repeat变量的属性都是方法。因此,当你用Python表达式时,必须这样来调用:
python:repeat['item'].length()** 注意 ** first和last适用于过滤序列。它们试图根据相同值把序列分成组。如果提供了一个路径,那么根据这个路径取得的数值进行分组,否则使用数据项的值。你可以通过传递参数提供路径,比如"python:repeat['item'].first(color)",或通过repeat变量附加路径,比如,"repeat/item/first/color"。
9.4 8.4. 例子对字符串序列进行循环:
<p tal:repeat="txt python:'one', 'two', 'three'"> <span tal:replace="txt" /></p>插入表格行,使用变量repeat显示行号:
<table> <tr tal:repeat="item here/cart"> <td tal:content="repeat/item/number">1</td> <td tal:content="item/description">Widget</td> <td tal:content="item/price">$1.50</td> </tr></table>嵌套的循环:
<table border="1"> <tr tal:repeat="row python:range(10)"> <td tal:repeat="column python:range(10)"> <span tal:define="x repeat/row/number; y repeat/column/number; z python:x*y" tal:replace="string:$x * $y = $z">1 * 1 = 1</span> </td> </tr></table>插入对象,按照meta-type进行分组:
<div tal:repeat="object objects"> <h2 tal:condition="repeat/object/first/meta_type" tal:content="object/meta_type">Meta Type</h2> <p tal:content="object/getId">Object ID</p> <hr tal:condition="repeat/object/last/meta_type" /></div>注意,上边例子中的对象应该已经按照meta-type进行了排序。
10. 9. replace: 替换元素10.1 9.1. 句法tal:replace 句法:
argument ::= (['text'] | 'structure') expression10.2 9.2. 描述tal:replace语句的作用是用动态内容替换一个元素。替换的内容可以是文本或结构。表达式中可加上类型前缀。表达式结果会自动转换成字符类型。如果使用了前缀structure,则会在输出文本中保持特殊字符不变;否则会进行转换:即把"&" 转换成 "&amp", 把"<" 转换成 "&lt;", 把">" 转换成 "&gt;"。
如果为nothing,则只删除元素。如果为默认值,元素保持不变。
10.3 9.3. 例子插入模板标题的两种方式:
<span tal:replace="template/title">Title</span><span tal:replace="text template/title">Title</span>插入HTML/XML:
<div tal:replace="structure table" />插入nothing(空):
<div tal:replace="nothing">This element is a comment.</div>10.4 9.4. 参见tal:content
11. 10. TALES 概述模板属性语言表达式句法(TALES)描述了用于TAL和METAL的表达式。TALES提供了多种表达类型。
以下是基本的TALES句法:
Expression ::= [type_prefix ':'] Stringtype_prefix ::= Name以下是一些例子:
a/b/c path:a/b/c nothing path:nothing python: 1 + 2 string:Hello,${user/getUserName}表达式前边可加上可选的类型前缀。如果不指定一个前缀,默认为路径表达式。
11.1 10.1. TALES表达式类型:
  • path 表达式 - 根据路径确定数值。
  • exists 表达式 - 测试路径是否有效。
  • nocall 表达式 - 根据路径定位一个对象。
  • not 表达式 - 逻辑非表达式
  • string 表达式 - 字符串格式
  • python 表达式 - 运行Python表达式
11.2 10.2. 内建名称TALES表达式中可使用以下名称:
  • nothing - 用于表示空值的特殊值。
  • default - 用于指定不可替换的文本。
  • options - 传递给模板的关键字参数。通常出现在通过方法和脚本调用模板的时候。
  • repeat - tal:repeat语句中的repeat变量。
  • attrs - 包含当前语句初始属性值的字典。
  • CONTEXTS - 标准名称的列表。用于访问隐藏的内建变量。
  • root - 系统中最顶级的对象:根文件夹。
  • here - 应用模板的对象。
  • container - 模板所属的文件夹。
  • template - 模板本身。
  • request - 请求对象。
  • user - 已验证用户对象。
  • modules - 可以访问的模块。
注意 root, here, container, template, request, user, 和 modules是可选的名称,TALES标准中不要求必须使用。
11.3 10.3. 参见TAL Overview
METAL Overview
exists expressions
nocall expressions
not expressions
string expressions
path expressions
python expressions
12. 11.TALES Exists 表达式12.1 11.1. 句法Exists 表达式句法:
exists_expressions ::= 'exists:' path_expression12.2 11.2. 描述Exists表达式测试路径是否存在。如果路径存在返回真。当不能定位一个对象时,返回假。
12.3 11.3. 例子测试一个表单变量:
<p tal:condition="not:exists:request/form/number"> Please enter a number between 0 and 5</p>注意,此时不能使用 not:request/form/number,这是由于如果变量number存在并为0,表达式值将为真。
13. 12.TALES Nocall 表达式13.1 12.1. 句法Nocall 表达式句法:
nocall_expression ::= 'nocall:' path_expression13.2 12.2. 描述Nocall 表达式避免了调用路径表达式的结果。
通常路径表达式会调用对象。也就是说,如果对象是函数、脚本方法或其它可执行对象,则表达式会调用这些对象运行的结果。大多数情况下需要这样,但不总需要这样。例如,如果你想通过一个变量使用DTML文档,然后调用它的属性,此时就不能使用通常的路径表达式,否则会得到文档运行后的字符串。
13.3 12.3. 例子使用nocall得到文档属性:
<span tal:define="doc nocall:here/aDoc" tal:content="string:${doc/getId}: ${doc/title}">Id: Title</span>对一个函数使用nocall表达式:
<p tal:define="join nocall:modules/string/join">这个例子定义了一个变量join,它绑定给string.join函数。
14. 13. TALESNot 表达式14.1 13.1. 句法Not 表达式句法:
not_expression ::= 'not:' expression14.2 13.2. 描述Not表达式先对expression字符串求值,然后返回它的布尔中的逻辑非值。如果提供的表达式求值后不是一个布尔值,则会根据以下规则转换成布尔类型:
  1. 数字 0 为假
  2. 正数和负数为真
  3. 空的字符串或其它序列为假
  4. 非空的字符串或其它序列为真
  5. 空值为假,比如:void, None, Nil, NULL等等。
  6. 其它值为真。
如果没有提供expression字符串,会引发错误。
14.3 13.3. 例子测试一个序列:
<p tal:condition="not:here/objectIds"> There are no contained objects.</p>15. 14. TALESPath 表达式15.1 14.1. 句法Path 表达式句法:
PathExpr ::= Path [ '|' Expression ]Path ::= variable [ '/' PathSegment ]*variable ::= NamePathSegment ::= ( '?' variable ) | PathChar+PathChar ::= AlphaNumeric | ' ' | '_' | '-' | '.' | ',' | '~'15.2 14.2. 描述路径表达式由一个路径组成,也可以在后边通过竖线符号(|)增加另外一个表达式,如果前一个表达式无效则使用后一个。路径由一个或多个非空字符串组成,用/分开。第一个字符串必须为一个变量名称(内建变量或用户定义的变量),其余部分可以包含字母、数字、空格和符号"_","-",".","~"。
表达式中可以使用符号?表示一个动态变量,这个变量必须是一个字符串,运行时会替换成对应的字符串。
例如:
request/cookies/oatmealnothinghere/some-file 2001_02.html.tar.gz/fooroot/to/branch | default request/name | string:Anonymous Cowardhere/?tname/macros/?mname当对表达式求值时,从左到右依次处理路径。
如果出现错误,显示错误信息。
附加的表达式可以是任何TALES表达式。比如,request/name| string:Anonymous Coward,就是一个有效的路径表达式。通过这个表达式,可以提供默认值。也可以提供多个附加的路径表达式,比如,first | second | third | nothing。
如果没有路径,则为nothing。
由于每个路径必须以一个变量名称开始,因此这个起始变量应该能够找到相应的对象才行。参见前边列出的内建变量。变量名称先在本地搜索,然后在内建变量列表中搜索,因此内建变量就像Python中内建的变量一样。也可以在变量名称前声明范围。你也可以直接通过CONTEXTS访问内建变量,比如, CONTEXTS/root, CONTEXTS/nothing。
15.3 14.3. 例子插入一个cookie变量或一个属性:
<span tal:replace="request/cookies/pref | here/pref"> preference</span>插入用户名称:
<p tal:content="user/getUserName"> User name</p>16. 15.TALES Python 表达式16.1 15.1. 句法Python 表达式句法:
任何有效的Python语言表达式16.2 15.2. 描述Python表达式在安全限制环境内对Python代码求值。Python表达式和脚本对象和DTML中的变量表达式一样。
16.3 15.2.1. 安全限制Python表达式的限制和脚本对象中的一样,这些限制包括:
访问限制
Python 表达式受到Zope许可和角色安全的限制。另外,表达式还不能访问那些名称用下划线开头的对象。
写入限制
Python表达式不能更改Zope对象属性。
16.4 15.2.2. 内建函数Python表达式和脚本对象中的内建函数一样,但增加了一些。
标准的内建函数包括: None, abs, apply, callable, chr, cmp, complex,delattr, divmod, filter, float, getattr, hash, hex, int, isinstance,issubclass, list, len, long, map, max, min, oct, ord, repr, round, setattr,str, tuple.
range和pow函数不能生成非常巨大的数值和序列。
另外,还可以使用:DateTime,test,same_type。参考DTML 函数部分。
最后,可以在Python表达式中使用这些函数:
path(string)
对一个TALES path表达式求值。
string(string)
对一个TALESstring表达式求值。
exists(string)
对一个TALESexists表达式求值。
nocall(string)
对一个TALESnocall表达式求值。
16.5 15.2.3. Python模块可以使用默认提高的模块,也可以增加模块。访问模块的方式可以通过路径表达式,比如:modules/string/join,也可以在Python中使用module映射对象,比如:modules['string'].join。以下是默认的模块:
string
string模块。注意,模块中的大多数函数也可以通过字符串对象的方法进行访问。
random
随机模块。
math
数学模块。
sequence
具有强大排序功能的模块。
Products.PythonScripts.standard
提供各种HTML格式处理函数。
ZTUtils
提供批块处理功能。
AccessControl
提供安全和权限检查功能。
16.6 15.2.4. 例子使用一个模块,从列表中随机选择一个数据项:
<span tal:replace="python:modules['random'].choice(['one', 'two', 'three', 'four', 'five'])"> a random number between one and five</span>字符串处理,把用户名变成大写格式:
<p tal:content="python:user.getUserName().capitalize()"> User Name</p>数学处理,把图像大小转换成兆字节:
<p tal:content="python:image.getSize() / 1048576.0"> 12.2323</p>处理字符串格式,把浮点型格式为两个小数位:
<p tal:content="python:'%0.2f' % size"> 13.56</p>17. 16.TALES String 表达式17.1 16.1. 句法String 表达式句法:
string_expression ::= ( plain_string | [ varsub ] )*varsub ::= ( '$' Path ) | ( '${' Path '}' )plain_string ::= ( '$$' | non_dollar )*non_dollar ::= any character except '$'17.2 16.2. 描述字符串表达式按照文本处理字符串。如果没有提供字符串,则为空。字符串中可以通过$name或${path}格式使用变量,其中name为变量名称,path为路径表达式。如果要插入$符号,需要使用$$。
17.3 16.3. 例子处理字符串:
<span tal:replace="string:$this and $that"> Spam and Eggs</span>使用路径:
<p tal:content="total: ${request/form/total}"> total: 12</p>包含一个$符号:
<p tal:content="cost: $$$cost"> cost: $42.00</p>18. 17. METAL 概述宏扩展模板属性语言(METAL)用于处理HTML/XML宏(Macro)。它可以结合TAL和TALES一起使用。
宏提供了一种共享模板数据的方式。如果宏发生了变化,那么相应的共享数据也会发生变化。另外,宏可以扩展,从而具有最终结果的样子,方便编辑处理。
18.1 17.1. METAL 名称空间METAL名称空间URI定义为:
xmlns:metal="http://xml.zope.org/namespaces/metal"当创建内容类型具有text/html的模板时,Zope不要求声明XML名称空间。但对于其它类型则需要声明。
18.2 17.2. METAL 语句METAL定义的语句包括:
  • metal:define-macro - 定义一个宏。
  • metal:use-macro - 使用一个宏。
  • metal:define-slot - 定义一个宏定制点。
  • metal:fill-slot - 定制一个宏
18.3 参见TAL Overview
TALES Overview
metal:define-macro
metal:use-macro
metal:define-slot
metal:fill-slot
19. 18.define-macro: 定义一个宏19.1 18.1. 句法metal:define-macro 句法:
argument ::= Name19.2 18.2. 描述metal:define-macro语句定义一个宏。通过语句表达式进行定义,有效返回是当前所在的元素和下级元素。
可以通过模板的macros对象调用宏定义。比如,在模板master.html中定义了宏header,那么你可以使用路径表达式master.html/macros/header访问这个宏。
19.3 18.3. 例子简单的宏定义:
<p metal:define-macro="copyright"> Copyright 2001, <em>Foobar</em> Inc.</p>19.4 参见metal:use-macro
metal:define-slot
20. 19.define-slot: 定义一个宏定制点20.1 19.1. 句法metal:define-slot 句法:
argument ::= Name20.2 19.2. 描述metal:define-slot语句定义了一个宏定制点,或内容块(slot)。当使用一个宏时,这个内容块可以替换成其它内容。内容块定义提供了默认的内容。如果不调用内容块,则显示默认的内容。
metal:define-slot语句必须在metal:define-macro之中。
内容块的名称必须唯一。
20.3 19.3. 例子简单的内容块定义:
<p metal:define-macro="hello"> Hello <b metal:define-slot="name">World</b></p>这个例子定义了一个宏和一个名为name的内容块。当你使用这个宏的时候,可以在b元素中添加进定制的内容。
20.4 19.4. 参见metal:fill-slot
21. 20.fill-slot: 定制一个宏21.1 20.1. 句法metal:fill-slot 句法:
argument ::= Name21.2 20.2. 描述metal:fill-slot语句通过替换宏中的内容块来定制宏。
metal:fill-slot 语句必须在metal:use-macro中使用。
内容块名称必须唯一。
如果宏中不存在指定的内容块,则删除这个内容块。
21.3 20.3. 例子先定义宏:
<p metal:define-macro="hello"> Hello <b metal:define-slot="name">World</b></p>你可以这样来填充内容块name:
<p metal:use-macro="container/master.html/macros/hello"> Hello <b metal:fill-slot="name">Kevin Bacon</b></p>21.4 20.4. 参见metal:define-slot
22. use-macro: 使用一个宏22.1 句法metal:use-macro 句法:
argument ::= expression22.2 21.2. 描述metal:use-macro语句用宏替换当前元素中的内容。expression描述了一个宏定义。
expression将生成一个路径表达式,用来指向另外一个模板中定义的宏。参见metal:define-macro部分。
扩展宏的效果就是从另外一个文档或当前文档中提取部分内容,然后放进当前元素之中,即替换当前的内容。原来的内容保持不变,如果使用了内容块,则内容块中使用新的内容。
当扩展宏时,使用metal:use-macro语句。
22.3 例子基本用法:
<p metal:use-macro="container/other.html/macros/header"> header macro from defined in other.html template</p>这个例子引用了在other.html模板中定义的header。当扩展这个宏时,p元素和它的内容将替换成宏中的内容。
22.4 参见metal:define-macro
metal:fill-slot
23. 22. ZPT特定的行为Zope页面模板的行为几乎完全可以通过TAL、TALES和METAL进行描述。但还有一些特定的行为。
23.1 HTML支持的特性当一个页面模板的内容类型设置为text/html,处理过程会有些不同于其它内容类型。在TAL名称空间中讲过,HTML不需要声明名称空间,默认>提供TAL和METAL名称空间。
HTML文档采用非XML解析器,因此可以处理一些不符合XML要求的标记符。特别是那些没有结束标记符的元素,比如段落和列表,但还不能认为>是错误,除非它们是语句元素。这样就容易导致错误,比如使用用<div>元素,但不嵌套在<p>中,此时由于</p>标记符,则会引发一个NestingError错误。因此解决的方法是使用<span>。
没有合拢的语句元素通常认为是错误,因此应该尽量指明合拢的标记符。对于那些没有结束标记符的元素,比如image和input元素,不要求加>上合拢标记符或使用XHTML中的<tag/>格式。
某些布尔属性,比如checked和selected,在tal:attributes中处理方式不同。数值为真或假。如果为真,则会处理成attr="attr"格式,如果为假,则会忽略。如果值为default,则此时,如果属性已经存在认为是真,不存在为假。比如:
<input type="checkbox" checked tal:attributes="checked default"><input type="checkbox" tal:attributes="checked string:yes"><input type="checkbox" tal:attributes="checked python:42">结果为:
<input type="checkbox" checked="checked">对于:
<input type="checkbox" tal:attributes="checked default"><input type="checkbox" tal:attributes="checked string:"><input type="checkbox" tal:attributes="checked nothing">将生成:
<input type="checkbox">这种处理方式可以在所有的浏览器中正确显示。
24. METAL(理解Plone高级模板技术页面模板的一个优点是将不同的功能清晰的分离到不同的名字空间。在前面的章节中,你看到了TAL名字空间。他不是页面模板唯一提供的名字空间;另外是METAL。如其全面所暗示的,他类似TAL,也是一个属性语言,把他自己插入到元素属性中。然而,他的主要目标是保证你能够重复使用其他的页面模板的大段代码。他通过使用槽(slot)和宏(macro)实现。
24.1 使用METAL挂接(hook)进Plone目前为止,你看到如何使用TAL来动态构建页面的某些部分。然而,这个并不能让你构建大量的复杂模板系统。除了TAL,还没有一个机制,让你能够把标准的头放到每个页面上方。METAL是一个预先处理模板并提供更多比TAL更加强大的功能。所有的METAL功能都以 metal: 开始。
24.1.1 metal:define-macroThe metal:define-macro command allows you to define an element toreference from another template. The name of the referenced chunk is the nameof the macro. The following is an example that defines boxA as a piece youwant to use elsewhere:
<div metal:define-macro="boxA"> …</div>这个 div 元素就是一个能够被其他模板引用的宏。这个宏仅仅代表页面中被那个元素所指向的部分。这里,也就是这个 div 标记。因此,很常见的是在一个页面中使用多个 macro:defines ,为了让这个页面成为一个有效的HTML页面,可如下:
<html xmlns:tal="http://xml.zope.org/namespaces/tal" xmlns:metal="http://xml.zope.org/namespaces/metal" i18n:domain="plone"> <body> <div metal:define-macro="boxA"> … </div> <div metal:define-macro="boxB"> … </div> </body></html>Corresponding with the earlier goals of page templates,this page is a valid HTML page that can be edited by a designer. When the macrois called, the HTML outside the div tags will be discarded.
24.1.2 metal:use-macroThe metal:use-macro command uses a macro that has been definedusing the define-macro.When a template defines a macro using the define-macro command, it's accessible toother templates through a macros property. For example, if you want to pull the portlet macro out ofthe portlet_logintemplate, you can do the following:
<div metal:use-macro="context/portlet_login/macros/portlet"> The about slot will go here</div>This will fetch the macro and insert the result in itsplace. As shown, the use-macrocommand takes a path expression that points to the template and then to thespecific macro in the template.
Example: Using the use-macro and define-macro Macros
24.1.3 示例:使用use-macro和define-macro宏As an example of this, the following is a template called time_template. Thistemplate shows the date and time on the current Plone server. This is quite auseful function to have, so you can wrap this in a macro to be reused. This isthe example page template containing the define-macro:
<html> <body> <div metal:define-macro="time"> <div tal:content="context/ZopeTime"> The time </div> </div> </body></html>If your template is called time_template, then you can referencethis macro in another template. You can now reference this macro in multipletemplates. This is an example template:
<html> <body> <div metal:use-macro="context/time_template/macros/time"> If there is a message then the macro will display i here. </div> </body></html>When this template is rendered, the HTML produced by Plonelooks like this:
<html> <body> <div> <div>2004/04/15 17:18:18.312 GMT-7</div> </div> </body></html>24.1.4 metal:define-slotA slot is a section of a macro that the template author expectsto be overridden by another template. You could think of it as a hole in yourpage template that you're expecting something else to fill in. All define-slot commandsmust be contained within a define-macro. For example:
<div metal:define-macro="master"> <div metal:define-slot="main"> … </div></div>24.1.5 metal:fill-slotThis completes a slot that has been defined with the use-slot command. A fill-slot must bedefined with a use-macrocommand. When the define-macro part is called, the macro will attempt fill in all the defineslots with the appropriate fill-slots. Here's an example fill-slot:
<div metal:use-macro="master"> <div metal:fill-slot="main"> The main slot will go here </div></div>24.1.6 示例:使用宏和槽Returning to the previous example, you'll now enhance it alittle. If you wanted to put a custom message at the beginning of the time,then you'd add a slot at the beginning of the time_template, inside the define-macro. Theslot is called timeand is as follows:
<html> <body> <div metal:define-macro="time"> <div metal:define-slot="msg">Time slot</div> <div tal:content="context/ZopeTime"> The time </div> </div> </body></html>Now, in the calling page template, you can call the fill-slot:
<html> <body> <div metal:use-macro="context/time_template/macros/time"> <div metal:fill-slot="msg">The time is:</div> If there is a message then the macro will display i here. </div> </body></html>The end result is that you'll see the time-slot filled inas follows:
<html> <body> <div> <div>The time is: </div> <div>2004/04/15 17:18:18.312 GMT-7</div> </div> </body></html>24.1.7 Plone是如何使用宏和槽的Both macros and slots are similar inasmuch as they bothextract content from another template and insert content, but they do thisdifferently. The difference comes in how they're used: Macros are elements of atemplate that are explicitly called, but slots are like holes in a templatethat you expect other templates to fill in for you. For example, in Plone'scase, the portlets such as the calendar, navigation, and so on are macros thatare explicitly called.
In fact, if in the Zope Management Interface (ZMI) you lookat the file by clicking portal_skins, clicking plone_templates, and then clicking main_template,you'll see that the entire page consists of macros and slots. At this stage,it's probably a little confusing, but when called, it runs through a series ofmacros and pulls everything together. This allows a user to easily change anypart of a Plone site by overriding that macro, as you'll see in the nextchapter. For example:
…<div metal:use-macro="here/global_siteactions/macros/site_actions"> Site-wide actions (Contact, Sitemap, Help, Style Switcher etc)</div> <div metal:use-macro="here/global_searchbox/macros/quick_search"> The quicksearch box, normally placed at the top right</div>…Continuing to scroll down through main_template,you'll encounter some define slots. Briefly I'll recap how a page in Plone isrendered. When an object is shown, a template for that view of that content isshown. When you view an image, the template image_view is shown, and that templatecontrols how the image is shown. To do this task, the image template fills the main slot. If youlook in the image_viewtemplate, you'll see the following code defined in that template:
<div metal:fill-slot="main">…</div>If you jump back to the main_template, you'll see that it containsa define-slotdefinition for the slot main:
<metal:bodytext metal:define-slot="main" tal:content="nothing"> Page body text</metal:bodytext>Each type of content has a different template, and eachtemplate defines how they'll use the main slot differently. So, then eachcontent type has its own particular look and feel from the templates. Just oneelement is missing in the equation. Somehow when you called image_view, thetemplate knew it should use main_template. In the image_view template, you use the macrofrom the main_template.This is defined in the following HTML:
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US" metal:use-macro="here/main_template/macros/master" i18n:domain="plone">In this case, the main_template has the main slot filled inby the slot defined as main in the template being rendered. The following is atimeline of how the page is built for viewing an image:
image_view main_templateimage_viewmain_templatedefine-slot="main"fill-slot image_view fill-slot main_template main_template Thisallows Plone to be flexible in terms of how each page is defined. For example, main_templatedefines just more than that one slot; there's also a slot for insertingCascading Style Sheets (CSS) code:
<metal:cssslot fill-slot="css_slot"> <metal:cssslot define-slot="css_slot" /></metal:cssslot>If a view needed a custom set of CSS, you could define thisslot in the view, and it'd be filled in when rendering. Some of the macros in main_template defineslots in them as well and then fill them back in the main_template sothat if you really wanted, you could also fill in those slots. That's an advancedtechnique, however, so you'll want to ensure you have the basics down beforegoing down that road.
25. 以collective.conference为例browser/templates/agenda.pt


设置