【问题标题】:Smarty foreach white pageSmarty foreach 白页
【发布时间】:2014-04-19 01:38:35
【问题描述】:

我有这个 Smarty 代码:

{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>

但即使有了这段代码,我得到的只是一个白页

【问题讨论】:

  • 没有foreach 页面工作?我的意思是foreach 真的有问题吗?你可以试试吗{$chat.id}

标签: php html smarty


【解决方案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}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-09-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多