首页 php教程 php手册 MVC架构学习之Smarty学习病来而蔫 - 韧还

MVC架构学习之Smarty学习病来而蔫 - 韧还

May 20, 2016 am 10:14 AM

前两天是五一小长假,而每次假期都想着如何如何刻苦一番,往往是自作多情。。

当然这次是有小病在身,多个借口吧。

一有病就蔫的不行。。。要锻炼了啊,脚估计也差不多了,游泳试试吧这周。

这次学习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的代码规范,我写了个人的表头

<span style="color: #008080;"> 1</span> <span style="color: #000000;">php
</span><span style="color: #008080;"> 2</span> <span style="color: #008000;">/*</span><span style="color: #008000;">*
</span><span style="color: #008080;"> 3</span> <span style="color: #008000;"> * Project: MVCSmartyTry
</span><span style="color: #008080;"> 4</span> <span style="color: #008000;"> * File: index.php
</span><span style="color: #008080;"> 5</span> <span style="color: #008000;"> * 
</span><span style="color: #008080;"> 6</span> <span style="color: #008000;"> * This file is the test file of using Smarty.
</span><span style="color: #008080;"> 7</span> <span style="color: #008000;"> * 
</span><span style="color: #008080;"> 8</span> <span style="color: #008000;"> * @copyright Andy Liang
</span><span style="color: #008080;"> 9</span> <span style="color: #008000;"> * @author Andy Liang
</span><span style="color: #008080;">10</span> <span style="color: #008000;"> * @package   Smarty
</span><span style="color: #008080;">11</span> <span style="color: #008000;"> * @version   3.1.30-dev
</span><span style="color: #008080;">12</span>  <span style="color: #008000;">*/</span>
登录后复制

 

然后就是简单的引入和实例化

<span style="color: #008080;"> 1</span> <span style="color: #000000;">namespace MVCSmarty\index;
</span><span style="color: #008080;"> 2</span> <span style="color: #008000;">/*</span><span style="color: #008000;">*
</span><span style="color: #008080;"> 3</span> <span style="color: #008000;"> * Require the main file of Smarty
</span><span style="color: #008080;"> 4</span>  <span style="color: #008000;">*/</span>
<span style="color: #008080;"> 5</span> <span style="color: #0000ff;">require</span> 'Smarty.class.php'<span style="color: #000000;">;
</span><span style="color: #008080;"> 6</span>  
<span style="color: #008080;"> 7</span> <span style="color: #008000;">/*</span><span style="color: #008000;">*
</span><span style="color: #008080;"> 8</span> <span style="color: #008000;"> * Instantiation of Smarty
</span><span style="color: #008080;"> 9</span>  <span style="color: #008000;">*/</span>
<span style="color: #008080;">10</span> <span style="color: #800080;">$smarty</span>=<span style="color: #0000ff;">new</span> \Smarty();
登录后复制

 

注意,实例化的时候可以去看下.class.php中具体的类名什么的;然后require和include的选择也不再赘述,上文说过;

一般来说实例化感觉都是没有最后那个反斜杠的,可能是Zend的毛病,我再查查看吧。

2、配置相关

这个特殊的类,搞了一大堆属性和方法,网上有很多配置教程,这里提几个常用的东西;

尤其是两个方法:display和assign

<span style="color: #008080;"> 1</span> <span style="color: #008000;">/*</span><span style="color: #008000;">*
</span><span style="color: #008080;"> 2</span> <span style="color: #008000;"> * Configuration of Smarty
</span><span style="color: #008080;"> 3</span> <span style="color: #008000;"> * Especially five configs & two functions
</span><span style="color: #008080;"> 4</span>  <span style="color: #008000;">*/</span>
<span style="color: #008080;"> 5</span> <span style="color: #800080;">$smarty</span>->left_delimiter = "{"<span style="color: #000000;">;
</span><span style="color: #008080;"> 6</span> <span style="color: #800080;">$smarty</span>->right_delimiter = "}"<span style="color: #000000;">;
</span><span style="color: #008080;"> 7</span> <span style="color: #008000;">//</span><span style="color: #008000;">其实看源码会发现:和源码中配置的是一样的;
</span><span style="color: #008080;"> 8</span> <span style="color: #008000;">//然后还有就是templates模板文件存放地址
</span><span style="color: #008080;"> 9</span> <span style="color: #008000;">//templates_c模板编译生成的文件
</span><span style="color: #008080;">10</span> <span style="color: #008000;">//这两个是protected或者private
</span><span style="color: #008080;">11</span> <span style="color: #008000;">//这个版本的Smarty中这些设定的修改都是有方法的
</span><span style="color: #008080;">12</span> <span style="color: #008000;">//比如setTemplateDir,好像是这个
</span><span style="color: #008080;">13</span> <span style="color: #008000;">//总之多看源码</span>
<span style="color: #008080;">14</span> <span style="color: #800080;">$smarty</span>->cache_lifetime = 120<span style="color: #000000;">;
</span><span style="color: #008080;">15</span> 
<span style="color: #008080;">16</span> <span style="color: #008000;">/*</span><span style="color: #008000;">*
</span><span style="color: #008080;">17</span> <span style="color: #008000;"> * The most frequently-used methods.
</span><span style="color: #008080;">18</span>  <span style="color: #008000;">*/</span>
<span style="color: #008080;">19</span> <span style="color: #800080;">$smarty</span>->assign('articleTitle', '文章标题'<span style="color: #000000;">);
</span><span style="color: #008080;">20</span> <span style="color: #800080;">$smarty</span>->display('./templates/test.tpl');
登录后复制

 

