昨晚回来后,就去看了一下Smarty模板引擎的使用,感受其实对于新手来讲应该仍是很好用的。也很容易懂。由于以前学过ThinkPHP,因此,理解起来也不难,应用起来也挺好的,不过,这种东西,也仍是要多在项目中使用才会越顺手。php
一、开始使用Smartycss
- /*
- *index.php
- *author:qtvb-star
- */
- require "Smarty.class.php"; //加载Smarty引擎,这是核心文件
- $smarty = new Smarty();//实例化
- $smarty -> template_dir = './templates/';//模板目录
- $smarty -> compile_dir = './templates_c/';//编译目录
- $smarty -> config_dir = './configs/';//配置目录,刚开始用可能不会用,后期可能会用上
- $smarty -> cache_dir = './cache/';//缓存目录
- $smarty -> assign('name', 'my name');//给变量赋值
- $smarty -> display('index.tpl');//显示模板,也能够是index.html这种文件
而后这就是最简单的使用了,关于index.tpl或者index.html怎么写,以下html
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
- <head>
- <title>{$name}</title>
- <style type="text/css">
- {literal}
- {/literal}
- </style>
- </head>
- <body>
- {$dc}
- <h1>{$name}会显示在这里</h1>
- </body>
- </html>
默认的单词分割符是“{”与“}”,这个是能够本身修改,具体不介绍。缓存
其实以前我也不懂这个原理的,后来,本身百度了下,如何本身写模板引擎,而后明白了,原理差很少都是同样的,就是用正则匹配{}这个定义的分割符,而后,替换成<?php echo $name ?>这种形式,固然,其它循环语句也相似。而后把替换后的内容写进一个php文件,而后在index.php中include那个php文件就能够了。你们能够本身尝试写一下,会加深理解的。ide