Table of Contents
{$site_name}" >{$site_name}
{$blog.title}" >{$blog.title}
·使用tmd_tpl模板引擎之后
·不用tmd_tpl的话,我们来看看Discuz!和DedeCMS的模板。
Home php教程 php手册 TMDPHP 模板引擎使用教程

TMDPHP 模板引擎使用教程

Jun 13, 2016 pm 12:01 PM
php smarty use exist engine take Tutorial template of want this

在PHP界谈模板引擎,必不可免的要拿Smarty开刀,
这个无比傻帽的却又带有一点点官方色彩的模板引擎,
如果没有我这样人富有正义感又富有创新精神的热血青年站出来,
不知道它还要继续毒害多少那些处于花季而又对PHP充满美丽幻想的少年。
1.语法
你真的认为美工学的了 {foreach key=key item=item from=$contact} 这样的语法
却学不了 $item) { ?> 吗?
而 {if $name eq "Fred" or $name eq "Wilma"}
又比 优秀到哪里去?
首先我对美工会学Smarty语法始终保持怀疑态度,至少我工作这么多年还没遇到过一个会Smarty语法的,
而就算美工愿意学,你又为何不教他正宗的PHP语法,却要教他一门连你自己搞不清楚的“Smarty语言”
2.可视化
当页面从美工交接到你手上,然后你给那些完美的网页,套上那恶心的Smarty代码,
然后在Dreamweaver里,你是否认真的看过那些页面已经变得何等的丑陋,
图片还看得见吗?CSS还在吗?更不用说include了。而要修改的时候呢?你还能一眼认出来吗?
这些都解决不了,那些所谓的模板引擎又怎么配得上“强大”二字?
3....
多的我就不说了,这里只是拿Smarty举个例子,应该不难发现,其它模板引擎也大同小异,
都忙着发明自己的模板语言,而真正需要解决的问题则避而不谈,
现在你是否明白了,所谓的模板引擎,所谓的强大,都TMD骗子,
在夜深人静的夜里,我曾无数次的醒来,感觉自己的担子又重了一些,只因为不能够将这个残忍的事实告诉你。
于是我痛心疾首,痛下狠心,百忙之中抽空写了这个命名为tmd_tpl的真正的模板引擎,
虽然也许现在还不算强大,但强大是未来的一种必然。
·tmd_tpl使用入门教程:
接下来我们来学习tmd_tpl的使用方法,流程上和其它模板引擎没什么太大的差别。
一、初始化模板引擎

复制代码 代码如下:


// 将 tmd_tpl 包含进来
require '../tmdphp/tmd_tpl.php'; // 请改为你的tmd_tpl所在路径
// 实例化 tmd_tpl
$TPL = new tmd_tpl();
// 下面是配置 tmd_tpl
// 指定模板目录,以斜杠结尾
$TPL->tpl_dir = './tpl/';
// 指定模板文件扩展名
// 建议以php为后缀,因为这样在Dreamweaver中php代码有高亮效果。
// 另外在Chrome浏览器中可以直接打开进行预览,用IE打开会提示下载。
$TPL->tpl_ext = '.tpl.php';
// 指定编译后模板的保存目录
$TPL->cache_dir = './tpl_c/';
// 设置编译后文件的有效期,单位:秒
$TPL->cache_time = 0; // 0是每次都重新编译,-1是永不过期,
// 自定义正则替换
$TPL->my_rep = array(
'~(\.\./)+static/~' => '/proj-1/static/',
// ↑如果项目的访问地址是 http://localhost/proj-1/
// 自定义替换这里有很多技巧,入门期先不写出来
);


二、赋值并显示页面

复制代码 代码如下:


// 普通赋值
$TPL->assign('site_name', '王道中强流');
$TPL->assign('site_intro', '我是一个PHP程序员,tmd_tpl的作者。');
// 支持数组
$blog = array(
'title' => '去TMD的Smarty',
'content' => '在讲解tmd_tpl的使用方法之前,我要先讲讲为什么要重新发明这个轮子。
那我们要从这世界上所谓的PHP模板引擎都为大家做了哪些贡献说起。
在PHP界谈模板引擎,必不可免的要拿Smarty开刀,
这个无比傻帽的却又带有一点点官方色彩的模板引擎,
如果没有我这样人富有正义感又富有创新精神的热血青年站出来,
不知道它还要继续毒害多少那些处于花季而又对PHP充满美丽幻想的少年。',
// 目前只支持到二维数组,一般来说二维已经足够了
'info' => array(
'addtime' => '2012.3.11',
'author' => '王忠强',
'weibo' => 'http://t.qq.com/teeband',
),
);
$TPL->assign('blog', $blog);
// 模板中将演示循环输出这个数组
$links = array(
'脚本之家' => 'http://www.jb51.net',
'素材天下' => 'http://sc.jb51.net/',
'百度' => 'http://www.baidu.com/',
'网址导航' => 'http://www.hao123.com',
'伤不起' => 'http://www.3buqi.com/',
'嘿!' => 'http://www.hei123.net/',
);
$TPL->assign('links', $links);
$TPL->display('index');


三、模板静态文件的目录结构


四、模板篇

复制代码 代码如下:


// 模板中调用变量


// 调用数组

{$blog.title}


// 二维数组
{$blog.info.author}
// 这些模板引擎的基本功能,在tmd_tpl中也是一样的。
// 但是循环和判断,tmd_tpl观点是直接用php代码。没必要创造个模板语言来实现。
foreach ($links as $name => $url) {
?>
  • {$name}

  • }
    ?>
    // 函数方面,对目前的tmd_tpl来说,是一个弱项。
    // 还不支持 {$blog.content|nl2br} 这样子的格式。
    {:nl2br( $blog['content'] )} // 只能这样
    =nl2br( $blog['content'] )?> // 或者这样
    // 都将自动转换为

    // 如果调用的是一些无返回值的函数
    {~print_r( $blog )}

    tmd_tpl真正的创新之处在于,路径的转换。
    你可以在Dreamweaver中直接插入图片,引入CSS,调用JS,还有包含另一个页面。

    复制代码 代码如下:


    // 插入图片

    // 引入CSS

    // 调用JS

    // 包含一个页面


    ·使用tmd_tpl模板引擎之后

    查看通过http访问效果 >点这里

    在Dreamweaver中(这样子前端人员才有办法嘛~)

     

    在Chrome中(只有include的页面不能显示)

     

    ·不用tmd_tpl的话,我们来看看Discuz!和DedeCMS的模板。

    Discuz!

     

    DedeCms

     

    现在你知道那些所谓强大的模板引擎有多可笑了吧?

    还等什么?改变历史的时刻到了,点击你手中的鼠标,下载第二代PHP模板引擎tmd_tpl!

    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)

    Hot Topics

    Java Tutorial
    1660
    14
    PHP Tutorial
    1261
    29
    C# Tutorial
    1234
    24
    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

    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,

    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.

    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

    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.

    PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

    PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

    PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

    PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

    PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

    PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

    See all articles