2014-04-17 114 views
0

我有這樣的Smarty代碼:的Smarty的foreach白頁

{foreach from=$chats item=chat} 
    {$chat['id']} 
{/foreach} 

當我嘗試執行此,我得到一個白色的頁面。

我檢查,如果PHP代碼通過一個合法的陣列

{$chats[0]['id']} 

而這個工作。

我想Smarty應該輸出一個錯誤,如果有什麼問題,所以我檢查了我的Smarty設置,但他們似乎沒關係?

<?php 
require('smarty/Smarty.class.php'); 
global $smarty; 
error_reporting(E_ALL); 
ini_set('display_errors', '1'); 
$smarty = new Smarty(); 
$smarty->error_reporting = E_ALL & ~E_NOTICE; 
$smarty->debugging = true; 
$smarty->template_dir = "templates"; 
$smarty->compile_dir = "templates_c"; 
$smarty->cache_dir = "cache"; 
$smarty->config_dir = "configs"; 
?> 

我檢查了我的Smarty安裝:

Smarty Installation test... 
Testing template directory... 
\templates is OK. 
Testing compile directory... 
\templates_c is OK. 
Testing plugins directory... 
\plugins is OK. 
Testing cache directory... 
\cache is OK. 
Testing configs directory... 
\configs is OK. 
Testing sysplugin files... 
... OK 
Testing plugin files... 
... OK 
Tests complete. 

沒有foreach語句,一切正常。

更新

我有Smarty的版本3.1.13,

我試過了這個例子的代碼,從Smarty的文檔(http://www.smarty.net/docs/en/language.function.foreach.tpl):

<?php 
require("smarty.php"); 
$arr = array('red', 'green', 'blue'); 
$smarty->assign('myColors', $arr); 
$smarty->display('foreach.tpl'); 
?> 

<ul> 
{foreach $myColors as $color} 
    <li>{$color}</li> 
{/foreach} 
</ul> 

但即使有這樣的代碼全部我得到的是一個白頁

+0

沒有'foreach'頁面工作嗎?我的意思是'foreach'上的問題?你可以試試'{$ chat.id}' – waki

回答

1

你應該查看文檔:http://www.smarty.net/docs/en/language.function.foreach.tpl

您必須在變量之前放置$。另外,item=語法在Smarty3中已棄用,我假定您使用的是因爲在您的發佈中沒有提及過時的Smarty2。

{foreach $chats as $chat} 
    {$chat['id']} 
{/foreach} 

如果你還在使用SMARTY2反正 - 你應該嘗試用{$chat.id}語法來訪問,而不是使用括號您的數據。 (另請參閱文檔:http://www.smarty.net/docsv2/en/language.function.foreach.tpl

{foreach from=$chats item=chat} 
    {$chat.id} 
{/foreach}