Smarty Forum Index Smarty
The discussions here are for Smarty, a template engine for the PHP programming language.
accessing associative arrays

 
Post new topic   Reply to topic    Smarty Forum Index -> Feature Requests
View previous topic :: View next topic  
Author Message
rtaranu
Smarty n00b


Joined: 12 Jan 2007
Posts: 2

PostPosted: Fri Jan 12, 2007 3:06 pm    Post subject: accessing associative arrays Reply with quote

It is very cumbersome to access values for array keys that do not follow php variable naming conventions.

Example

in php:
$array = array('foo-bar' => 'hello, world');
$smarty->assign('array', $array);

in template:
{$array.foo-bar} will not work

workaround:
{assign var='foo_bar' value='foo-bar'}
{$vars.$foo_bar}

This workaround is very cumbersome. It would be nice to have a natural way to access values with non php-named keys.

I would like this feature because sometimes the array keys are not chosen by me, they are populated with xml files, or any other number of sources that have other naming conventions than php.
Back to top
View user's profile Send private message
Hielke Hoeve
Smarty Elite


Joined: 06 Jan 2006
Posts: 406
Location: Netherlands

PostPosted: Fri Jan 12, 2007 6:20 pm    Post subject: Re: accessing associative arrays Reply with quote

rtaranu wrote:
in template:
{$array.foo-bar} will not work


how about {$array.`foo-bar`} ? i never use special chars in a var name, then u get these probs. an _ should work better.
_________________
Debug XHTML Compliance
SmartyPaginate
Smarty License Questions
---
(About Unix) The learning curve is full of aha! moments, such as that glorious day that the full beauty of grep and, later, find is revealed in all its majesty. --- Robert Uhl <ruhl@4dv.net>
Back to top
View user's profile Send private message
rtaranu
Smarty n00b


Joined: 12 Jan 2007
Posts: 2

PostPosted: Fri Jan 12, 2007 7:44 pm    Post subject: Re: accessing associative arrays Reply with quote

Hielke Hoeve wrote:

how about {$array.`foo-bar`} ? i never use special chars in a var name, then u get these probs. an _ should work better.


{$array.`foo-bar`} does not work
{$array.'foo-bar'} does not work

The only workaround I've found is the posted above.

I never use special chars in variable names either. I populate an array with the values found in an xml file, the user is supposed to use a web page to modify the values in the xml-file.

These names come from the xml file, where the author decided to use a bastard version of underscore notation.
Back to top
View user's profile Send private message
Hielke Hoeve
Smarty Elite


Joined: 06 Jan 2006
Posts: 406
Location: Netherlands

PostPosted: Sat Jan 13, 2007 2:13 pm    Post subject: Reply with quote

regex the - to a _, this can be done in the xml parsing or afterwards.
_________________
Debug XHTML Compliance
SmartyPaginate
Smarty License Questions
---
(About Unix) The learning curve is full of aha! moments, such as that glorious day that the full beauty of grep and, later, find is revealed in all its majesty. --- Robert Uhl <ruhl@4dv.net>
Back to top
View user's profile Send private message
shoulders
Smarty n00b


Joined: 05 May 2010
Posts: 3

PostPosted: Mon Jul 08, 2013 3:07 pm    Post subject: might be of use Reply with quote

i dont know if this is the same thing but the following worked for me.

http://www.phpeveryday.com/articles/Smarty-Variable-Object-P613.html

Code:
{$foo->id}


This returns the value that is stored for 'id'

This works when the $foo variable is a 'Smarty_Variable Object', inside it is a sinlge level array.

I also found the follwing link useful.

http://www.smarty.net/docs/en/language.syntax.variables.tpl[/url][/code]
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    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