Smarty入门学习

---------------------------------
安装和设置
---------------------------------

require('../Smarty/Smarty.class.php');
$smarty = new Smarty;
$smarty->template_dir= 'templates/';
$smarty->compile_dir = 'templates_c/';
$smarty->config_dir = 'configs/';
$smarty->cache_dir = 'caches/'; 
$smarty->caching = true;
$smarty->debugging = true;
$smarty->cache_lifetime = 120;
// $smarty->force_compile = true;
// $smarty->left_delimiter = '<{';
// $smarty->right_delimiter = '}>';

 

  

---------------------------------
基本语法
---------------------------------


1.注释
{* 我是Smarty 注释, 在编译输出后我并不存在 *}


2.变量
(1)数学和嵌入标签
{$foo={counter}+3}// 标签里面嵌套标签
(2)定义数组
{assign var=foo value=['y'=>'yellow','b'=>'blue']}
{assign var=foo value=[1,[9,8],3]}
(3)短变量
{$foo = strlen($bar)} 
{$foo.bar=1} 
(4)dot语法
{$foo.a.b.c}        // $foo['a']['b']['c'] 
{$foo.a.$b.c}       // $foo['a'][$b]['c']
(5)可变变量
{$foo_{$x}} // 如果$x=1, 则会输出变量$foo_1的值
(6)对象链:
{$object->method1($x)->method2($y)}
(7)直接使用php函数
{time()}


3.函数
{funcname attr1="val" attr2="val"}
例如:{include file="header.tpl"}


4.属性
{include file="header.tpl"}
{include file="header.tpl" nocache}// 等于nocache=true
{include file="header.tpl" attrib_name="value"} // 多个
{include file=$includeFile}// 变量
{include file=#includeFile# title="My Title"} // config 
{assign var=foo value={counter}}  // 插件
{assign var=foo value=substr($bar,2,5)}  // PHP函数返回值
{assign var=foo value=$bar|strlen}  // 使用调节器
{func var="test $foo test"}// 双引号里嵌入变量
{func var="test $foo.bar test"}// 双引号里嵌入变量(变量可以是表达式,数组等)


5.忽略/转移Smarty解析

<script>
  // 以下大括号的内容会被Smarty忽略:因为它们里面有空格
  function foobar {
  alert('foobar!');
}
// 下面的内容会保持原义输出:因为使用了literal标签
{literal}
  function bazzy {alert('foobar!');}
{/literal}
</script>

 

  

 

 

 

 

 

 

;

转载于:https://www.cnblogs.com/upup2015/p/7782559.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值