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 Email: Step-by-Step Sending GuidePHP Email: Step-by-Step Sending GuideMay 09, 2025 am 12:14 AM

    PHPisusedforsendingemailsduetoitsintegrationwithservermailservicesandexternalSMTPproviders,automatingnotificationsandmarketingcampaigns.1)SetupyourPHPenvironmentwithawebserverandPHP,ensuringthemailfunctionisenabled.2)UseabasicscriptwithPHP'smailfunct

    How to Send Email via PHP: Examples & CodeHow to Send Email via PHP: Examples & CodeMay 09, 2025 am 12:13 AM

    The best way to send emails is to use the PHPMailer library. 1) Using the mail() function is simple but unreliable, which may cause emails to enter spam or cannot be delivered. 2) PHPMailer provides better control and reliability, and supports HTML mail, attachments and SMTP authentication. 3) Make sure SMTP settings are configured correctly and encryption (such as STARTTLS or SSL/TLS) is used to enhance security. 4) For large amounts of emails, consider using a mail queue system to optimize performance.

    Advanced PHP Email: Custom Headers & FeaturesAdvanced PHP Email: Custom Headers & FeaturesMay 09, 2025 am 12:13 AM

    CustomheadersandadvancedfeaturesinPHPemailenhancefunctionalityandreliability.1)Customheadersaddmetadatafortrackingandcategorization.2)HTMLemailsallowformattingandinteractivity.3)AttachmentscanbesentusinglibrarieslikePHPMailer.4)SMTPauthenticationimpr

    Guide to Sending Emails with PHP & SMTPGuide to Sending Emails with PHP & SMTPMay 09, 2025 am 12:06 AM

    Sending mail using PHP and SMTP can be achieved through the PHPMailer library. 1) Install and configure PHPMailer, 2) Set SMTP server details, 3) Define the email content, 4) Send emails and handle errors. Use this method to ensure the reliability and security of emails.

    What is the best way to send an email using PHP?What is the best way to send an email using PHP?May 08, 2025 am 12:21 AM

    ThebestapproachforsendingemailsinPHPisusingthePHPMailerlibraryduetoitsreliability,featurerichness,andeaseofuse.PHPMailersupportsSMTP,providesdetailederrorhandling,allowssendingHTMLandplaintextemails,supportsattachments,andenhancessecurity.Foroptimalu

    Best Practices for Dependency Injection in PHPBest Practices for Dependency Injection in PHPMay 08, 2025 am 12:21 AM

    The reason for using Dependency Injection (DI) is that it promotes loose coupling, testability, and maintainability of the code. 1) Use constructor to inject dependencies, 2) Avoid using service locators, 3) Use dependency injection containers to manage dependencies, 4) Improve testability through injecting dependencies, 5) Avoid over-injection dependencies, 6) Consider the impact of DI on performance.

    PHP performance tuning tips and tricksPHP performance tuning tips and tricksMay 08, 2025 am 12:20 AM

    PHPperformancetuningiscrucialbecauseitenhancesspeedandefficiency,whicharevitalforwebapplications.1)CachingwithAPCureducesdatabaseloadandimprovesresponsetimes.2)Optimizingdatabasequeriesbyselectingnecessarycolumnsandusingindexingspeedsupdataretrieval.

    PHP Email Security: Best Practices for Sending EmailsPHP Email Security: Best Practices for Sending EmailsMay 08, 2025 am 12:16 AM

    ThebestpracticesforsendingemailssecurelyinPHPinclude:1)UsingsecureconfigurationswithSMTPandSTARTTLSencryption,2)Validatingandsanitizinginputstopreventinjectionattacks,3)EncryptingsensitivedatawithinemailsusingOpenSSL,4)Properlyhandlingemailheaderstoa

    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

    Video Face Swap

    Video Face Swap

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

    Hot Tools

    SecLists

    SecLists

    SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

    DVWA

    DVWA

    Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

    SublimeText3 Mac version

    SublimeText3 Mac version

    God-level code editing software (SublimeText3)

    SublimeText3 English version

    SublimeText3 English version

    Recommended: Win version, supports code prompts!

    SublimeText3 Linux new version

    SublimeText3 Linux new version

    SublimeText3 Linux latest version