Kaj
Kaj

Reputation: 2503

Smarty foreach white page

I have this Smarty code:

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

When I try to execute this, I get a white page.

I checked if the PHP code passed a legit array with

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

And this works.

I figured Smarty should output an error if there is something wrong, so I checked my Smarty settings but they seem okay?

<?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";
?>

I checked my Smarty installation:

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.

Without the foreach statement, everything works.

Update

I have Smarty version 3.1.13,

I tried this example code, from the Smarty documentation (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>

But even with this code all I get is a white page

Upvotes: 0

Views: 165

Answers (1)

Vestalis
Vestalis

Reputation: 270

You should check out the documentation: http://www.smarty.net/docs/en/language.function.foreach.tpl

You have to place a $ before your variable. Also the item= syntax is deprecated in Smarty3, which i assume you are using as there is no reference to the outdated Smarty2 in your posting.

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

If you are still using Smarty2 anyways - You should try to access your data with {$chat.id} syntax, instead of using brackets. (Also featured in docs: http://www.smarty.net/docsv2/en/language.function.foreach.tpl)

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

Upvotes: 1

Related Questions