一、基本概念
1.什么是mvc?
    mvc是一种开发模式,核心思想是:数据的输入、数据的处理、数据显示的强制分离。
2.什么是smarty?
    smarty是一个php的模板引擎。更明白的来说,它能够帮助开发人员更好的分离程序逻辑和页面显示。
3.smarty执行原理
        模板文件,就是一个显示数据的模板,当中须要显示的数据用占位符取代。
        smarty执行时,会读取模板文件,将模板文件里的占位符替换成真正的数据,并输出一个处理后的php文件,交由server执行。

二、自己写一个smarty模板
    为了更好的理解smarty模板,如今自己先写一个自己的smarty模板-minismarty,让自己更加深入的了解smarty执行原理。
1.新建项目minismarty
    新建模板文件路径:templates
    新建模板文件被编译后的文件路径:templates_c
    新建模板文件:intro.tpl
    新建执行的文件:index.php
    新建自己的smarty,即处理模板的文件:cls_MiniSmarty.php
2.编写index.php文件

<?php
require_once './cls_MiniSmarty.php';
$miniSmarty = new MiniSmarty();
//传递数据
$miniSmarty->assign("title","hello minismarty!");
$miniSmarty->assign("content","<font color='red'>this is content!</font>");
//传递数据到哪个页面显示
$miniSmarty->display("intro.tpl");
?>

3.编写intro.tpl文件

<!--这是个模板文件-->
<html>
<head>
<meta http-equiv="Content-Language" content="en" />
<meta name="GENERATOR" content="PHPEclipse 1.0" />
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>{$title}</title>
</head>
<body bgcolor="#FFFFFF" text="#000000" link="#FF9966" vlink="#FF9966" alink="#FFCC99">
{$content}
</body>
</html>
 这里面的内容是用占位符的形式,smarty的作用就是将占位符的内容替换成真正的数据。
    这样就能够实现模板文件和数据文件强制分离,通过smarty进行数据的传递。
4.编写cls_MiniSmarty.php文件

<?php
/**
*
* 原本是通过smarty模板引擎给模板提供数据的
* 如今自己模仿写一个模板,给模板提供数据的类
* smarty执行时,读取模板文件,将模板文件替换成可执行的php文件。server真正执行的文件是处理后的文件。
*/
class MiniSmarty {
//模板文件路径
var $template_dir = "./templates/";
//模板文件被替换后的文件路径
var $templates_c_dir = "./templates_c/";
//存放变量值
var $tpl_vars = array ();
//主要模拟2个方法
/**
* 加入数据
* 參数1:键
* 參数2:值,默觉得null
*/
function assign($tpl_var, $var = null) {
if ($tpl_var != '') {
$this->tpl_vars[$tpl_var] = $var; //将数据加入到数组中
}
}
/**
* 显示数据
* 參数1:显示到哪个模板文件里
*/
function display($tpl_file) {
//获得模板文件的路径
$tpl_file_path = $this->template_dir . $tpl_file;
//获得模板文件被编译后的文件路径
$compile_file_path = $this->templates_c_dir . "com_" . $tpl_file . ".php";
//推断文件是否存在
if (!file_exists($tpl_file_path)) {
return false;
}
//不用每次都生成编译文件,仅仅有编译文件不存在或者模板文件被改动了才生成新的编译文件
//相当于缓存了编译文件
//filemtime函数:获得文件的生成时间
if (!file_exists($compile_file_path) || filemtime($tpl_file_path) > filemtime($compile_file_path)) {
//读取模板文件的内容
$fpl_file_content = file_get_contents($tpl_file_path);
$newStr = myReplace($fpl_file_content);
//将替换后的字符串生成新的文件,也就是编译后的文件
file_put_contents($compile_file_path, $newStr);
}
//引入编译后的文件
include $compile_file_path;
}
/**
* 对模板文件里的内容进行替换,获得新的字符串
*/
function myReplace($fpl_file_content) {
$pattern = array (
'/\{\s*\$([a-zA-Z_][a-zA-Z0-9_]*)\s*\}/i'
);
$replace = array (
'<?php echo $this->tpl_vars["${1}"] ?>'
);
$newStr = preg_replace($pattern, $replace, $fpl_file_content);
return $newStr;
}
}
?>
preg_replace方法介绍:
    參数1:替换的规则
    參数2:替换成的内容
    參数3:替换操作的内容
5.执行结果
    标题和内容都显示出来了

结论:
    真正执行的文件,既不是index.php,也不是intro.php,而是二者通过smarty作用后的文件:
com_intro.tpl.php。这个文件里数据来源于index.php,显示的布局来自intro.tpl,中间的桥梁是smarty。
    smarty的作用是接受数据、填充数据(替换模板中的占位符)、并载入替换后的文件。

三、解说smarty使用细节
1.怎样配置smarty?
    解压后,将libs目录复制到项目目录下就可以,然后再创建2个目录templates和templates_c,分别放模板文件和模板编译后文件。

2.使用smarty注意事项
①替换变量的标示符。
    由于默认的表示符是{}这个和style样式中的{}发生冲突,所以须要改动一下默认的标识符,一般改动为:{<>}
②改动标识符的方法。
    方法一:直接改动smarty类源代码:不推荐。
    方法二:使用smarty提供的方法进行改动。
    $smarty->left_delimiter="{<";

$smarty->right_delimiter=">}";

③smarty的一些基本配置
    $smarty->template_dir="./templates";//模板路径
    $smarty->compile_dir="./templates_c";//编译路径
    $smarty->caching=false;    //是否使用缓存

$smarty->cache_dir="./smarty_cache";//假设使用缓存的话:缓存的路径  


3.smarty模板技术分配变量的细节问题
    一句话:能够分配php支持的各种数据。
    php基本数据:int double string bool
    复合数据类型:array object
    特殊数据类型:resource null

最新文章

  1. mui 下拉刷新
  2. PHP 判断客户端是IOS还是Android
  3. AndroidStudio调试APP
  4. 【转】如何使用PhoneGap打包Web App
  5. Hibernate笔记——(ONE TO ONE)一对一
  6. 应用MVP模式写出可维护的优美Android应用
  7. windows内核初窥(二)-----系统机制
  8. How to enable/disable EWF
  9. &lt;blockquote&gt;标签,长文本引用
  10. centos安装redis-3.2.3
  11. 一个大数据平台省了20个IT人力——敦奴数据平台建设案例分享
  12. JavaScript表单
  13. 如何使用纯CSS制作特效导航条?
  14. N76E003的环境搭建
  15. hdu 1072(BFS) 有炸弹
  16. Delphi中快速修改变量名称
  17. Automapper实现自动映射
  18. 可简单避免的三个 JavaScript 发布错误
  19. Hadoop的集群和优化
  20. 清北学堂学习总结 day2 字符串 练习

热门文章

  1. HDU 2253 Longest Common Subsequence Again
  2. Android studio树形
  3. HDU 4406 GPA
  4. Ubuntu 14.04 SNMP安装与配置
  5. [Recompose] Stream Props to React Children with RxJS
  6. 伸缩--也可用于tabs
  7. Swift的闭包,枚举,类和结构体
  8. How to search Installed Updates
  9. Flutter入门
  10. 进阶《Python高性能编程》中文PDF+英文PDF+源代码