|
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 |
rtaranu Smarty n00b
Joined: 12 Jan 2007 Posts: 2
|
Posted: Fri Jan 12, 2007 3:06 pm Post subject: accessing associative arrays |
|
|
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 |
|
Hielke Hoeve Smarty Elite
Joined: 06 Jan 2006 Posts: 406 Location: Netherlands
|
Posted: Fri Jan 12, 2007 6:20 pm Post subject: Re: accessing associative arrays |
|
|
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 |
|
rtaranu Smarty n00b
Joined: 12 Jan 2007 Posts: 2
|
Posted: Fri Jan 12, 2007 7:44 pm Post subject: Re: accessing associative arrays |
|
|
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 |
|
Hielke Hoeve Smarty Elite
Joined: 06 Jan 2006 Posts: 406 Location: Netherlands
|
Posted: Sat Jan 13, 2007 2:13 pm Post subject: |
|
|
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 |
|
shoulders Smarty Rookie
Joined: 05 May 2010 Posts: 11
|
|
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
|