TMDPHP 模板引擎使用教程
Jun 13, 2016 pm 12:01 PM
在PHP界談模板引擎,必不可免的要拿Smarty開刀,
這個無比傻帽的卻又帶有一點點官方色彩的模板引擎,
如果沒有我這樣人富有正義感又富有創(chuàng)新精神的熱血青年站出來,
不知道它還要繼續(xù)毒害多少那些處于花季而又對PHP充滿美麗幻想的少年。
1.語法
你真的認為美工學的了 {foreach key=key item=item from=$contact} 這樣的語法
卻學不了 $item) { ?> 嗎?
而 {if $name eq "Fred" or $name eq "Wilma"}
又比 優(yōu)秀到哪里去?
首先我對美工會學Smarty語法始終保持懷疑態(tài)度,至少我工作這么多年還沒遇到過一個會Smarty語法的,
而就算美工愿意學,你又為何不教他正宗的PHP語法,卻要教他一門連你自己搞不清楚的“Smarty語言”
2.可視化
當頁面從美工交接到你手上,然后你給那些完美的網(wǎng)頁,套上那惡心的Smarty代碼,
然后在Dreamweaver里,你是否認真的看過那些頁面已經(jīng)變得何等的丑陋,
圖片還看得見嗎?CSS還在嗎?更不用說include了。而要修改的時候呢?你還能一眼認出來嗎?
這些都解決不了,那些所謂的模板引擎又怎么配得上“強大”二字?
3....
多的我就不說了,這里只是拿Smarty舉個例子,應(yīng)該不難發(fā)現(xiàn),其它模板引擎也大同小異,
都忙著發(fā)明自己的模板語言,而真正需要解決的問題則避而不談,
現(xiàn)在你是否明白了,所謂的模板引擎,所謂的強大,都TMD騙子,
在夜深人靜的夜里,我曾無數(shù)次的醒來,感覺自己的擔子又重了一些,只因為不能夠?qū)⑦@個殘忍的事實告訴你。
于是我痛心疾首,痛下狠心,百忙之中抽空寫了這個命名為tmd_tpl的真正的模板引擎,
雖然也許現(xiàn)在還不算強大,但強大是未來的一種必然。
·tmd_tpl使用入門教程:
接下來我們來學習tmd_tpl的使用方法,流程上和其它模板引擎沒什么太大的差別。
一、初始化模板引擎
復制代碼 代碼如下:
// 將 tmd_tpl 包含進來
require '../tmdphp/tmd_tpl.php'; // 請改為你的tmd_tpl所在路徑
// 實例化 tmd_tpl
$TPL = new tmd_tpl();
// 下面是配置 tmd_tpl
// 指定模板目錄,以斜杠結(jié)尾
$TPL->tpl_dir = './tpl/';
// 指定模板文件擴展名
// 建議以php為后綴,因為這樣在Dreamweaver中php代碼有高亮效果。
// 另外在Chrome瀏覽器中可以直接打開進行預覽,用IE打開會提示下載。
$TPL->tpl_ext = '.tpl.php';
// 指定編譯后模板的保存目錄
$TPL->cache_dir = './tpl_c/';
// 設(shè)置編譯后文件的有效期,單位:秒
$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的作者。');
// 支持數(shù)組
$blog = array(
'title' => '去TMD的Smarty',
'content' => '在講解tmd_tpl的使用方法之前,我要先講講為什么要重新發(fā)明這個輪子。
那我們要從這世界上所謂的PHP模板引擎都為大家做了哪些貢獻說起。
在PHP界談模板引擎,必不可免的要拿Smarty開刀,
這個無比傻帽的卻又帶有一點點官方色彩的模板引擎,
如果沒有我這樣人富有正義感又富有創(chuàng)新精神的熱血青年站出來,
不知道它還要繼續(xù)毒害多少那些處于花季而又對PHP充滿美麗幻想的少年。',
// 目前只支持到二維數(shù)組,一般來說二維已經(jīng)足夠了
'info' => array(
'addtime' => '2012.3.11',
'author' => '王忠強',
'weibo' => 'http://t.qq.com/teeband',
),
);
$TPL->assign('blog', $blog);
// 模板中將演示循環(huán)輸出這個數(shù)組
$links = array(
'腳本之家' => 'http://www.jb51.net',
'素材天下' => 'http://sc.jb51.net/',
'百度' => 'http://www.baidu.com/',
'網(wǎng)址導航' => 'http://www.hao123.com',
'傷不起' => 'http://www.3buqi.com/',
'嘿!' => 'http://www.hei123.net/',
);
$TPL->assign('links', $links);
$TPL->display('index');
三、模板靜態(tài)文件的目錄結(jié)構(gòu)
四、模板篇
復制代碼 代碼如下:
// 模板中調(diào)用變量
// 調(diào)用數(shù)組
{$blog.title}
// 二維數(shù)組
{$blog.info.author}
// 這些模板引擎的基本功能,在tmd_tpl中也是一樣的。
// 但是循環(huán)和判斷,tmd_tpl觀點是直接用php代碼。沒必要創(chuàng)造個模板語言來實現(xiàn)。
foreach ($links as $name => $url) {
?>
}
?>
// 函數(shù)方面,對目前的tmd_tpl來說,是一個弱項。
// 還不支持 {$blog.content|nl2br} 這樣子的格式。
{:nl2br( $blog['content'] )} // 只能這樣
=nl2br( $blog['content'] )?> // 或者這樣
// 都將自動轉(zhuǎn)換為
// 如果調(diào)用的是一些無返回值的函數(shù)
{~print_r( $blog )}
tmd_tpl真正的創(chuàng)新之處在于,路徑的轉(zhuǎn)換。
你可以在Dreamweaver中直接插入圖片,引入CSS,調(diào)用JS,還有包含另一個頁面。
復制代碼 代碼如下:
// 插入圖片
// 引入CSS
// 調(diào)用JS
// 包含一個頁面
·使用tmd_tpl模板引擎之后
查看通過http訪問效果 >點這里
在Dreamweaver中(這樣子前端人員才有辦法嘛~)
?
在Chrome中(只有include的頁面不能顯示)
?
·不用tmd_tpl的話,我們來看看Discuz!和DedeCMS的模板。
Discuz!
?
DedeCms
?
現(xiàn)在你知道那些所謂強大的模板引擎有多可笑了吧?
還等什么?改變歷史的時刻到了,點擊你手中的鼠標,下載第二代PHP模板引擎tmd_tpl!

