Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

superglobals and foreach loops - problem w/ keys

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
liamr
Smarty n00b


Joined: 14 Nov 2003
Posts: 2

PostPosted: Fri Nov 14, 2003 7:16 pm    Post subject: superglobals and foreach loops - problem w/ keys Reply with quote

I'm trying use foreach to iterate through $_POST, but am having problems with the array keys. I'm using the code from the manual's entry on foreach:

{foreach name=outer item=postvar from=$smarty.post}
{foreach key=key item=item from=$postvar}
{$key}: {$item}<br>
{/foreach}
{/foreach}

This returns...

0: personal
0: 1064980800
0: HTML
0: jobs
0: submit

While, from php, a print_r( $_POST ) results in:

Array
(
[log_type] => personal
[month] => 1064980800
[action] => HTML
[log_gid] => jobs
[submit] => submit
)

Smarty returns the right value if I refer to the complete name of the variable.. eg,

{$smarty.post.log_type}

will display "personal"

Is there something I need to do specific to the super globals?
I'm using Smarty 2.5.0 w/ PHP 4.3.3. I tried 2.6.0RC3, but couldn't get it to work at all ([Fri Nov 14 13:41:41 2003] [error] PHP Fatal error: Cannot instantiate non-existent class: smarty_compiler in /usr/lib/php/smarty-2.6.0rc3/Smarty.class.php on line 1506).

thx
Liam
Back to top
View user's profile Send private message
messju
Administrator


Joined: 16 Apr 2003
Posts: 3336
Location: Oldenburg, Germany

PostPosted: Fri Nov 14, 2003 7:41 pm    Post subject: Reply with quote

1. you have to say {foreach ... key=key} to make $key being defined on each iteration. you can say key=foo to use $foo, of course.

2. that's odd. 2.6.0-RC3 loads the compiler just the same way as 2.5.0 did. be sure SMARTY_DIR is set correctly. did you clear your templates_c after upgrade?
Back to top
View user's profile Send private message Send e-mail Visit poster's website
liamr
Smarty n00b


Joined: 14 Nov 2003
Posts: 2

PostPosted: Fri Nov 14, 2003 8:23 pm    Post subject: Reply with quote

1. you have to say {foreach ... key=key} to make $key being defined on each iteration. you can say key=foo to use $foo, of course.

D'oh. I didn't look at the example closely enough. I didn't realize that it was an array of arrays.

Reducing it to one foreach loop worked as you said it would.

2. that's odd. 2.6.0-RC3 loads the compiler just the same way as 2.5.0 did. be sure SMARTY_DIR is set correctly. did you clear your templates_c after upgrade?

Prolly a discussion best in a different thread, but..
Smarty's in my include path, and I've got a central smarty setup file I'm using in my php..

<?php

require('smarty/Smarty.class.php');

class Smarty_Template extends Smarty {

function Smarty_Template() {

$this->Smarty();

$this->template_dir = '/usr/local/projects/flume/smarty/templates/';
$this->compile_dir = '/usr/local/projects/flume/smarty/templates_c/';
$this->config_dir = '/usr/local/projects/flume/smarty/configs/';
$this->cache_dir = '/usr/local/projects/flume/smarty/cache/';
}
}

?>

I tried replacing the "require('smarty-2.60rc3/Smarty.class.php');" (which depends on include_path) with..

define('SMARTY_DIR','/usr/local/lib/php/smarty-2.60rc3/');
require(SMARTY_DIR.'Smarty.class.php');

And... the error message shows that it's /finding/ Smarty.class.php.
I did empty templates_c, and the php accelerator's cache directory, and restarted apache..
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP