前两天是五一小长假,而每次假期都想着如何如何刻苦一番,往往是自作多情。。
当然这次是有小病在身,多个借口吧。
一有病就蔫的不行。。。要锻炼了啊,脚估计也差不多了,游泳试试吧这周。
这次学习smarty引擎。
一、视图引擎简介
帮助用户实现mvc中view的开发。
(可以理解为view的模板
--好的视图引擎
贴近html;
语法简单易懂;
良好的缓存机制;
拓展性良好;
网络资源多
(所以先学习已有的视图引擎)
--知名的
smarty,phplib
二、smarty简介
提供了逻辑和外在内容的分离(php程序员和美工工作的分离;
百度的介绍
然后直接官网或者github下载就好;
其中,demo就是官方给的例子,据说挺不错的;
libs里面是我们要引入的文件,如下:
smarty.class.php就是实例化要用到的;plugins是插件,可以自己写写放进来用;
三、smarty的引入、配置与实例制作
1、引入与实例化
首先,还是我个人的环境,前面说过:php5.4(忘记了)+zend+wampserver
创建项目,把demo和libs的东西都放进来
然后在index.php中进行代码编写;
参考smarty.class.php的代码规范,我写了个人的表头
1 php 2 /** 3 * project: mvcsmartytry 4 * file: index.php 5 * 6 * this file is the test file of using smarty. 7 * 8 * @copyright andy liang 9 * @author andy liang10 * @package smarty11 * @version 3.1.30-dev12 */
然后就是简单的引入和实例化
1 namespace mvcsmarty\index; 2 /** 3 * require the main file of smarty 4 */ 5 require 'smarty.class.php'; 6 7 /** 8 * instantiation of smarty 9 */10 $smarty=new \smarty();
注意,实例化的时候可以去看下.class.php中具体的类名什么的;然后require和include的选择也不再赘述,上文说过;
一般来说实例化感觉都是没有最后那个反斜杠的,可能是zend的毛病,我再查查看吧。
2、配置相关
这个特殊的类,搞了一大堆属性和方法,网上有很多配置教程,这里提几个常用的东西;
尤其是两个方法:display和assign
1 /** 2 * configuration of smarty 3 * especially five configs & two functions 4 */ 5 $smarty->left_delimiter = {; 6 $smarty->right_delimiter = }; 7 //其实看源码会发现:和源码中配置的是一样的; 8 //然后还有就是templates模板文件存放地址 9 //templates_c模板编译生成的文件10 //这两个是protected或者private11 //这个版本的smarty中这些设定的修改都是有方法的12 //比如settemplatedir,好像是这个13 //总之多看源码14 $smarty->cache_lifetime = 120;15 16 /**17 * the most frequently-used methods.18 */19 $smarty->assign('articletitle', '文章标题');20 $smarty->display('./templates/test.tpl');
assign就是个赋值,但是是在模板中的赋值;
display,顾名思义就是展示了;
给你看demo
php/** * example application * * @package example-application */require '../smarty.class.php';$smarty = new smarty;//$smarty->force_compile = true;$smarty->debugging = true;$smarty->caching = true;$smarty->cache_lifetime = 120;$smarty->assign(name, fred irving johnathan bradley peppergill, true);$smarty->assign(firstname, array(john, mary, james, henry));$smarty->assign(lastname, array(doe, smith, johnson, case));$smarty->assign(class, array(array(a, b, c, d), array(e, f, g, h), array(i, j, k, l), array(m, n, o, p)));$smarty->assign(contacts, array(array(phone => 1, fax => 2, cell => 3), array(phone => 555-4444, fax => 555-3333, cell => 760-1234)));$smarty->assign(option_values, array(ny, ne, ks, ia, ok, tx));$smarty->assign(option_output, array(new york, nebraska, kansas, iowa, oklahoma, texas));$smarty->assign(option_selected, ne);$smarty->display('index.tpl');
3、demo学习
---------明天更,看bibel去了----------