assign()和$smarty-這是鄭曉技術(shù)博客的原創(chuàng)博文"/ >
當前位置:博客首頁>>PHP >> 閱讀正文

學(xué)習(xí)smarty第一天

作者: 鄭曉 分類: PHP 發(fā)布于: 2011-11-15 23:22 瀏覽:3,653 沒有評論


從今天開始學(xué)習(xí)smarty模板技術(shù)了,其實應(yīng)該是第二天了,昨天晚上學(xué)習(xí)smarty的基本配置。再其實,以前學(xué)過,都忘了……….

今天主要學(xué)習(xí)的是smarty中$smarty->assign()和$smarty->display()兩個函數(shù)的簡單使用,以及簡單的{section}和{foreach}循環(huán)。
smarty簡單配置文件:

config_dir = 'smarty/config_file.class.php';
$smarty->caching = false;
$smarty->cache_dir = './cache';
$smarty->cache_lifetime = 0;
$smarty->template_dir = './tpl';
$smarty->complate_dir = './templates_c';
$smarty->left_delimiter = '{';
$smarty->right_delimiter = '}';
?>

index.php:

'新聞第一條','date'=>'2011-11-15');
$new[] = array('new_name'=>'新聞第二條','date'=>'2011-11-15');
$new[] = array('new_name'=>'新聞第三條','date'=>'2011-11-15');
$new[] = array('new_name'=>'新聞第四條','date'=>'2011-11-15');
$new[] = array('new_name'=>'新聞第五條','date'=>'2011-11-15');

$smarty->assign('new',$new);
$smarty->display('index.htm');
?>

index模板文件:

{foreach from=$new key=k item=v}

  • {$k} - {$v.new_name}
  • {/foreach}


    foreach中,from是要循環(huán)的數(shù)組變量,item是循環(huán)時當前的變量值,key是循環(huán)時當前的鍵值,name只是一個標記。換成php中,應(yīng)該可以這樣表示吧?foreach( from as key =>item){}

    ? ? ? ?

    本文采用知識共享署名-非商業(yè)性使用 3.0 中國大陸許可協(xié)議進行許可,轉(zhuǎn)載時請注明出處及相應(yīng)鏈接。

    本文永久鏈接: http://www.yjfs.org.cn/the-first-day-of-learning-smarty.html

    發(fā)表評論

    change vcode