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中的定界符

 <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>

我们查看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 {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 = "}";

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

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

接下来把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>

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

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

 

smarty课程---smarty3的安装和使用的更多相关文章

  1. Linux课程---9、安装RPM包(RPM的全称是什么)

    Linux课程---9.安装RPM包(RPM的全称是什么) 一.总结 一句话总结: redhat package management 1.在Packages中查找和php相关的文件如何查找? ls ...

  2. smarty课程---smarty的处理过程是怎样的

    smarty课程---smarty的处理过程是怎样的 一.总结 一句话总结:编译文件里时间戳记录模板文件修改时间,如果模板被修改过就可以检测到,然后重新编译 1. smarty将php源文件,首先编译 ...

  3. smarty课程---最最最简单的smarty例子

    smarty课程---最最最简单的smarty例子 一.总结 一句话总结:其实所有的模板引擎的工作原理是差不多的,无非就是在php程序里面用正则匹配将模板里面的标签替换为php代码从而将两者混合为一个 ...

  4. php 模板框架之smarty 的下载和安装

    Smarty 官网: http://www.smarty.net/ Smarty 下载: https://github.com/smarty-php/smarty/releases/tag/v3.1. ...

  5. PHP smarty模版引擎基本安装

    环境:  PHP5.2 以上版本 先去官网下载smarty模版引擎的库文件到你的电脑或服务器上 smarty官方网站库文件下载地址: https://www.smarty.net/download 下 ...

  6. Android课程---Android Studio安装及使用

    2013年Google I/O 大会首次发布了Android Studio IDE(Android平台集成开发环境).它基于Intellij IDEA 开发环境,旨在取代Eclipse和ADT(And ...

  7. 吴超老师课程--Flume的安装和介绍

    常用的分布式日志收集系统

  8. 吴超老师课程--Sqoop的安装和介绍

    SQOOP是用于对数据进行导入导出的.    (1)把MySQL.Oracle等数据库中的数据导入到HDFS.Hive.HBase中    (2)把HDFS.Hive.HBase中的数据导出到MySQ ...

  9. python入门课程 第二章 安装Python

    2-1 选择python版本首先python2.7和python3是不可以通用的目前丰富的类库都支持python2.7,所以选用Python2.7    选择python2.7版本2-2 window ...

随机推荐

  1. mysql查询表基本操作

    数据库表的创建create table <表名>( <列名> <数据类型及长度> [not null], <列名> <数据类型及长度>, . ...

  2. BufferedImage操作图片笔记(转)

    BufferedImage是Image的一个子类,BufferedImage生成的图片在内存里有一个图像缓冲区,利用这个缓冲区我们可以很方便的操作这个图片,通常用来做图片修改操作如大小变换.图片变灰. ...

  3. 浅谈css中渐变衔接

    无论transition还是keyframes,如何让变化更自然,这是前端应该考虑的问题. 这里,我简单总结下自己的方法. 以实践为例子. 1.图像渐变 @keyframes looppic{ fro ...

  4. UVALive - 7269 I - Snake Carpet

    思路: 多画画就发现从五的时候可以这么填: 六的时候这么填: 七的时候这么填: 看出规律了吗? 没看出的话再画画把. #include <bits/stdc++.h> using name ...

  5. MySQL重装失败,could not start the service MySQL.Error:0

    MySQL5.5 安装失败现象: mysqld.exe [6132] 中发生了未经处理的 win32 异常 could not start the service MySQL.Error:0 1.在 ...

  6. Linux基础命令---comm

    comm 逐行比较两个已经排序过的文件.结果以3列显示:第1列显示只在file1出现的内容,第2列显示只在file2出现的内容,第3列显示同时出现的内容. 此命令的适用范围:RedHat.RHEL.U ...

  7. Ubuntu系统下查看显卡相关信息

    查看显卡信息 root@ubuntu:/home/ubuntu# lspci |grep -i vga 02:00.0 VGA compatible controller: NVIDIA Corpor ...

  8. web前端----jQuery扩展(很重要!!)

    1.jQuery扩展语法 把扩展的内容就可以写到xxxx.js文件了,在主文件中直接导入就行了. 用法1.$.xxx() $.extend({ "GDP": function () ...

  9. 9大行为导致Java程序员薪资过低, 你有几个?

    Java程序员薪水有高有低,有的人一个月可能拿30K.50K,有的人可能只有2K.3K.同样有五年工作经验的Java程序员,可能一个人每月拿20K,一个拿5K.是什么因素导致了这种差异?本文整理导致J ...

  10. 20145310《网络对抗》Exp2 后门原理与实践

    实验内容 (1)使用netcat获取主机操作Shell,cron启动,使用socat获取主机操作Shell, 任务计划启动. (2)使用MSF meterpreter生成可执行文件,利用ncat或so ...