smarty 是什么,就不多说了,用过php,接触过php的人都对smarty 再熟悉不过了。它是一个很强大的代码分离软件,作为PHP的第三方类库引用到PHP项目中,将PHP代码和HTML完美分开,加速程序员和前端人员协同开发,提高开发速度。
smarty 的目前最新版本是3版本。http://www.smarty.net/files/Smarty-3.1.14.zip
下载下来。解压,我们需要是里面的libs 文件夹的内容。复制这个文件夹,到我们的文件的php项目中。
我们前面说过,smarty是php的一个类库导入到项目中,所以第一步,我们就要引入这个类库。
require './libs/Smarty.class.php';
//smarty 3 $smarty = new Smarty; $smarty->setTemplateDir('./tpl/'); $smarty->setCompileDir('./comp/');
对比一下smarty3 和 2 ,我们发现,smarty 3 完全改成php面向对象的方式来处理,我们看一下smarty 2 中是如何定义模板和缓存目录的:
//smarty 2 $smarty = new Smarty(); //建立smarty实例对象$smarty $smarty->templates("./tpl/"); //设置模板目录 $smarty->templates_c("./comp/"); //设置编译目录
下面是完整的php代码
require './libs/Smarty.class.php'; $smarty = new Smarty; $smarty->setTemplateDir(ROOT.'tpl/'); $smarty->setCompileDir(ROOT.'comp/'); $title = '这是一个smarty3的标题'; $content = '欢迎使用smarty 3 模版引擎!'; $smarty->assign('title',$title); $smarty->assign('content',$content); $smarty->display('index.html');
html 代码:
<html> <head> <title><{$title}></title> </head> <body> <{$content}> </body> </html>
在第2点中的基本设置,smarty 已经可以正常使用,但是,我们在使用中会发现几个问题:
比如在index.html中有如下代码:
<html> <head> <title><{$title}></title> <style type="text/css"> body {width: 860px;margin: 0px;padding: 0px;} </style> <script type="text/javascript"> function addStips(){alert(123);} addStips(); </script> </head> <body> {$content} </body> </html>
我们运行代码的时候发现报错了:
Fatal error: Uncaught exception 'SmartyCompilerException' with message 'Syntax Error in template "D:\wamp\www\yangyi\2smarty3\tpl\index.html" on line 8 "body {width: 860px;margin: 0px;padding: 0px;}" - Unexpected ": ", expected one of: "}" , " " , ATTR' in D:\wamp\www\yangyi\2smarty3\libs\sysplugins\smarty_internal_templatecompilerbase.php on line 667
我们仔细发现,原来css 和js 的{ } 没有换行写,和smarty中的定界符{} 冲突了,然后当作变量解析了,所以出错。那如何修改这个问题呢。那就只有修改smarty的定界符了。
我们查看lib/Smarty.class.php 中我们发现定界符的定义
/** * template left-delimiter * @var string */ public $left_delimiter = "{"; /** * template right-delimiter * @var string */ public $right_delimiter = "}";
$smarty->left_delimiter = '<{'; $smarty->right_delimiter = '}>';
<html> <head> <title><{$title}></title> <style type="text/css"> body {width: 860px;margin: 0px;padding: 0px;} </style> <script type="text/javascript"> function addStips(){alert(123);} addStips(); </script> </head> <body> <{$content}> </body> </html>
我们在项目过程中,是一定会有多个php文件,也可能有前台和后台,那么我们就要在每个php文件开头都这样来一次:
require './libs/Smarty.class.php'; $smarty = new Smarty; $smarty->setTemplateDir(ROOT.'tpl/'); $smarty->setCompileDir(ROOT.'comp/'); $smarty->left_delimiter = '<{'; $smarty->right_delimiter = '}>';
//init.class.php require ROOT.'libs/Smarty.class.php'; $smarty = new Smarty; $smarty->setTemplateDir('tpl/'); $smarty->setCompileDir('comp/'); $smarty->left_delimiter = '<{'; $smarty->right_delimiter = '}>';
require './init.class.php'; $title = '这是一个smarty3的标题'; $content = '欢迎使用smarty 3 模版引擎!'; $smarty->assign('title',$title); $smarty->assign('content',$content); $smarty->display('index.html');
5. smarty 的优化-分级目录如何导入
上面写在一个配置文件中之后,我们使用起来就很方便了,但是现在又出现另外一个问题,就是多目录了,比如有一个admin/admin.php 中也需要smarty ,那么引入进来之后,却发现报错了,说找不到smarty.class.php
所以,我们必须在init.class.php中导入smarty时候目录用绝对路径就可以了:
$root = str_replace('\\', '/', __FILE__); define('ROOT', dirname($root).'/'); require ROOT.'libs/Smarty.class.php'; $smarty = new Smarty; $smarty->setTemplateDir(ROOT.'tpl/'); $smarty->setCompileDir(ROOT.'comp/'); $smarty->left_delimiter = '<{'; $smarty->right_delimiter = '}>';
我们经常发现一些人在用display的时候,总是写不对,总是相对于当前的php文件来设定目录:
php:admin/admin.php
html:tpl/user/admin.html
所以我们总是理所当然的认为是这样:
require '../init.class.php'; $smarty->assign('title',1111); $smarty->assign('content',2222); $smarty->display('../user/addUser.html'); //错的。
require '../init.class.php'; $smarty->assign('title',1111); $smarty->assign('content',2222); $smarty->display('user/addUser.html'); //正确
多看源代码,比如,我们忘记了如何设定模板和缓存模板,忘记了设定左定界符和右定界符符合写。那么就可以打来Smarty.class.php 搜一下,多看看这个文件。就好办了。