:::

5-3 顯示所有文章摘要

一、傳送變數至樣板

  1. PHP可以分別傳送單一變數到樣板檔,例如:
    $smarty->assign('title', '文章標題');
    $smarty->assign('author', '作者資訊');
    $smarty->assign('content', '文章內容');

     

  2. 然後,樣板檔只要用{$變數}便能呈現該變數:
    標題:{$title}
    作者:{$author}
    內容:{$content}

     

二、傳送陣列至樣板

  1.  一篇文章有很多欄位,若每個欄位都要 assign 一次,那會很麻煩,所以,可以用陣列方式來送到樣板,例如:
    $news = [
      'title'=>'標題', 
      'author'=>'資訊',
      'content'=>'內容',
    ];
    
    $smarty->assign('news', $news);

     

  2. 接收的時候,用 {$陣列.索引} 即可:
    標題:{$news.title}
    作者:{$news.author}
    內容:{$news.content}

     

三、傳送二維陣列至樣板

  1.  首頁不只一篇文章,每篇文章又有很多欄位,那就用二維陣列來傳,例如:
    $news[1] = [
      'title'=>'標題1', 
      'author'=>'資訊1',
      'content'=>'內容1',
    ];
    
    $news[2] = [
      'title'=>'標題2', 
      'author'=>'資訊2',
      'content'=>'內容2',
    ];
    
    
    $smarty->assign('all_news', $news);

     

  2. 接收的時候,先跑 foreach() 迴圈,依序取出第N篇文章資料,再用 {$陣列.索引} 即可:
    {foreach $all_news as $news}
      標題:{$news.title}
      作者:{$news.author}
      內容:{$news.content}
    {/foreach}

     

 


:::

書籍目錄

展開 | 闔起

http%3A%2F%2Fcampus-xoops.tn.edu.tw%2Fmodules%2Ftad_book3%2Fpage.php%3Ftbsn%3D52%26tbdsn%3D1806

計數器

今天: 2066206620662066
昨天: 4745474547454745
總計: 7712027771202777120277712027771202777120277712027