Table of Contents
PHP 数据
属性优先权
TAL有效空间
定义变量
定义全局(global)变量title和局部变量 key
定义默认值变量" >定义默认值变量
定义拼接变量" >定义拼接变量
定义变量,执行php方法
输出内容
tal:replace="数据" 将用值替代整个标签,如果没有赋值将替空" >tal:replace="数据" 将用值替代整个标签,如果没有赋值将替空
tal:content="数据" 将会替代标签内的内容" >tal:content="数据" 将会替代标签内的内容
${数据}" >${数据}
tal:attributes 将会改变tag的属性或值" >tal:attributes 将会改变tag的属性或值
tal:on-error="string: 出错时显示内容" ;出错时显示error内容,并且没有当前标签;" >tal:on-error="string: 出错时显示内容" ;出错时显示error内容,并且没有当前标签;
判断条件
tal:condition="数据",条件是true,标签和它的内容就会被显示" >tal:condition="数据",条件是true,标签和它的内容就会被显示
循环
tal:repeat="赋值变量 数据";属性循环它的元素和内容直到结束" >tal:repeat="赋值变量 数据";属性循环它的元素和内容直到结束
标签渲染与否
tal:omit-tag="condition";要求phptal解析时忽略实体的开关闭标签,它的内容仍然会显示." >tal:omit-tag="condition";要求phptal解析时忽略实体的开关闭标签,它的内容仍然会显示.
<tal:block>;代替标签,标签永远不输出" ><tal:block>;代替标签,标签永远不输出
METAL空间 支持宏
metal:define-macro 定义宏" >metal:define-macro 定义宏
metal:use-macro 调用宏" >metal:use-macro 调用宏
metal:define-slot 定义宏标签替换" >metal:define-slot 定义宏标签替换
metal:fill-slot  使用宏标签替换" >metal:fill-slot  使用宏标签替换
宏获取参数" >宏获取参数
宏传参数(tal:define)
phptal空间
phptal:cache 使整个元素(包括标签)保存在磁盘而不重新解析直到cache失效,有效期的格式由数字和'd', 'h','m'或's'组成." >phptal:cache 使整个元素(包括标签)保存在磁盘而不重新解析直到cache失效,有效期的格式由数字和'd', 'h','m'或's'组成.
使用php:语法
如下所述同正规表达式,除了->被替换成.及变量不需要前缀$,使用空格装配由点分隔的字符串." >如下所述同正规表达式,除了->被替换成.及变量不需要前缀$,使用空格装配由点分隔的字符串.
使用not:、exists:语法
exists:可以用于tal:condition中,判断存在" >exists:可以用于tal:condition中,判断存在
使用default、structure语法
default默认值;在 tal:define、tal:content、tal:attributes 使用" >default默认值;在 tal:define、tal:content、tal:attributes 使用
使用structure语法
允许将包括html/xml等变量输出显示;
在tal:content中使用" >在tal:content中使用
在${}中使用" >在${}中使用
Home Backend Development PHP Tutorial Parsing of TAL template engine syntax in PHP (code)

Parsing of TAL template engine syntax in PHP (code)

Aug 01, 2018 am 10:25 AM
php

这篇文章给大家介绍的内容是关于php TAL模板引擎语法,内容很详细,有需要的朋友可以参考一下,希望可以帮助到大家。

PHP 数据

本文档的使用到的数据($talData)

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

//定义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')

);

Copy after login

1

2

3

//输出的数据结构

{"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"}]}

Copy after login

属性优先权

  • define   定义变量

  • condition

  • repeat

  • content or replacae

  • attributes  属性

  • omit-tag

TAL有效空间

定义变量

定义全局(global)变量title和局部变量 key

1

2

3

4

5

6

7

<p tal:define="global title talData/title; key talData/key">

    ${title} && ${key}

</p>

${title} && ${key}

<!-- 结果 -->

<p>title1 && key1</p>

title1 &&

Copy after login
定义默认值变量

1

2

3

4

5

6

7

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

Copy after login

TAL有效空间

定义变量

定义拼接变量

1

2

3

4

5

6

7

8

9

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

Copy after login
定义变量,执行php方法

1

2

3

4

5

<p tal:define="global rand php: rand(1, 4)">

    ${rand}

</p>

<!-- 结果 -->

<p>2</p>

Copy after login

TAL有效空间

输出内容

tal:replace="数据" 将用值替代整个标签,如果没有赋值将替空

1

2

3

4

5

6

7

8

9

10

11

12

13

14

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

Copy after login
tal:content="数据" 将会替代标签内的内容

1

2

3

4

5

<p tal:content="talData/title">

    test data<span>tips</span>

</p>

<!-- 结果 -->

<p>title1</p>

Copy after login

TAL有效空间

输出内容

${数据}

1

2

3

<p class="p p_${talData/title}">${talData/title}</p>