assign就是个赋值,但是是在模板中的赋值;

display,顾名思义就是展示了;

给你看demo

<span style="color: #000000;">php
</span><span style="color: #008000;">/*</span><span style="color: #008000;">*
 * Example Application
 *
 * @package Example-application
 </span><span style="color: #008000;">*/</span>

<span style="color: #0000ff;">require</span> '../Smarty.class.php'<span style="color: #000000;">;

</span><span style="color: #800080;">$smarty</span> = <span style="color: #0000ff;">new</span><span style="color: #000000;"> Smarty;

</span><span style="color: #008000;">//</span><span style="color: #008000;">$smarty->force_compile = true;</span>
<span style="color: #800080;">$smarty</span>->debugging = <span style="color: #0000ff;">true</span><span style="color: #000000;">;
</span><span style="color: #800080;">$smarty</span>->caching = <span style="color: #0000ff;">true</span><span style="color: #000000;">;
</span><span style="color: #800080;">$smarty</span>->cache_lifetime = 120<span style="color: #000000;">;

</span><span style="color: #800080;">$smarty</span>->assign("Name", "Fred Irving Johnathan Bradley Peppergill", <span style="color: #0000ff;">true</span><span style="color: #000000;">);
</span><span style="color: #800080;">$smarty</span>->assign("FirstName", <span style="color: #0000ff;">array</span>("John", "Mary", "James", "Henry"<span style="color: #000000;">));
</span><span style="color: #800080;">$smarty</span>->assign("LastName", <span style="color: #0000ff;">array</span>("Doe", "Smith", "Johnson", "Case"<span style="color: #000000;">));
</span><span style="color: #800080;">$smarty</span>->assign("Class", <span style="color: #0000ff;">array</span>(<span style="color: #0000ff;">array</span>("A", "B", "C", "D"), <span style="color: #0000ff;">array</span>("E", "F", "G", "H"), <span style="color: #0000ff;">array</span>("I", "J", "K", "L"),
                               <span style="color: #0000ff;">array</span>("M", "N", "O", "P"<span style="color: #000000;">)));

</span><span style="color: #800080;">$smarty</span>->assign("contacts", <span style="color: #0000ff;">array</span>(<span style="color: #0000ff;">array</span>("phone" => "1", "fax" => "2", "cell" => "3"),
                                  <span style="color: #0000ff;">array</span>("phone" => "555-4444", "fax" => "555-3333", "cell" => "760-1234"<span style="color: #000000;">)));

</span><span style="color: #800080;">$smarty</span>->assign("option_values", <span style="color: #0000ff;">array</span>("NY", "NE", "KS", "IA", "OK", "TX"<span style="color: #000000;">));
</span><span style="color: #800080;">$smarty</span>->assign("option_output", <span style="color: #0000ff;">array</span>("New York", "Nebraska", "Kansas", "Iowa", "Oklahoma", "Texas"<span style="color: #000000;">));
</span><span style="color: #800080;">$smarty</span>->assign("option_selected", "NE"<span style="color: #000000;">);

</span><span style="color: #800080;">$smarty</span>->display('index.tpl');
登录后复制

3、Demo学习

---------明天更,看bibel去了----------

 

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

仓库:如何复兴队友
1 个月前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.能量晶体解释及其做什么(黄色晶体)
2 周前 By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒险:如何获得巨型种子
1 个月前 By 尊渡假赌尊渡假赌尊渡假赌

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)