Smarty模板引擎的使用

Smarty是PHP中一个基于MVC模式的模板引擎。

Download: http://www.smarty.net/download

 

特点

1、  最快速度的程序开发模板

2、  自定义模板的界定符

3、  可以使用条件判断语句if/elseif/else/endif

4、  内建缓存支持

5、  可以自定义插件.

Smarty其实很简单,目前的3.1.14版本中的一个Demo的目录如下:

只有简单的几个文件夹。

Index.php

<?php
/**
* Example Application * @package Example-application
*/ //载入Smarty的核心类库文件
require('libs/Smarty.class.php');
//实例化一个Smarty
$smarty = new Smarty; //$smarty->force_compile = true;
//调试模式,会弹出调试框,查看变量值
$smarty->debugging = true;
//缓存模式
$smarty->caching = true;
//缓存时间
$smarty->cache_lifetime = 120; //定义一个数据的MAP,将数据发送到模板中去
$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');
?>

使用的都是默认的各个目录文件夹,显示如资源列表图。

当然,还有很多的参数设置,可以参考Smarty.class.php文件,不做详细介绍了。

index.tpl 中的解析

{*这样的是注释*}
{* {} 是界定符 *} {*载入 配置文件 *}
{config_load file="test.conf" section="setup"} {* 载入一个文件,并传入一个变量 title = foo *}
{include file="header.tpl" title=foo} <PRE> {* ##是从config 文件读取的配置数据 *}
{if #bold#}<b>{/if}
{*
‘|’ 是针对变量的修饰符,
capitalize 首字母大写
upper 全部大写
lower 全部小写
nl2br 将换行符换位<br/>
date_format:"%Y-%M-%D" 格式化时间
replace:"value1":"value2" 替换,使用value2替换value1
dafault 默认值
cat:"characters" 追加字符
.....
*}
Title: {#title#|capitalize}
{if #bold#}</b>{/if} The current date and time is {$smarty.now|date_format:"%Y-%m-%d %H:%M:%S"} The value of global assigned variable $SCRIPT_NAME is {$SCRIPT_NAME} {*
{$smarty.server.SERVER_NAME} 服务器 地址
*}
Example of accessing server environment variable SERVER_NAME: {$smarty.server.SERVER_NAME} The value of {ldelim}$Name{rdelim} is <b>{$Name}</b>
{*
{ldelim} 左界定符
{rdelim} 右界定符
*}
variable modifier example of {ldelim}$Name|upper{rdelim} <b>{$Name|upper}</b> An example of a section loop: {section name=outer
loop=$FirstName}
{* 没两次 交替 一次*}
{if $smarty.section.outer.index is odd by 2}
{$smarty.section.outer.rownum} . {$FirstName[outer]} {$LastName[outer]}
{else}
{$smarty.section.outer.rownum} * {$FirstName[outer]} {$LastName[outer]}
{/if}
{sectionelse}
none
{/section} An example of section looped key values:
{* 类似 泛型的枚举 循环*}
{section name=sec1 loop=$contacts}
phone: {$contacts[sec1].phone}<br>
fax: {$contacts[sec1].fax}<br>
cell: {$contacts[sec1].cell}<br>
{/section}
<p> testing strip tags
{strip}
<table border=0>
<tr>
<td>
<A HREF="{$SCRIPT_NAME}">
<font color="red">This is a test </font>
</A>
</td>
</tr>
</table>
{/strip} </PRE> This is an example of the html_select_date function: {* 时间选择器*}
<form>
{html_select_date start_year=1998 end_year=2010}
</form> This is an example of the html_select_time function: <form>
{html_select_time use_24_hours=false}
</form> This is an example of the html_options function: {* option 枚举*}
<form>
<select name=states>
{html_options values=$option_values selected=$option_selected output=$option_output}
</select>
</form> {include file="footer.tpl"}

在服务器上显示的  index.php就显示如下了

更多的详细介绍,查看官网http://www.smarty.net/docs/zh_CN/

Smarty模板引擎的使用的更多相关文章

  1. smarty模板引擎

    1.    使用smarty 1.1 项目引入 // 3, 连接数据库,提取相关数据 $title = "Smarty模板引擎"; $content = "Smarty模 ...

  2. 写一个迷你版Smarty模板引擎,对认识模板引擎原理非常好(附代码)

    前些时间在看创智博客韩顺平的Smarty模板引擎教程,再结合自己跟李炎恢第二季开发中CMS系统写的tpl模板引擎.今天就写一个迷你版的Smarty引擎,虽然说我并没有深入分析过Smarty的源码,但是 ...

  3. 深入浅出之Smarty模板引擎工作机制(二)

    源代码下载地址:深入浅出之Smarty模板引擎工作机制 接下来根据以下的Smarty模板引擎原理流程图开发一个自己的模板引擎用于学习,以便加深理解. Smarty模板引擎的原理,其实是这么一个过程: ...

  4. 深入浅出之Smarty模板引擎工作机制(一)

    深入浅出Smarty模板引擎工作机制,我们将对比使用smarty模板引擎和没使用smarty模板引擎的两种开发方式的区别,并动手开发一个自己的模板引擎,以便加深对smarty模板引擎工作机制的理解. ...

  5. Smarty模板引擎技术二

    Smarty模板引擎技术 内建函数 include_php内建函数 作用:载入一个php文件,将载入的文件的内容赋值给一个变量   注意:该内建函数只能在2.0中使用,如果使用的话,必须得实例化Sma ...

  6. Smarty模板引擎技术

    Smarty模板引擎技术 什么是模板引擎? 什么是Smarty模板引擎? 为何选择Smarty模板引擎? 如何使用Smarty模板引擎? 一.历史背景 场景一:回顾之前编写PHP项目的方式 //链接数 ...

  7. Ci框架整合smarty模板引擎

    Ci框架整合smarty模板引擎 备注:下载smarty时,最好选择2.6版本,其他测试有坑,ci可以是2.2或其他 大体思路:将smarty封装成ci框架的一个类,然后重新配置一下smarty,这样 ...

  8. 迷你版 smarty --模板引擎和解析

    http://blog.ipodmp.com/archives/php-write-a-mini-smarty-template-engine/ 迷你版Smarty模板引擎目录结构如下: ① 要开发一 ...

  9. smarty模板引擎原理解析

    //php 控制器文件 <?php//引入模板引擎文件include("20130304.php");$smarty =newTinySmarty();$qq_numbers ...

  10. smarty模板引擎中section循环loop与total的区别

    在smarty模板引擎的section循环中 $data=[101,102,103,105,104]; section的两个属性total与loop {section foo $data start= ...

随机推荐

  1. netduino第一步,环境配置

    在netduino.com的官网介绍下,我很快就入门,现在的最新netduino的版本是4.3,但4.3是运行在win8下的,在codeplex.net上有,大部分人还使用的是win7,因此我现在采用 ...

  2. DataTable的一些使用技巧

    在做机房的时候经常用到DataTable,发现如果DataTable使用的好的话,不仅能使程序简洁实用,而且能够提高性能,达到事半功倍的效果.现在对我知道的一些技巧做个总结,虽然都是一些简单的,但是发 ...

  3. Android控件拖动的实现

    这个也是从网上得到的代码,例子比较简单,但是如果有需要此功能的,这个例子可以提供很多提示,首先,给个截图 这个是拖动以后的效果,一个imageview和一个button控件,提供两份代码下载吧,一份是 ...

  4. cp命令的实现

    #include <unistd.h> #include <fcntl.h> #include <stdlib.h> #define BUFFERSIZE 4096 ...

  5. 待机状态下,服务类型 WCDMA Service type in Idle mode

    Services aredistinguished into categories defined in [7]; also the categorisation of cellsaccording ...

  6. [置顶] cocos2d-x 3.0游戏开发xcode5帅印博客教学 004.[HoldTail]主角的上下飞行跟移动

    cocos2d-x 3.0游戏开发xcode5帅印博客教学 004.[HoldTail]主角的上下飞行跟移动 写给大家的前言,在学习cocos2d-x的时候自己走了很多的弯路,也遇到了很多很多问题,不 ...

  7. Attach()函数和Detach()函数的作用

    基本就是把一个句柄绑定和解绑定于一个类对象上,是其可以使用MFC的函数而不是API 首先,你要明白Windows对象和MFC对象的区别.MFC对象实际上并没有把整个Windows对象都包装在其中,它只 ...

  8. vc中改变对话框的背景色

    ---- 笔者曾在<软件报>2000年第5期中讨论过如何改变控件的颜色,但还有相当一部分的读者来信提问:一个基于对话框的MFC AppWizard应用程序中,如何改变对话框的背景颜色呢?对 ...

  9. hdu 1665 That Nice Euler Circuit(欧拉定理)

    输入n个点,然后从第一个点开始,依次链接点i->点i+1,最后回到第一点(输入中的点n),求得到的图形将平面分成了多少部分. 根据欧拉定理 v_num + f_num - e_num = 2可知 ...

  10. 8086 CPU 寻址方式

    8086 CPU 寻址方式灵活.有以下几种 idata 表示常量 1.   [ idata ] 用一个常量来表示地址,可用于直接定位内存单元,但是在 MASM中要显实在的说明 ds 段寄存器, 比如 ...