View previous topic :: View next topic |
Author |
Message |
Exar Smarty n00b
Joined: 29 Oct 2004 Posts: 3 Location: Sheffield, UK
|
Posted: Fri Oct 29, 2004 3:25 pm Post subject: Using assigned variables as array indexes within templates |
|
|
Here's what I'm trying to do (hopefully I'm not just thinking about this all wrong):
I have a PHP object with a var called 'status', which can have one of a few values such as 'ACT', 'INACT', 'SUS''. I then have an array which maps these values to some meaningful text (see code below).
Within the template, I wanted to be able to use the value of the 'status' var as an array key to decide what text to display:
[php:1:2df18633cf]
$thing = new Thing(); // $thing has a var called 'status'
$status_mappings = array(
"ACT" => "Active",
"INACT" => "Inactive",
"SUS" => "Suspended"
);
$smarty->assign('thing', $thing);
$smarty->assign('status_map', $status_mappings);
$smarty->display('blah.tpl');
[/php:1:2df18633cf]
Then in the template:
Code: |
{* blah.tpl *}
<p>Status: {$status_map.$thing->status}</p>
|
I'm aware that the above template code isn't valid, but hopefully it's obvious what I'm trying to accomplish. I also tried putting the array index in separate braces, eg:
Code: |
<p>Status: {$status_map.{$thing->status}}</p>
|
...but no joy here either. Is this just something I should be doing a different way, or is it something that might be worked into future versions of smarty by way of some cunning escaping characters?
Basically I'm trying to avoid having to put the mapped value of the 'status_mappings' array in as a separate assign() call (because I could end up with hundreds of these if I have lots of separate arrays to use). I'm also trying to avoid setting the mapped value as a var on the object (because this puts a bit of a dent in the whole 'separate data from presentation' thing
Thanks in advance for any feedback. |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Fri Oct 29, 2004 3:48 pm Post subject: |
|
|
It is actually not very easy to mix arrays and objects.
First of all, this won't work because you can not nest Smarty tags:
Code: | <p>Status: {$status_map.$thing->status}</p> |
What you want is something like {$status_map[$thing->status]}, but that doesn't work either because you can't use object derefs in the bracket syntax (but something like {$status_map[$thing.status]} would have worked if $thing was an array). The typical solution is a temp var in your template:
Code: | {assign var=temp value=$thing->status}
{$status_map.$temp} |
But you can probably ease your life if you write a simple modifier to do lookups like this:
Code: | {$thing->status|lookup:$status_map} |
Something like this, but I didn't bother to test:
plugins/modifier.lookup.php
[php:1:925c11d262]<?php
function smarty_modifier_lookup($value='', $from=array())
{
if (array_key_exists($value, $from)) {
return $from[$value];
}
return '';
}
?>[/php:1:925c11d262] |
|
Back to top |
|
Exar Smarty n00b
Joined: 29 Oct 2004 Posts: 3 Location: Sheffield, UK
|
Posted: Fri Oct 29, 2004 4:19 pm Post subject: |
|
|
Yeah, good idea - the lookup modifier seems the way to go.
Thanks for replying so quickly. |
|
Back to top |
|
Exar Smarty n00b
Joined: 29 Oct 2004 Posts: 3 Location: Sheffield, UK
|
Posted: Mon Nov 01, 2004 9:20 am Post subject: |
|
|
Just as a followup (and to justify this thread's position in 'feature requests') - your solution seems like such a useful one (I'm now using it everywhere) that it should be included in future smarty releases as a standard modifier plugin  |
|
Back to top |
|
upsfeup Smarty n00b
Joined: 05 Jan 2005 Posts: 3
|
Posted: Wed Jan 05, 2005 2:05 am Post subject: |
|
|
Exar wrote: | Just as a followup (and to justify this thread's position in 'feature requests') - your solution seems like such a useful one (I'm now using it everywhere) that it should be included in future smarty releases as a standard modifier plugin  |
Agreed!
I tried several stuff including
Code: |
{assign var=temp value=$thing[j]->status}
{$status_map.$temp}
|
But assign could only return $thing[j] not ->status variable.
But this function worked like a charm! Congrats and thank you!  |
|
Back to top |
|
CNE Smarty n00b
Joined: 14 Mar 2005 Posts: 2
|
Posted: Fri Jun 24, 2005 10:35 pm Post subject: |
|
|
Please split off if this is OT...
I made a class that generates some pagination data to be assigned directly to smarty. I thought it was very clever until I tried to do the following.
[php:1:4b60f2d26f]//php example...
$class->pageArray[$class->previousPageIndexValue][URL] = "filename.php?start=10&...;
[/php:1:4b60f2d26f]
Of course, this doesn't work in Smarty:
Code: | $class->pageArray.$class->previousPageIndexValue.URL |
Because Smarty thinks that the URL key is part of previousPageIndexValue, and not pageArray. The "assign $class->previousPageIndexValue to a temp smarty var" method fixes the problem, but we shouldn't have to add all these temporary vars just to process arrays & classes.
Why couldn't there be a way to "evaluate" variable values within array dimensions? Couldn't we use some sort of token to seperate class/array associations from the rest of the array/class expression? For example:
Code: | $class->pageArray.`$class->previousPageIndexValue`.URL |
|
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Fri Jun 24, 2005 11:15 pm Post subject: |
|
|
I don't think it is such a bad idea but I wonder if Smarty really needs new syntax, especially when it is to compress a complicated expression into a single line. You can easily do an intermediate assign and get the benefit of clearer meaning code. One problem is that people would certainly then want to embed `` inside of other `` expressions. What we really need are parens but arbitrary levels of nested parens are a classic example of something that can't be parsed by a regex engine like Smarty.
I think that the moral is: doing everything in one step is not always the best option in Smarty. Try to do everything you can from the application side so that you can pass in simplified structures to your template. Failing that, provide some custom plugins to help abstract obtuse expressions or constructs in your templates. Make your template users life easier instead of looking for ways to extend the syntax.
Just my 2c! |
|
Back to top |
|
|