簡體   English   中英

如何在smarty中使用{section}打印關聯數組?

[英]How to use {section} in smarty to print an associative array?

我想使用section打印一個關聯數組,但是我不能這樣做。以下是PHP和Smarty代碼:

PHP代碼:

  $smarty->assign('$all_latest_news',       $grid_data);

在上面的代碼中, $grid_data是分配給smarty模板的關聯數組。 供您參考,使用print_r($grid_data)打印后,數組如下所示:

Array ( [0] => Array ( [news_id] => 1 [news_title] => News Channel URL [news_link] => http://indiatoday.intoday.in/ [news_added_date] => 2013-02-26 ) [1] => Array ( [news_id] => 2 [news_title] => News Paper Web Address [news_link] => http://www.bhaskar.com/ [news_added_date] => 2013-02-25 ) )

現在,我想在smarty模板中打印此數組,其代碼片段如下:

<table width="100%" align="center" cellpadding="6" cellspacing="1" bgcolor="#FFFFFF">
                <tr align="center" id="tableHeader">
                    <td width="20%" align="left"><b>News Title</b></td>
                    <td width="55%" align="left"><b>News Link</b></td>
                    <td width="15%" align="center"><b>Option</b></td>
                </tr>
                {section name=news loop=$all_latest_news}
                <tr align="center" id="tabledata" bgcolor="{cycle values="#d0e8f4,#96c0d5"}">
                    <td align="left" valign="top">{$all_latest_news[news].news_title}</td>
                    <td align="left" valign="top">{$all_latest_news[news].news_link|truncate:350:" ...":true}</td>

                    <td align="center" valign="top"><a href="manage_latest_news.php?op=edit&news_id={$all_latest_news[news].news_id}">Edit</a>
                    &nbsp;&nbsp;<a href="manage_latest_news.php?op=delete&news_id={$all_latest_news[news].news_id}" onClick="return ConfirmDelete()">Delete</a></td>
                </tr>
                {sectionelse}
                <tr>
                    <td colspan="5" align="center"><b>No News Available</b></td>
                </tr>
                {/section}
</table>

使用此代碼,我無法將數組值打印到相應的列。 列標題正確打印。 之后,將執行{sectionelse}部分,並顯示消息“ No News Available”。

實際上我想在數組中打印值。 誰能幫我解決這個問題? 提前致謝。

當您將值分配給Smarty中的變量時,您不想放置'$'符號。 將您的代碼更改為

$smarty->assign('all_latest_news', $grid_data);

更改$smarty->assign('$all_latest_news', $grid_data);

至 :

$smarty->assign('all_latest_news', $grid_data);

用這個 :

{foreach from=$all_latest_news item=news name=news}
   {$news.news_title}
{/foreach}

參考: http : //www.smarty.net/docsv2/zh/language.function.foreach

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM