smarty2 设置、变量、函数

设置

View Code
<?php
    require './libs/Smarty.class.php';
    
    $smarty = new Smarty;
    $smarty->template_dir = './templates';//模板目录
    $smarty->compile_dir = './templates_c';//编译目录
    $smarty->left_delimiter = '<{';//左边界符
    $smarty->right_delimiter = '}>';//右边界符
    $smarty->caching = false; //缓存是否开启
    $smarty->cache_dir = './smarty_cache';//缓存目录
    
?>

变量分配

View Code
 1     $smarty->assign('str', 'string');
 2     $smarty->assign('int', 123);
 3     $smarty->assign('bool', TRUE);
 4     $smarty->assign('float', 3.14);
 5     $smarty->assign('arr', array('a', 'b'));
 6 //tpl中 $arr.0 $arr.1
 7     $smarty->assign('arr', array('one' => array('name' => 'jlt', 'age' =>'66'), 'b'));
 8 //tpl中 $arr.one.name $arr.one.age   
 9     class Dog {
10         public $name;
11         public $age;
12         public $color;
13         
14         function __construct($name, $age, $color) {
15             $this->name = $name;
16             $this->age = $age;
17             $this->color = $color;
18         }
19     }  
20     $dog = new Dog('name', 99, 'red');
21     $smarty->assign('obj', $dog);
22 //tpl中 $obj->name $obj->age $obj->color

配置信息获取

View Code
1 my.conf
2 
3 title = 'my site name';
4 content = 'my acticle con';
5 
6 tpl中
7 
8 <{config_load file='../my.conf'}>
9 <{#title#}>

smarty保留变量

{$smarty.get.value}

{$smarty.post.value}

{$smarty.cookies.value}

{$smarty.server.SERVER_NAME}

{$smarty.env.value}

{$smarty.session.value}

{$smarty.request.value}

内建函数

<{foreach from=$arr item=temp key=k name=foreachname}>
<{$temp}>===<{$k}>
<{/foreach}>

{if $a > 10}

{elseif $a <50} 

{else}

{/if}

 

posted @ 2013-02-26 23:07  Caps  阅读(174)  评论(0编辑  收藏  举报