Smarty学习

Smarty:

http://www.smarty.net/download

http://www.smarty.net/documentation

1. 引入Smarty.class.php

define('SMARTY_DIR','/usr/local/lib/php/Smarty/'); require(SMARTY_DIR.'Smarty.class.php'); $smarty = new Smarty;2. templates, templates_c, configs and cache(Smarty要求4个目录,默认下命名为。每个都是可以自定义的,可以修改Smarty类属性:
$template_dir, $compile_dir, $config_dir, and $cache_dir respectively。最好将这4个目录和网页文件目录分开来)
Smarty的 $compile_dir 和$cache_dir必须可写。通常是user "nobody" 和 group "nobody"。
{* Smarty *} 是一个模板注释

// load Smarty libraryrequire('Smarty.class.php'); $smarty = new Smarty;$smarty->template_dir = '/web/www.mydomain.com/smarty/guestbook/templates/'; $smarty->compile_dir = '/web/www.mydomain.com/smarty/guestbook/templates_c/'; $smarty->config_dir = '/web/www.mydomain.com/smarty/guestbook/configs/'; $smarty->cache_dir = '/web/www.mydomain.com/smarty/guestbook/cache/'; $smarty->assign('name','Ned'); $smarty->display('index.tpl');//3.08 Smartypublic function __construct() { // selfpointer need by some other class methods$this->smarty = $this; if (is_callable('mb_internal_encoding')) { mb_internal_encoding(SMARTY_RESOURCE_CHAR_SET); } $this->start_time = microtime(true); // set default dirs $this->template_dir = array('.' . DS . 'templates' . DS); $this->compile_dir = '.' . DS . 'templates_c' . DS; $this->plugins_dir = array(SMARTY_PLUGINS_DIR); $this->cache_dir = '.' . DS . 'cache' . DS; $this->config_dir = '.' . DS . 'configs' . DS; $this->debug_tpl = 'file:' . SMARTY_DIR . 'debug.tpl'; if (isset($_SERVER['SCRIPT_NAME'])) { $this->assignGlobal('SCRIPT_NAME', $_SERVER['SCRIPT_NAME']); } }

3. 模板文件放在$this->template_dir文件夹下。 默认.tpl

$smarty = new Smarty(); $smarty->allow_php_templates= true; $smarty->force_compile = false;$smarty->caching = true; $smarty->cache_lifetime = 100;//$smarty->debugging = true; $foo = 'xxoo';$smarty->assign('foo', $foo); $person = new Person;$smarty->assign('person',$person); $smarty->assign('array',array('a'=>array('aa'=>'This is a long string'),'b'=>2)); $smarty->display('php:index_view.php');

4. 注释{* 注释 *}


5. 变量 (变量前缀$可以配置)

分:从PHP分配的变量、从配置文件读取的变量、{$smarty} 保留变量 输出变量:{$Name}

5.1.1 数组 用‘.’ array($k => $v); [下标] array(0,1,2……)

5.1.2 对象 ‘->’