<!-- 结果 -->

<p class="p p_title1">title1</p>

Copy after login
tal:attributes 将会改变tag的属性或值

1

2

3

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

Copy after login
tal:on-error="string: 出错时显示内容" ;出错时显示error内容,并且没有当前标签;

1

2

3

4

5

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

当前数据不存在

Copy after login

TAL有效空间

判断条件

tal:condition="数据",条件是true,标签和它的内容就会被显示

1

2

3

4

5

6

7

8

9

10

<p tal:condition="php: true" tal:content="talData/title">标题</p>

<p tal:condition="php: talData[&#39;key&#39;] == &#39;key1&#39;" tal:content="talData/key">关键词</p>

<p tal:condition="php: key == &#39;key1&#39;" 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>

Copy after login

TAL有效空间

循环

tal:repeat="赋值变量 数据";属性循环它的元素和内容直到结束

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

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

Copy after login

TAL有效空间

标签渲染与否

tal:omit-tag="condition";要求phptal解析时忽略实体的开关闭标签,它的内容仍然会显示.

1

2

3

4

5

6

7

8

9

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

Copy after login
;代替标签,标签永远不输出

1

2

3

<tal:block tal:repeat="x php:range(1,10)" tal:content="x">文字会出现十次</tal:block>

<!-- 结果 -->

12345678910

Copy after login

METAL空间 支持宏

metal:define-macro 定义宏

1

2

3

4

5

6

<!-- 在pWeb/_macro/common.html文件中代码 -->

<tal:block metal:define-macro="WinPageData">

    <script type="text/javascript">

        window.WinPageData = ${structure WinPageData};

    </script>

</tal:block>

Copy after login
metal:use-macro 调用宏

1

2

3

4

5

6

7

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

Copy after login

METAL空间 支持宏

metal:define-slot 定义宏标签替换

1

2

3

4

5

6

7

8

9

10

<!-- 在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=&#39;xxx.js&#39;></script>

</tal:block>

Copy after login
metal:fill-slot  使用宏标签替换

1

2

3

4

5

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

Copy after login

METAL空间 支持宏

宏获取参数

1

2

3

4

5

<!-- 在pWeb/_macro/mlog.html文件中代码 -->

<tal:block metal:define-macro="conent">

    <p>${title}</p>

    <p>${key}</p>

</tal:block>

Copy after login
宏传参数(tal:define)

1

<tal:block metal:use-macro="pWeb/_macro/mlog.html/conent"  tal:define="title &#39;标题&#39;; key &#39;关键词&#39;"></tal:block>

Copy after login

phptal空间

phptal:debug

phptal:cache 使整个元素(包括标签)保存在磁盘而不重新解析直到cache失效,有效期的格式由数字和'd', 'h','m'或's'组成.

1

2

3

<p class="footer" phptal:cache="3h">...</p>

有效期可以有选择的跟随'per'参数来定义多少个缓存被共享,使用'per url'分别针对每个url复制元素.

<ol id="breadcrumbs" phptal:cache="1d per url">...</ol>

Copy after login

使用php:语法

如下所述同正规表达式,除了->被替换成.及变量不需要前缀$,使用空格装配由点分隔的字符串.

1

2

3

4

5

6

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

Copy after login

使用not:、exists:语法

not:可以用于tal:condition中,相反判断

1

2

3

<span tal:condition="not: logged">not logged</span>

<!-- 结果 -->

<span>not logged</span>

Copy after login
exists:可以用于tal:condition中,判断存在

1

2

3

<span tal:condition="exists: talData/title" tal:content="talData/title">存在</span>

<!-- 结果 -->

<span>title1</span>

Copy after login

使用default、structure语法

default默认值;在 tal:define、tal:content、tal:attributes 使用

1

2

3

4

5

6

7

8

9

10

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

Copy after login

使用structure语法

允许将包括html/xml等变量输出显示;

注意存在XSS攻击风险,谨慎使用

在tal:content中使用

1

2

3

4

5

<p tal:content="talData/html"></p>

<p tal:content="structure talData/html"></p>

<!-- 结果 -->

<p>&lt;h1&gt;h1标题&lt;/h1&gt;</p>

<p><h1>h1标题</h1></p>

Copy after login
在${}中使用

1

2

3

4

5

<p>${talData/html}</p>

<p>${structure talData/html}</p>

<!-- 结果 -->

<p>&lt;h1&gt;h1标题&lt;/h1&gt;</p>

<p><h1>h1标题</h1></p>

Copy after login

相关文章推荐:

PHP的学习--PHP加密,PHP学习--PHP加密_PHP教程

The above is the detailed content of Parsing of TAL template engine syntax in PHP (code). For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

7 PHP Functions I Regret I Didn't Know Before 7 PHP Functions I Regret I Didn't Know Before Nov 13, 2024 am 09:42 AM

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

See all articles