:::
5-3 顯示所有文章摘要
一、傳送變數至樣板
- PHP可以分別傳送單一變數到樣板檔,例如:
$smarty->assign('title', '文章標題'); $smarty->assign('author', '作者資訊'); $smarty->assign('content', '文章內容'); - 然後,樣板檔只要用
{$變數}便能呈現該變數:標題:{$title} 作者:{$author} 內容:{$content}
二、傳送陣列至樣板
- 一篇文章有很多欄位,若每個欄位都要
assign一次,那會很麻煩,所以,可以用陣列方式來送到樣板,例如:$news = [ 'title'=>'標題', 'author'=>'資訊', 'content'=>'內容', ]; $smarty->assign('news', $news); - 接收的時候,用
{$陣列.索引}即可:標題:{$news.title} 作者:{$news.author} 內容:{$news.content}
三、傳送二維陣列至樣板
- 首頁不只一篇文章,每篇文章又有很多欄位,那就用二維陣列來傳,例如:
$news[1] = [ 'title'=>'標題1', 'author'=>'資訊1', 'content'=>'內容1', ]; $news[2] = [ 'title'=>'標題2', 'author'=>'資訊2', 'content'=>'內容2', ]; $smarty->assign('all_news', $news); - 接收的時候,先跑
foreach()迴圈,依序取出第N篇文章資料,再用{$陣列.索引}即可:{foreach $all_news as $news} 標題:{$news.title} 作者:{$news.author} 內容:{$news.content} {/foreach}
5-2 將主樣板檔拆分成小樣板