require './smarty/Smarty.class.php'; $sm = new Smarty; $sm->setTemplateDir("./dir");//设置模板路径 $sm->left_delimiter = '<{';//设置左右边界符 $sm->right_delimiter = '}>'; $sm->caching = true;//是否开启缓存 $sm->cache_lifetime = 120;//缓存生命周期 $d = "这个雨季,快结束吧2"; $arr = array('a'=>'小明','b'=>'大欧'); class A{ public $b='bbb'; } $obj = new A(); $sm->assign('mytitle',$d); $sm->assign('obj',$obj); $sm->assign('t',$arr); $sm->display('2.html');
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <style> h1{font-size: 12px;} </style> </head> <body> <{Config_load file="xxx.conf"}> <h1><{$smarty.config.bb}></h1> <h1><{$mytitle}></h1> <h1><{$t['a']}></h1> <h1><{$t.a}></h1> <h1><{$obj->b}></h1> <h1><{$smarty.now}></h1> <h1><{$smarty.get.id}></h1><!--获得$_GET['id'] --> <h1></h1> </body> </html>
循环,判断
<?php require './smarty/Smarty.class.php'; $sm = new Smarty; $sm->setTemplateDir("./dir");//设置模板路径 $n = mt_rand(1,9); $arr = array( array('id'=>1,'title'=>'小叶'), array('id'=>2,'title'=>'小红'), array('id'=>3,'title'=>'小刚') ); $sm->assign('n',$n); $sm->assign('arr',$arr); $sm->display('3.html');
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> {foreach $arr as $k=>$v} {$v['id'] + 2} {$v['title']} {/foreach} <h1> {if $n == 5}您好啊5 {else if $n == 4}您好啊4 {else}您好啊other {/if} </h1> </body> </html>