Hot AI Tools

Undress AI Tool
Undress images for free

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

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

Clothoff.io
AI clothes remover

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

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

The method to get the current session ID in PHP is to use the session_id() function, but you must call session_start() to successfully obtain it. 1. Call session_start() to start the session; 2. Use session_id() to read the session ID and output a string similar to abc123def456ghi789; 3. If the return is empty, check whether session_start() is missing, whether the user accesses for the first time, or whether the session is destroyed; 4. The session ID can be used for logging, security verification and cross-request communication, but security needs to be paid attention to. Make sure that the session is correctly enabled and the ID can be obtained successfully.

To extract substrings from PHP strings, you can use the substr() function, which is syntax substr(string$string,int$start,?int$length=null), and if the length is not specified, it will be intercepted to the end; when processing multi-byte characters such as Chinese, you should use the mb_substr() function to avoid garbled code; if you need to intercept the string according to a specific separator, you can use exploit() or combine strpos() and substr() to implement it, such as extracting file name extensions or domain names.

UnittestinginPHPinvolvesverifyingindividualcodeunitslikefunctionsormethodstocatchbugsearlyandensurereliablerefactoring.1)SetupPHPUnitviaComposer,createatestdirectory,andconfigureautoloadandphpunit.xml.2)Writetestcasesfollowingthearrange-act-assertpat

In PHP, the most common method is to split the string into an array using the exploit() function. This function divides the string into multiple parts through the specified delimiter and returns an array. The syntax is exploit(separator, string, limit), where separator is the separator, string is the original string, and limit is an optional parameter to control the maximum number of segments. For example $str="apple,banana,orange";$arr=explode(",",$str); The result is ["apple","bana

JavaScript data types are divided into primitive types and reference types. Primitive types include string, number, boolean, null, undefined, and symbol. The values are immutable and copies are copied when assigning values, so they do not affect each other; reference types such as objects, arrays and functions store memory addresses, and variables pointing to the same object will affect each other. Typeof and instanceof can be used to determine types, but pay attention to the historical issues of typeofnull. Understanding these two types of differences can help write more stable and reliable code.

std::chrono is used in C to process time, including obtaining the current time, measuring execution time, operation time point and duration, and formatting analysis time. 1. Use std::chrono::system_clock::now() to obtain the current time, which can be converted into a readable string, but the system clock may not be monotonous; 2. Use std::chrono::steady_clock to measure the execution time to ensure monotony, and convert it into milliseconds, seconds and other units through duration_cast; 3. Time point (time_point) and duration (duration) can be interoperable, but attention should be paid to unit compatibility and clock epoch (epoch)

ToaccessenvironmentvariablesinPHP,usegetenv()orthe$_ENVsuperglobal.1.getenv('VAR_NAME')retrievesaspecificvariable.2.$_ENV['VAR_NAME']accessesvariablesifvariables_orderinphp.iniincludes"E".SetvariablesviaCLIwithVAR=valuephpscript.php,inApach

In PHP, to pass a session variable to another page, the key is to start the session correctly and use the same $_SESSION key name. 1. Before using session variables for each page, it must be called session_start() and placed in the front of the script; 2. Set session variables such as $_SESSION['username']='JohnDoe' on the first page; 3. After calling session_start() on another page, access the variables through the same key name; 4. Make sure that session_start() is called on each page, avoid outputting content in advance, and check that the session storage path on the server is writable; 5. Use ses
