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

Indexing an array within a Smarty template

 
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
Budda
Smarty Regular


Joined: 19 Apr 2003
Posts: 53
Location: Lymm, Cheshire. UK

PostPosted: Thu Jun 05, 2003 9:45 am    Post subject: Indexing an array within a Smarty template Reply with quote

Please forgive me if this simplish question has already been asked, I couldn't find anything after a quick search.

I have an array containing a number of strings, indexed by a number. Its a basic array
Code:
Array("Woman", "Man");
called $sexText;

I then have another Smarty variable which is an integer {$sex} ranging from 0 to 1 in value.

How on earth do I use the Smarty integer variable as an index to the Smarty array?

The following code fails in Smarty...
Code:
You are a {$sexText[$sex]}
Back to top
View user's profile Send private message Visit poster's website Yahoo Messenger MSN Messenger
sweatje
Smarty Regular


Joined: 17 Apr 2003
Posts: 70
Location: Bettendorf, Iowa, USA

PostPosted: Thu Jun 05, 2003 11:00 am    Post subject: Reply with quote

Are you absolutly sure the index keys and the $sex key you are using are integers? Smarty's syntax for accessing a string based key would be:
Code:
{$sexText.$sex}

_________________
Jason
jsweat_php AT yahoo DOT com
Back to top
View user's profile Send private message
Budda
Smarty Regular


Joined: 19 Apr 2003
Posts: 53
Location: Lymm, Cheshire. UK

PostPosted: Thu Jun 05, 2003 11:30 am    Post subject: Reply with quote

Doing the suggested index referencing gives the Smarty error
Quote:
Warning: Illegal offset type in ...blahblahfilename...


As the array is defined without specific index values (ie its not associative) i would assume PHP creates them as integers starting at 0?
Back to top
View user's profile Send private message Visit poster's website Yahoo Messenger MSN Messenger
sweatje
Smarty Regular


Joined: 17 Apr 2003
Posts: 70
Location: Bettendorf, Iowa, USA

PostPosted: Thu Jun 05, 2003 12:24 pm    Post subject: Reply with quote

[php:1:c5a7951798]require_once('Smarty.class.php');

$sextext = array('Male', 'Female');
$sex = (int)0;
$sex2 = (int)1;

$t = new Smarty;
$t->assign(array(
'sextext' => $sextext
,'sex' => $sex
,'sex2' => $sex2
));
$t->Display('test.tpl');
[/php:1:c5a7951798] with template
Code:
<html>
<head>
<title>test</title>
</head>
<body>
<h1>{$sextext[$sex]}</h1>
<h1>{$sextext[$sex2]}</h1>
</body>
</html>

produces:
Male
Female

as expected for me...
_________________
Jason
jsweat_php AT yahoo DOT com
Back to top
View user's profile Send private message
Budda
Smarty Regular


Joined: 19 Apr 2003
Posts: 53
Location: Lymm, Cheshire. UK

PostPosted: Thu Jun 05, 2003 2:14 pm    Post subject: Reply with quote

This is what I expected to happen when I attempted just that this morning. The only slight twist on the scenario is that the variables $sexText and $sex are part of an object. And I just pass the object to Smarty as a single variable.

Then in Smarty I use {$result->sexText[$result->sex]} - but without success. Is there any reason why the added nesting within an object should cause a problem?[/code]
Back to top
View user's profile Send private message Visit poster's website Yahoo Messenger MSN Messenger
sweatje
Smarty Regular


Joined: 17 Apr 2003
Posts: 70
Location: Bettendorf, Iowa, USA

PostPosted: Thu Jun 05, 2003 3:04 pm    Post subject: Reply with quote

I use objects extensivly in my PHP code, but I tend to avoid them with Smarty. In particular, there are several nuances of exactly when and where they can be used withing a template. http://smarty.php.net/manual/en/advanced.features.php#ADVANCED.FEATURES.OBJECTS is a starting point, but perhaps someone with more object in templates experience can comment on your issue.
_________________
Jason
jsweat_php AT yahoo DOT com
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