5.1.3 配置文件中 {#varname#} , {$smarty.config.varname}

5.2 保留变量{$smarty}、页面请求变量

5.2.1 $smarty.get 、post、request、cookies、server、env、session。 eg:{$smarty.get.varname}

5.2.2 $smarty.now 、const、captured、config、'{$smarty.section}, {$smarty.foreach}'、template


6. 显示文件 $smarty->display('./extras/headerT.tpl');


7. 在模板中引入文件{include file="header.tpl" key=val}


8. 变量调节器用于变量,自定义函数和字符串

a.‘|’符号和调节器名称应用调节器b.参数由‘:’符号分开eg:{$topic|truncate:40:"..."}

组合修改器对于同一个变量,你可以使用多个修改器

capitalize将变量里的所有单词首字大写 (ucwords())

count_characters:true计算变量里的字符数,不带‘:true’不及时空格(strlen())

cat连接到给定的变量后面{$articleTitle|cat:" yesterday."}

count_paragraphs计算变量里的段落数量

count_sentences 计算变量里句子的数量

count_words计算变量里的词数

date_format格式化日期{$smarty.now|date_format:"%Y/%m/%d %H:%M:%S"}

default:'默认值' 为空变量设置一个默认值,当变量为空或者未分配的时候

escape[编码]用于html转码,url转码,在没有转码的变量上转换单引号,十六进制转码,十六进制美化,或者javascript转码。默认是html转码 eg:{$articleTitle|escape:"html"} htmlall、url、quotes、

indent[缩进]lower 小写upper 大写nl2br 换行符替换成<br />regex_replace 正则替换 replace 替换spacify [插空] 在字符中间插空string_format 字符串格式化,使用sprintf语法格式化 eg:{$number|string_format:"%.2f"}strip 去除(多余空格)strip_tags 去除html标签truncate [截取]wordwrap 行宽约束

9. 内建函数

9.1 capture函数的作用是捕获模板输出的数据并将其存储到一个变量里,而不是把它们输出到页面. 【ob_start() 和 ob_get_clean】
任何在 {capture name="foo"}和{/capture}之间的数据将被存储到变量$foo中,该变量由name属性指定.
在模板中通过 $smarty.capture.foo 访问该变量.

9.2 config_load 从配置文件中加载变量(文件在configs文件夹)

{config_load file="test.conf" section="con"}

9.3 foreach,foreachelse用于处理简单数组

foreach 必须和 /foreach 成对使用,且必须指定 from 和 item 属性

from 待循环数组的名称

item 当前处理元素的变量名称

key 当前处理元素的键名

name 该循环的名称,用于访问该循环

9.4 include 用于在当前模板中包含其它模板

file 待包含的模板文件名

assign 该属性指定一个变量保存待包含模板的输出

[var ...] 传递给待包含模板的本地参数,只在待包含模板中有效

9.5 inluce_php 函数用于在模板中包含 php 脚本 (它使得 php 代码从模板文件中被分离出来)

9.6 Insert 函数类似欲 inluce 函数,不同之处是 insert 所包含的内容不会被缓存,每次调用该模板都会重新执行该函数.

name 插入函数的名称 function (insert_name)

assign 该属性指定一个变量保存待插入函数输出

script 插入函数前需要先包含的php脚本名称

[var ...] 传递给待插入函数的本地参数

9.7 if,elseif,else

if 必须于 /if 成对出现. 可以使用 else 和 elseif 子句

eq、ne、neq、gt、lt、lte、le、gte、ge、is even、is odd、is not even、is not odd、not、mod、div by、even by、odd by、==、!=、>、<、<=、>=.

9.8 ldelim 和 rdelim 用于输出分隔符,也就是大括号 "{" 和 "}".

9.9 Literal 标签区域内的数据将被当作文本处理

{literal}{/literal} 标签中时,模板引擎将不分析它们,而直接显示.

9.10 php 标签允许在模板中直接嵌入 php 脚本

是否处理这些语句取决于 allow_php_tag 的设置( allow_php_tag = true )


9.11 section,sectionelse

name 该循环的名称

loop 决定循环次数的变量名称

start 0 循环执行的初始位置. 如果该值为负数,开始位置从数组的尾部算起. 例如:如果数组中有7个元素,指定start为-2,那么指向当前数组的索引为5. 非法值(超过了循环数组的下限)将被自动调整为最接近的合法值.

step 1 该值决定循环的步长. 例如指定step=2将只遍历下标为0、2、4等的元素. 如果step为负值,那么遍历数组的时候从后向前遍历.

max 1 设定循环最大执行次数.

show true 决定是否显示该循环.

9.12 strip

Smarty 在显示前将除区任何位于 {strip}{/strip} 标记中数据的首尾空格和回车

#========== 9 ==========# #------2 config_load------# {* config file 上面是全局,下面是段落 *} title = Welcome to Smarty! cutoff_size = 40 [setup]bold = true [config]html1 = 'htmlxx1' html2 = 'htmlxx2' #------3 foreach,foreachelse------# {* $smarty->assign('custid',array(1001,2001,3001)); $smarty->assign("contacts", array( array("phone" => "1", "fax" => "2", "cell" => "3"), array("phone" => "555-4444", "fax" => "555-3333", "cell" => "760-1234") ) ); *} {foreach from=$custid item=curr_id} id: {$curr_id}<br> {/foreach} {foreach name=outer item=contact from=$contacts} {foreach key=key item=item from=$contact} {$key}: {$item}<br> {/foreach} {/foreach} #------4 include------# {* absolute filepath *} {include file="/usr/local/include/templates/header.tpl" title="Main Menu" table_bgcolor="#c0c0c0"} {* absolute filepath (same thing) *} {include file="file:/usr/local/include/templates/header.tpl"} {* windows absolute filepath (MUST use "file:" prefix) *} {include file="file:C:/www/pub/templates/header.tpl"} {* include from template resource named "db" *} {include file="db:header.tpl"} #------5 include_php------# {include_php file="/path/to/load_nav.php"} #------6 insert------# {* example of fetching a bannerSmarty 调用该函数类似insert_getBanner(array("lid"=>"12345","sid"=>67890"));并将返回的结果显示在调用的位置*} {insert name="getBanner" lid#banner_location_id# sid#site_id#} #------7 if,elseif,else------# {if $name eq "Fred"} Welcome Sir. {elseif $name eq "Wilma"} Welcome Ma'am. {else} Welcome, whatever you are. {/if} #------9.10 php------# {php} echo time();{/php}

10 自定义函数

10.1 assign 用于在模板被执行时为模板变量赋值

{assign var="name" value="Bob"}

var string 被赋值的变量名

value string 赋给变量的值

10.2 counter

{counter start=0 skip=2 print=true}

name string No default 计数器的名称

start number No 1 记数器初始值

skip number No 1 记数器间隔、步长

direction string No up 记数器方向,(增/减)

print boolean No true 是否输出值

assign string No n/a 输出值将被赋给模板变量的名称

10.3 Cycle

用于轮转使用一组值

10.4 debug 将调式信息输出到页面上

10.5

10.6 fetch 用于从本地文件系统、HTTP或FTP上取得文件并显示文件的内容. 如果文件名称以"http://"开头,将取得该网站页面并显示. 如果文件名称以"ftp://"开头,将从ftp服务器取得该文件并显示.

filestringYesn/a 待请求的文件,http或ftp方式

assignstringNon/a 输出值将被赋给模板变量的名称

10.7 html_checkboxes 创建复选按钮组

要么必须指定 values 和 ouput 属性,要么指定 options 替代

name string No checkbox 复选按钮组的名称

values array Yes, 或指定 options 属性 n/a 包含复选按钮组值的数组

output array Yes, 或指定 options 属性 n/a 包含复选按钮组显示值的数组

selected string/array No empty 已选定的元素或元素数组

options associative array Yes,或指定 values 属性 n/a 包含值和显示的关联数组

separator string No empty 分隔每个复选按钮的字符串

labels boolean No true 是否为每个复选按钮添加 <label> 标签


11.

在非生产环境下,$smarty->compile_check = false; 关闭编译检查变量。用于检测当前模板是否被修改过


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值