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

Using assigned variables as array indexes within templates

 
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 -> Feature Requests
View previous topic :: View next topic  
Author Message
Exar
Smarty n00b


Joined: 29 Oct 2004
Posts: 3
Location: Sheffield, UK

PostPosted: Fri Oct 29, 2004 3:25 pm    Post subject: Using assigned variables as array indexes within templates Reply with quote

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 Smile

Thanks in advance for any feedback.
Back to top
View user's profile Send private message
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Fri Oct 29, 2004 3:48 pm    Post subject: Reply with quote

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
View user's profile Send private message
Exar
Smarty n00b


Joined: 29 Oct 2004
Posts: 3
Location: Sheffield, UK

PostPosted: Fri Oct 29, 2004 4:19 pm    Post subject: Reply with quote

Yeah, good idea - the lookup modifier seems the way to go.

Thanks for replying so quickly.
Back to top
View user's profile Send private message
Exar
Smarty n00b


Joined: 29 Oct 2004
Posts: 3
Location: Sheffield, UK

PostPosted: Mon Nov 01, 2004 9:20 am    Post subject: Reply with quote

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 Smile
Back to top
View user's profile Send private message
upsfeup
Smarty n00b


Joined: 05 Jan 2005
Posts: 3

PostPosted: Wed Jan 05, 2005 2:05 am    Post subject: Reply with quote

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 Smile


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! Very Happy
Back to top
View user's profile Send private message
CNE
Smarty n00b


Joined: 14 Mar 2005
Posts: 2

PostPosted: Fri Jun 24, 2005 10:35 pm    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website AIM Address MSN Messenger
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Fri Jun 24, 2005 11:15 pm    Post subject: Reply with quote

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
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 -> Feature Requests 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