TMDPHP 模板引擎使用教程_php模板_PHP
在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.bitsCN.com',
'素材天下' => 'http://sc.bitsCN.com/',
'百度' => '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) {
?>
}
?>
// 函数方面,对目前的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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



In recent years, the template engine in PHP programming has become an important part of PHP development, making it easier for programmers to develop and manage pages. This article will introduce common template engines in PHP programming. SmartySmarty is a commonly used PHP template engine. It supports a series of functions such as cached templates, plug-in modules and custom functions. Smarty's syntax is very flexible and can solve the problem of combining PHP variables with HTML tags, making the PHP language more suitable for templated design. Moreover, S

ThinkPHP6 template engine usage guide: Create an exquisite front-end interface Introduction: With the development of web applications, the design and development of front-end interfaces have become increasingly important. As a developer, we need to use a powerful template engine to help us create and manage front-end interfaces. ThinkPHP6's template engine is a powerful tool to meet this need. This article will introduce how to use the ThinkPHP6 template engine to create a beautiful front-end interface. Part 1: Install ThinkPHP6 template engine

Fat-Free Framework is a lightweight PHP framework designed to provide simple and flexible tools for building web applications. It contains many useful features such as routing, database access, caching, etc. In the Fat-Free framework, using the Blade template engine can help us manage and render templates more conveniently. Blade is the template engine in the Laravel framework, which provides powerful syntax and template inheritance capabilities. In this article I will demonstrate how to use Bl in Fat-Free framework

Golang Template Engine Getting Started Guide: How to use templates in Golang, specific code examples are required Introduction: A template engine is a tool that can combine data and templates and generate documents in HTML, text or other formats. In Golang, we can use the built-in template package (html/template) to implement the functions of the template engine. This article will introduce in detail how to use the template engine in Golang and provide specific code examples. 1. The basic concept of template engine is to understand how to use it.

With the development of Internet technology, the demand for web applications is also increasing. Web developers often use template engines to generate dynamic web pages. This article will explore a new template engine: the Go language template engine. What is the Go language template engine? Go language is an advanced programming language developed by Google. Its syntax is concise and clear, making it easy to learn and use. The Go language template engine is a template system used to generate HTML templates in the Go language. The Go language template engine is called the "standard library".

PHP is a language widely used in web development. Whether you are developing a small website or a large system, PHP is very popular and convenient. In the PHP development process, we need to separate the logic and data layers, which requires the use of a template engine. The template engine can be simply understood as merging data and template files to generate the final HTML file. In this article, we will introduce some of the best template engines available in PHP. SmartySmarty is one of the most popular template engines in PHP,

Sharing experience on template engine selection and use in JavaScript development Introduction: In modern front-end development, the template engine (TemplateEngine) plays a crucial role. They enable developers to organize and manage large amounts of dynamic data more efficiently and effectively separate data from interface presentation. At the same time, choosing an appropriate template engine can also bring developers a better development experience and performance optimization. However, among the many JavaScript template engines, which one should you choose? catch

Nowadays, website development is inseparable from an important component-template engine. A template engine refers to a tool that combines page templates and data to generate HTML code with a specific format. In various website development frameworks, the template engine is an essential component, because the template engine can greatly reduce the duplication of code and improve the dynamics of the page. One of the most common and popular template engines is Smarty. Smarty is a DSL (DomainSpecif
