smarty课程---smarty3的安装和使用

smarty课程---smarty3的安装和使用

一、总结

一句话总结:smarty 是什么,就不多说了,用过php,接触过php的人都对smarty 再熟悉不过了。它是一个很强大的代码分离软件,作为PHP的第三方类库引用到PHP项目中,将PHP代码和HTML完美分开,加速程序员和前端人员协同开发,提高开发速度。

代码分离 php html 前端 后端

多看源代码

1、smarty3如何安装?

类库 引入 new

我们前面说过,smarty是php的一个类库导入到项目中,所以第一步,我们就要引入这个类库。

require './libs/Smarty.class.php';


引入进来之后,那么就要new 一个smarty实例了。

//smarty 3
$smarty = new Smarty;
$smarty->setTemplateDir('./tpl/');
$smarty->setCompileDir('./comp/');

2、thinkphp采用smarty作为模板引擎,给我们什么启示?

原生 使用

smarty里面的一些原生的操作方法,比如变量等等,都是可以在thinkphp中使用的,可以试试

不过一般用不着,现在提供的方法和功能已经可以满足所有了

3、如果css 和js 的{ } 没有换行写,和smarty中的定界符{} 冲突怎么办?

修改 定界符

修改smarty中的定界符

 1 <html>
 2     
 3     <head>
 4 
 5         <title><{$title}></title>
 6 
 7         <style type="text/css">
 8             body { 860px;margin: 0px;padding: 0px;}
 9         </style>
10 
11         <script type="text/javascript">
12             function addStips(){alert(123);}
13             addStips();
14         </script>
15         
16     </head>
17     <body>
18             {$content} 
19       </body>    
20         
21 </html>

我们查看lib/Smarty.class.php 中我们发现定界符的定义

    /**
     * template left-delimiter
     * @var string
     */
    public $left_delimiter = "{";
    /**
     * template right-delimiter
     * @var string
     */
    public $right_delimiter = "}";


默认的是{ } 极容易和css 还有js 的大括号冲突,所以我们要修改一下。

$smarty->left_delimiter  = '<{';
$smarty->right_delimiter = '}>';

4、控制器分配模板的时候是使用绝对路径还是相对路径?

相对路径

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'); //错的。


其实,是错的,我们只需要记住:这个display永远是相对于init.class.php中设定的tpl的路径,永远是和tpl/目录的。所以不需要手动加../等跳转目录:

	require '../init.class.php';

	$smarty->assign('title',1111);
	$smarty->assign('content',2222);
	$smarty->display('user/addUser.html'); //正确

二、1.smarty3的安装和使用

写在前面:

smarty 是什么,就不多说了,用过php,接触过php的人都对smarty 再熟悉不过了。它是一个很强大的代码分离软件,作为PHP的第三方类库引用到PHP项目中,将PHP代码和HTML完美分开,加速程序员和前端人员协同开发,提高开发速度。

 

1. 下载smarty

smarty 的目前最新版本是3版本。http://www.smarty.net/files/Smarty-3.1.14.zip

下载下来。解压,我们需要是里面的libs 文件夹的内容。复制这个文件夹,到我们的文件的php项目中。

2. 使用smarty

我们前面说过,smarty是php的一个类库导入到项目中,所以第一步,我们就要引入这个类库。

require './libs/Smarty.class.php';

引入进来之后,那么就要new 一个smarty实例了。
//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>

允许之后是可以正常运行的。

3. smarty 的优化

在第2点中的基本设置,smarty 已经可以正常使用,但是,我们在使用中会发现几个问题:

比如在index.html中有如下代码:

<html>
	
	<head>

		<title><{$title}></title>

		<style type="text/css">
			body { 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:wampwwwyangyi2smarty3	plindex.html" on line 8 
"body { 860px;margin: 0px;padding: 0px;}" - Unexpected ": ", expected one of: "}" , " " , 
ATTR' in D:wampwwwyangyi2smarty3libssyspluginssmarty_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 = "}";

默认的是{ } 极容易和css 还有js 的大括号冲突,所以我们要修改一下。

$smarty->left_delimiter  = '<{';
$smarty->right_delimiter = '}>';

接下来把html改一下:
<html>
	
	<head>

		<title><{$title}></title>

		<style type="text/css">
			body { 860px;margin: 0px;padding: 0px;}
		</style>

		<script type="text/javascript">
			function addStips(){alert(123);}
			addStips();
		</script>
		
	</head>
	<body>

		<{$content}>

	</body>	
		
</html>

这样就可以共存了,不要报错误。

4.smarty 的优化-配置文件导入

我们在项目过程中,是一定会有多个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,哪个文件需要smarty,引用进来不就可以了嘛:
//init.class.php

require ROOT.'libs/Smarty.class.php';

$smarty = new Smarty;
$smarty->setTemplateDir('tpl/');
$smarty->setCompileDir('comp/');
$smarty->left_delimiter  = '<{';
$smarty->right_delimiter = '}>';

那么在inde.php中我们就可以这样引用了:
	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 = '}>';

这样,这个init.class.php 文件就可以被任意项目中的目录引用,都不会报错了。

6.smarty 的优化-display()的路径

我们经常发现一些人在用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'); //错的。

其实,是错的,我们只需要记住:这个display永远是相对于init.class.php中设定的tpl的路径,永远是和tpl/目录的。所以不需要手动加../等跳转目录:
	require '../init.class.php';

	$smarty->assign('title',1111);
	$smarty->assign('content',2222);
	$smarty->display('user/addUser.html'); //正确

7.总结

多看源代码,比如,我们忘记了如何设定模板和缓存模板,忘记了设定左定界符和右定界符符合写。那么就可以打来Smarty.class.php 搜一下,多看看这个文件。就好办了。

参考:1.smarty3的安装和使用 - think2me - CSDN博客
https://blog.csdn.net/think2me/article/details/9320985

 
原文地址:https://www.cnblogs.com/Renyi-Fan/p/10117403.html