search
HomeBackend DevelopmentPHP TutorialParsing of TAL template engine syntax in PHP (code)

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

PHP 数据

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

//定义talData变量
$talData = array();

$talData['title'] = 'title1';
$talData['key'] = 'key1';
$talData['href'] = 'xxx.html';
$talData['html'] = '<h1 id="h-标题">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","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

TAL有效空间

定义变量

定义全局(global)变量title和局部变量 key
<p>
    ${title} && ${key}
</p>
${title} && ${key}
<!-- 结果 -->
<p>title1 && key1</p>
title1 &&
定义默认值变量
<p>
    ${fname} && ${lname}
</p>
${fname} && ${lname}
<!-- 结果 -->
<p>fname1 string && lname1 string</p>
fname1 string &&

TAL有效空间

定义变量

定义拼接变量
<p>
    ${hello}
</p>
或
<p>
    ${hello}
</p>
<!-- 结果 -->
<p>hello fname1 string welcome on this page</p>
定义变量,执行php方法
<p>
    ${rand}
</p>
<!-- 结果 -->
<p>2</p>

TAL有效空间

输出内容

tal:replace="数据" 将用值替代整个标签,如果没有赋值将替空
<block>
    <p>渲染1</p>
</block>
<block>
不渲染这里
</block>
<block>
    <p>不渲染这里</p>
</block>
<p>${talData/title}</p>
<!-- 结果 -->
<p>渲染1</p>
title1
<p>title1</p>
tal:content="数据" 将会替代标签内的内容
<p>
    test data<span>tips</span>
</p>
<!-- 结果 -->
<p>title1</p>

TAL有效空间

输出内容

${数据}
<p>${talData/title}</p>
<!-- 结果 -->
<p>title1</p>
tal:attributes 将会改变tag的属性或值
<a>sample link</a>
<!-- 结果 -->
<a>key1</a>
tal:on-error="string: 出错时显示内容" ;出错时显示error内容,并且没有当前标签;
<span>key 已经是定义了</span>
<span>ky 没有定义的</span>
<!-- 结果 -->
<span>key1</span>
当前数据不存在

TAL有效空间

判断条件

tal:condition="数据",条件是true,标签和它的内容就会被显示
<p>标题</p>
<p>关键词</p>
<p>关键词</p>
<p>关键词</p>
<p>关键词</p>
<!-- 结果 -->
<p>title1</p>
<p>key1</p>
<p>key1</p>
<p>key1</p>

TAL有效空间

