这篇文章给大家介绍的内容是关于php TAL模板引擎语法,内容很详细,有需要的朋友可以参考一下,希望可以帮助到大家。
本文档的使用到的数据($talData)
//定义talData变量 $talData = array(); $talData['title'] = 'title1'; $talData['key'] = 'key1'; $talData['href'] = 'xxx.html'; $talData['html'] = '<h1>h1标题</h1>'; $talData['fun'] = function($key=''){ return $key === 'key1'? true : false; }; //两维数组 $talData['list1'] = array( array('id'=>'1', 'title'=>'标题1'), array('id'=>'2', 'title'=>'标题2'), array('id'=>'3', 'title'=>'标题3'), array('id'=>'4', 'title'=>'标题4') );
//输出的数据结构 {"title":"title1","key":"key1","href":"xxx.html","html":" h1\u6807\u9898<\/h1>","fun":{},"list1":[{"id":"1","title":"\u6807\u98981"},{"id":"2","title":"\u6807\u98982"},{"id":"3","title":"\u6807\u98983"},{"id":"4","title":"\u6807\u98984"}]}
define 定义变量
condition
repeat
content or replacae
attributes 属性
omit-tag
<p tal:define="global title talData/title; key talData/key"> ${title} && ${key} </p> ${title} && ${key} <!-- 结果 --> <p>title1 && key1</p> title1 &&
<p tal:define="global fname string:fname1 string; lname string:lname1 string;"> ${fname} && ${lname} </p> ${fname} && ${lname} <!-- 结果 --> <p>fname1 string && lname1 string</p> fname1 string &&
<p tal:define="global hello string:hello $fname welcome on this page"> ${hello} </p> 或 <p tal:define="global hello string:hello ${fname} welcome on this page"> ${hello} </p> <!-- 结果 --> <p>hello fname1 string welcome on this page</p>
<p tal:define="global rand php: rand(1, 4)"> ${rand} </p> <!-- 结果 --> <p>2</p>
<tal:block> <p>渲染1</p> </tal:block> <tal:block tal:replace=""> 不渲染这里 </tal:block> <tal:block tal:replace="talData/title"> <p>不渲染这里</p> </tal:block> <p>${talData/title}</p> <!-- 结果 --> <p>渲染1</p> title1 <p>title1</p>
<p tal:content="talData/title"> test data<span>tips</span> </p> <!-- 结果 --> <p>title1</p>
<p class="p p_${talData/title}">${talData/title}</p> <!-- 结果 --> <p class="p p_title1">title1</p>
<a href="http://www.foo.com" title="some foo link" tal:attributes="href talData/href; title talData/title" tal:content="talData/key">sample link</a> <!-- 结果 --> <a href="xxx.html" title="title1">key1</a>
<span tal:on-error="string: 当前数据不存在" tal:content="talData/key">key 已经是定义了</span> <span tal:on-error="string: 当前数据不存在" tal:content="talData/ky">ky 没有定义的</span> <!-- 结果 --> <span>key1</span> 当前数据不存在
<p tal:condition="php: true" tal:content="talData/title">标题</p> <p tal:condition="php: talData['key'] == 'key1'" tal:content="talData/key">关键词</p> <p tal:condition="php: key == 'key1'" tal:define="key talData/key" tal:content="talData/key">关键词</p> <p tal:condition="talData/key" tal:content="talData/key">关键词</p> <p tal:condition="talData/fun" tal:content="talData/key">关键词</p> <!-- 结果 --> <p>title1</p> <p>key1</p> <p>key1</p> <p>key1</p>
<ul tal:repeat="item talData/list1"> <li tal:condition="php: repeat.item.index == 0"> repeat/item/key: ${repeat/item/key} ;(如果talData/list1是一组联合对象,返回item的键)<br/> repeat/item/index: ${repeat/item/index} ;(返回索引号(0~count-1))<br/> repeat/item/number: ${repeat/item/number} ;(返回索引号(1~count))<br/> repeat/item/even: ${repeat/item/even} ;(如果是偶数,true)<br/> repeat/item/odd: ${repeat/item/odd} ;(如果是奇数,true)<br/> repeat/item/start: ${repeat/item/start} ;(如果是第一个元素,true)<br/> repeat/item/end: ${repeat/item/end} ;(如果是最后一个元素,true)<br/> repeat/item/length: ${repeat/item/length} ;(返回talData/list1里面元素个数)<br/> ------------- </li> <li tal:condition="php: repeat.item.index > 0" tal:content="item/title">列表li</li> </ul> <!-- 结果 --> <li> repeat/item/key: 0 ;(如果talData/list1是一组联合对象,返回item的键)<br/> repeat/item/index: 0 ;(返回索引号(0~count-1))<br/> repeat/item/number: 1 ;(返回索引号(1~count))<br/> repeat/item/even: 1 ;(如果是偶数,true)<br/> repeat/item/odd: 0 ;(如果是奇数,true)<br/> repeat/item/start: 1 ;(如果是第一个元素,true)<br/> repeat/item/end: 0 ;(如果是最后一个元素,true)<br/> repeat/item/length: 4 ;(返回talData/list1里面元素个数)<br/> </li> <li>标题2</li> <li>标题3</li> <li>标题4</li>
<p tal:omit-tag="php: false"> omit-tag值为真,不出现当前p标签,否则就出现当前p标签 </p> <p tal:omit-tag="php: true"> omit-tag值为真,不出现当前p标签,否则就出现当前p标签 </p> <!-- 结果 --> <p>omit-tag值为真,不出现当前p标签,否则就出现当前p标签</p> omit-tag值为真,不出现当前p标签,否则就出现当前p标签
<tal:block tal:repeat="x php:range(1,10)" tal:content="x">文字会出现十次</tal:block> <!-- 结果 --> 12345678910
<!-- 在pWeb/_macro/common.html文件中代码 --> <tal:block metal:define-macro="WinPageData"> <script type="text/javascript"> window.WinPageData = ${structure WinPageData}; </script> </tal:block>
<tal:block metal:use-macro="pWeb/_macro/common.html/WinPageData"> 当前内容会被宏替换掉 <script type="text/javascript"> window.WinPageData = {}; </script> </tal:block> <p metal:use-macro="${路径变量}/macros.html/main_menu"></p>
<!-- 在pWeb/_macro/mlog.html文件中代码 --> <tal:block metal:define-macro="mobile"> <script> <metal:block define-slot="mlog_page"> var mlog_page = 'mobile_other'; </metal:block> var mlog_track = function(){}; </script> <script src='xxx.js'></script> </tal:block>
<tal:block metal:use-macro="pWeb/_macro/mlog.html/mobile"> <tal:block metal:fill-slot="mlog_page"> var mlog_page = 'mobile_index'; </tal:block> </tal:block>
<!-- 在pWeb/_macro/mlog.html文件中代码 --> <tal:block metal:define-macro="conent"> <p>${title}</p> <p>${key}</p> </tal:block>
<tal:block metal:use-macro="pWeb/_macro/mlog.html/conent" tal:define="title '标题'; key '关键词'"></tal:block>
phptal:debug
<p class="footer" phptal:cache="3h">...</p> 有效期可以有选择的跟随'per'参数来定义多少个缓存被共享,使用'per url'分别针对每个url复制元素. <ol id="breadcrumbs" phptal:cache="1d per url">...</ol>
php:htmlentities(foo) php:'string ${varReplaced}' php:'string ${some.path().to[0].var}' php:foo.a.b.c(e).htmlentities(SomeClass::staticMethod()) php:SomeClass::ConstOfClass php:SomeClass::$staticVar
<span tal:condition="not: logged">not logged</span> <!-- 结果 --> <span>not logged</span>
<span tal:condition="exists: talData/title" tal:content="talData/title">存在</span> <!-- 结果 --> <span>title1</span>
<span tal:define="myVar talData/title | default"> default my var value </span> <p tal:content="some/var | other/path | default"> 没有var,没有找到path </p> <a href="unknown.html" title="Unknown page" tal:attributes="href item/href | default; title talData/title | default" tal:content="item/title | default">Unknown page</a> <!-- 结果 --> <p>没有var,没有找到path</p> <a href="unknown.html" title="title1">Unknown page</a>
注意存在XSS攻击风险,谨慎使用
<p tal:content="talData/html"></p> <p tal:content="structure talData/html"></p> <!-- 结果 --> <p><h1>h1标题</h1></p> <p><h1>h1标题</h1></p>
<p>${talData/html}</p> <p>${structure talData/html}</p> <!-- 结果 --> <p><h1>h1标题</h1></p> <p><h1>h1标题</h1></p>
相关文章推荐:
PHP的学习--PHP加密,PHP学习--PHP加密_PHP教程
Atas ialah kandungan terperinci PHP中TAL模板引擎语法的解析(代码). Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!