Home > php教程 > php手册 > body text

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

WBOY
Release: 2016-05-20 10:14:55
Original
1256 people have browsed it

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

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

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

这次学习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>
Copy after login

 

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

<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();
Copy after login

 

注意,实例化的时候可以去看下.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');
Copy after login

 

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');
Copy after login

3、Demo学习

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

 

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template