循环

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

        
  •         repeat/item/key: ${repeat/item/key} ;(如果talData/list1是一组联合对象,返回item的键)
            repeat/item/index: ${repeat/item/index} ;(返回索引号(0~count-1))
            repeat/item/number: ${repeat/item/number} ;(返回索引号(1~count))
            repeat/item/even: ${repeat/item/even} ;(如果是偶数,true)
            repeat/item/odd: ${repeat/item/odd} ;(如果是奇数,true)
            repeat/item/start: ${repeat/item/start} ;(如果是第一个元素,true)
            repeat/item/end: ${repeat/item/end} ;(如果是最后一个元素,true)
            repeat/item/length: ${repeat/item/length} ;(返回talData/list1里面元素个数)
            -------------     
  •     
  •  0" tal:content="item/title">列表li
  • repeat/item/key: 0 ;(如果talData/list1是一组联合对象,返回item的键)
    repeat/item/index: 0 ;(返回索引号(0~count-1))
    repeat/item/number: 1 ;(返回索引号(1~count))
    repeat/item/even: 1 ;(如果是偶数,true)
    repeat/item/odd: 0 ;(如果是奇数,true)
    repeat/item/start: 1 ;(如果是第一个元素,true)
    repeat/item/end: 0 ;(如果是最后一个元素,true)
    repeat/item/length: 4 ;(返回talData/list1里面元素个数)
  • 标题2
  • 标题3
  • 标题4
  • TAL有效空间

    标签渲染与否

    tal:omit-tag="condition";要求phptal解析时忽略实体的开关闭标签,它的内容仍然会显示.
    <p>
        omit-tag值为真,不出现当前p标签,否则就出现当前p标签
    </p>
    <p>
        omit-tag值为真,不出现当前p标签,否则就出现当前p标签
    </p>
    <!-- 结果 -->
    <p>omit-tag值为真,不出现当前p标签,否则就出现当前p标签</p>
    omit-tag值为真,不出现当前p标签,否则就出现当前p标签
    ;代替标签,标签永远不输出
    <block>文字会出现十次</block>
    <!-- 结果 -->
    12345678910

    METAL空间 支持宏

    metal:define-macro 定义宏
    <!-- 在pWeb/_macro/common.html文件中代码 -->
    <block>
        <script>
            window.WinPageData = ${structure WinPageData};
        </script>
    </block>
    metal:use-macro 调用宏
    <block>
        当前内容会被宏替换掉
        <script>
            window.WinPageData = {};
        </script>
    </block>
    <p></p>

    METAL空间 支持宏

    metal:define-slot 定义宏标签替换
    <!-- 在pWeb/_macro/mlog.html文件中代码 -->
    <block>
        <script>
            <metal:block define-slot="mlog_page">
            var mlog_page = &#39;mobile_other&#39;;
            
            var mlog_track = function(){};
        </script>
        <script></script>
    </block>
    metal:fill-slot  使用宏标签替换
    <block>
        <block>
            var mlog_page = 'mobile_index';
        </block>
    </block>

    METAL空间 支持宏

    宏获取参数
    <!-- 在pWeb/_macro/mlog.html文件中代码 -->
    <block>
        <p>${title}</p>
        <p>${key}</p>
    </block>
    宏传参数(tal:define)
    <block></block>

    phptal空间

    phptal:debug

    phptal:cache 使整个元素(包括标签)保存在磁盘而不重新解析直到cache失效,有效期的格式由数字和'd', 'h','m'或's'组成.
    <p>...</p>
    有效期可以有选择的跟随'per'参数来定义多少个缓存被共享,使用'per url'分别针对每个url复制元素.
    <ol>...</ol>

    使用php:语法

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

    使用not:、exists:语法

    not:可以用于tal:condition中,相反判断
    <span>not logged</span>
    <!-- 结果 -->
    <span>not logged</span>
    exists:可以用于tal:condition中,判断存在
    <span>存在</span>
    <!-- 结果 -->
    <span>title1</span>

    使用default、structure语法

    default默认值;在 tal:define、tal:content、tal:attributes 使用
    <span>
    default my var value
    </span>
    <p>
    没有var,没有找到path
    </p>
    <a>Unknown page</a>
    <!-- 结果 -->
    <p>没有var,没有找到path</p>
    <a>Unknown page</a>

    使用structure语法

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

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

    在tal:content中使用
    <p></p>
    <p></p>
    <!-- 结果 -->
    <p><h1>h1标题</h1></p>
    <p></p><h1 id="h-标题">h1标题</h1>
    在${}中使用
    <p>${talData/html}</p>
    <p>${structure talData/html}</p>
    <!-- 结果 -->
    <p><h1>h1标题</h1></p>
    <p></p><h1 id="h-标题">h1标题</h1>

    相关文章推荐:

    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
    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
    PHP and Python: Different Paradigms ExplainedPHP and Python: Different Paradigms ExplainedApr 18, 2025 am 12:26 AM

    PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

    PHP and Python: A Deep Dive into Their HistoryPHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AM

    PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

    Choosing Between PHP and Python: A GuideChoosing Between PHP and Python: A GuideApr 18, 2025 am 12:24 AM

    PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

    PHP and Frameworks: Modernizing the LanguagePHP and Frameworks: Modernizing the LanguageApr 18, 2025 am 12:14 AM

    PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

    PHP's Impact: Web Development and BeyondPHP's Impact: Web Development and BeyondApr 18, 2025 am 12:10 AM

    PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

    How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

    PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

    How does PHP handle object cloning (clone keyword) and the __clone magic method?How does PHP handle object cloning (clone keyword) and the __clone magic method?Apr 17, 2025 am 12:24 AM

    In PHP, use the clone keyword to create a copy of the object and customize the cloning behavior through the \_\_clone magic method. 1. Use the clone keyword to make a shallow copy, cloning the object's properties but not the object's properties. 2. The \_\_clone method can deeply copy nested objects to avoid shallow copying problems. 3. Pay attention to avoid circular references and performance problems in cloning, and optimize cloning operations to improve efficiency.

    PHP vs. Python: Use Cases and ApplicationsPHP vs. Python: Use Cases and ApplicationsApr 17, 2025 am 12:23 AM

    PHP is suitable for web development and content management systems, and Python is suitable for data science, machine learning and automation scripts. 1.PHP performs well in building fast and scalable websites and applications and is commonly used in CMS such as WordPress. 2. Python has performed outstandingly in the fields of data science and machine learning, with rich libraries such as NumPy and TensorFlow.

    See all articles

    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

    AI Hentai Generator

    AI Hentai Generator

    Generate AI Hentai for free.

    Hot Article

    R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
    1 months agoBy尊渡假赌尊渡假赌尊渡假赌
    R.E.P.O. Best Graphic Settings
    1 months agoBy尊渡假赌尊渡假赌尊渡假赌
    Will R.E.P.O. Have Crossplay?
    1 months agoBy尊渡假赌尊渡假赌尊渡假赌

    Hot Tools

    VSCode Windows 64-bit Download

    VSCode Windows 64-bit Download

    A free and powerful IDE editor launched by Microsoft

    MantisBT

    MantisBT

    Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

    ZendStudio 13.5.1 Mac

    ZendStudio 13.5.1 Mac

    Powerful PHP integrated development environment

    Dreamweaver Mac version

    Dreamweaver Mac version

    Visual web development tools

    MinGW - Minimalist GNU for Windows

    MinGW - Minimalist GNU for Windows

    This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.