2008年5月17日 星期六

smarty


  1. 就是 foreach 要以巢狀處理的方式來呈現我們所 assign 的兩層陣列變數




  2. section 則以「主陣列[迴圈名稱].子陣列索引」即可將整個陣列呈現出來

    Smarty 在樣版中的 foreach 和 PHP 中的 foreach 是一樣的




  3. .丟給 section 的陣列索引必須是從 0 開始的正整數,即 0, 1, 2, 3, ...。如果您的陣列索引不是從 0 開始的正整數,那麼就得改用foreach 來呈現您的資料。

    (section無法列舉以字串作索引的陣列)




  4. 重點在於 $smarty.section.sec1.rownum 及 $smarty.section.sec1.total 這兩個 Smarty 變數,在 section 迴圈中這個 rownum 變數會取得從 1 開始的 索引值




  5. 而 total 這個變數會回傳資料總筆數




  6. include 中可以用「變數名稱=變數內容」來指定引含進來的樣版(子樣版)中所包含的變數,變數為全域。

    $tpl->assign("dyn_page", "test5_3.htm");

    {include file=$dyn_page}

    <p>{$custom_var}</p>

    {include file="test5_2.htm"}

    {include file=$dyn_page}

    {include file="test5_4.htm" custom_var="自訂變數的內容"}




  7. $a is even(N) by N,用在迴圈時,執行N次,true false轉換




  8. foreach

    {foreach from=$fruits item=aa key=bb name=cc}



    {if $smarty.foreach.cc.first}

    <ul>

    {/if}



    <li>第{$smarty.foreach.cc.iteration}種{$bb}{$aa}</li>



    {if $smarty.foreach.cc.last}

    </ul>

    {/if}



    {foreachelse}

    不存在

    {/foreach}



    {if $smarty.foreach.cc.show}

    共{$smarty.foreach.cc.total}種

    {/if}




  9. section

    loop:陣列或整數



    {section name=abc loop=$fruits start=0 step=1 max=50 show=true}



    {if $smarty.section.abc.first==true}

    <ul>

    {/if}



    <li>

    {$fruits[abc]},品種 {$class[abc]},

    index:{$smarty.section.abc.index},

    index_prev:{$smarty.section.abc.index_prev},

    index_next:{$smarty.section.abc.index_next},

    iteration:{$smarty.section.abc.iteration},

    rownum:{$smarty.section.abc.rownum}

    </li>



    {if $smarty.section.abc.last==true}

    </ul>

    {/if}



    {sectionelse}

    不存在

    {/section}



    {if $smarty.section.abc.show}

    total:{$smarty.section.abc.total}

    <br>

    loop:{$smarty.section.abc.loop}

    {/if}




  10. .巢狀迴圈

    section中,用 $陣列名稱[設定的name] ,即可取得所有陣列元素

    <pre>

    {section name=aa loop=$chapters}

    第{$smarty.section.aa.iteration}章 {$chapters[aa].name}



    {section name=bb loop=$chapters[aa].sections}

    {$smarty.section.aa.iteration}-{$smarty.section.bb.iteration} {$chapters[aa].sections[bb]}

    {/section}



    {/section}

    </pre>



  11. {literal}

    {/literal}



    $smarty->left_delimiter="<{";

    $smarty->right_delimiter="}>";




  12. $smarty->debugging=true;



    {debug}




  13. date_default_timezone_set('Asia/Taipei');

    $smarty->caching = true;或1

    $smarty->cache_lifetime=5;

    ################################

    主子樣板生命週期不同

    $smarty->caching = 2;



    $smarty->cache_lifetime = 10;//放在fetch之前

    $sub_page = $smarty->fetch('ch10/04_sub.tpl.htm');//10秒



    $smarty->cache_lifetime = 5;

    $smarty->assign('sub_page', $sub_page);



    $smarty->display('ch10/04.tpl.htm');



    if (!$smarty->is_cached('ch10/07.tpl.htm'))

    {

    $smarty->assign('start_time', date("Y-m-d H:i:s"));



    //做一些複雜且耗時的工作

    do_complex_work();

    $smarty->assign('end_time', date("Y-m-d H:i:s"));

    }



    $smarty->display('ch10/07.tpl.htm');

    #################################

    快取編號

    if (!$smarty->is_cached('ch10/08.tpl.htm', $num))

    #################################

    清除全部快取

    $smarty->clear_all_cache();

    清除特定頁面快取

    $smarty->clear_cache('ch10/01.tpl.htm');

    清除特定頁面特定編號快取

    $smarty->clear_cache('ch10/08.tpl.htm', $num);


  14. plugins

    array_push($this->plugins_dir,APP_REAL_PATH . "/plugins2/");

沒有留言:

張貼留言