|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
Budda Smarty Regular
Joined: 19 Apr 2003 Posts: 53 Location: Lymm, Cheshire. UK
|
Posted: Thu Jun 05, 2003 9:45 am Post subject: Indexing an array within a Smarty template |
|
|
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 |
|
sweatje Smarty Regular
Joined: 17 Apr 2003 Posts: 70 Location: Bettendorf, Iowa, USA
|
Posted: Thu Jun 05, 2003 11:00 am Post subject: |
|
|
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:
_________________ Jason
jsweat_php AT yahoo DOT com |
|
Back to top |
|
Budda Smarty Regular
Joined: 19 Apr 2003 Posts: 53 Location: Lymm, Cheshire. UK
|
Posted: Thu Jun 05, 2003 11:30 am Post subject: |
|
|
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 |
|
sweatje Smarty Regular
Joined: 17 Apr 2003 Posts: 70 Location: Bettendorf, Iowa, USA
|
Posted: Thu Jun 05, 2003 12:24 pm Post subject: |
|
|
[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 |
|
Budda Smarty Regular
Joined: 19 Apr 2003 Posts: 53 Location: Lymm, Cheshire. UK
|
Posted: Thu Jun 05, 2003 2:14 pm Post subject: |
|
|
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 |
|
sweatje Smarty Regular
Joined: 17 Apr 2003 Posts: 70 Location: Bettendorf, Iowa, USA
|
Posted: Thu Jun 05, 2003 3:04 pm Post subject: |
|
|
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 |
|
|
|
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
|