Page 1 of 1
Smarty: Access to array element using a variable?
Posted: Wed Jun 13, 2012 11:35 am
by faglork
Hi all!
I need to access a certain element of an array . No probs using a fixed value:
{assign var=eventlist value=$xml->veranstaltung}
{$eventlist[1]->datum}
--> works
but when I use a variable:
{assign var=eventlist value=$xml->veranstaltung}
{$eventlist[$event]->datum}
--> does not work. The value of $event is 1 ...
I searched for a long time, but none of the given solutions work.
In most cases people advise to use
{$eventlist.$event.datum}
but this does not work. So doesn't
{$eventlist.$event->datum}
But there must be a solution, or is this impossible? How do I do this?
Cheers,
Alex
Re: Smarty: Access to array element using a variable?
Posted: Wed Jun 13, 2012 12:10 pm
by vilkis
This should work:
Code: Select all
{assign var=eventlist value=$xml->veranstaltung}
{assign var=element value=$eventlist.$event}
{$element->datum}
vilkis
Re: Smarty: Access to array element using a variable?
Posted: Wed Jun 13, 2012 12:19 pm
by faglork
It doesn't
Cheers,
Alex
Re: Smarty: Access to array element using a variable?
Posted: Wed Jun 13, 2012 12:37 pm
by vilkis
I have no similar smarty object now, so can't test it.
What about:
Code: Select all
{assign var=eventlist value=$xml->veranstaltung}
{assign var=element value=$eventlist[$event]}
{$element->datum}
vilkis
Re: Smarty: Access to array element using a variable?
Posted: Wed Jun 13, 2012 12:49 pm
by vilkis
Both codes should work. Check if the value of $event is 1.
vilkis
Re: Smarty: Access to array element using a variable?
Posted: Wed Jun 13, 2012 1:04 pm
by faglork
here's the code:
Code: Select all
{assign var=eventlist value=$xml->veranstaltung}
{assign var=element value=$eventlist[$event]}
<p>$event : {$event}
<p>$element->datum : {$element->datum}
<p>$eventlist[1]->datum : {$eventlist[1]->datum}
<p>$eventlist[$event]->datum : {$eventlist[$event]->datum}
<p>$eventlist.$event->datum : {$eventlist.$event->datum}
<p>$xml->veranstaltung[1]->datum : {$xml->veranstaltung[1]->datum}
<p>$xml->veranstaltung.$event->datum : {$xml->veranstaltung.$event->datum}
and this is the output:
$event : 1
$element->datum :
$eventlist[1]->datum : 15.06.2012
$eventlist[$event]->datum :
$eventlist.$event->datum :
$xml->veranstaltung[1]->datum : 15.06.2012
$xml->veranstaltung.$event->datum :
It only works with fixed values.
Cheers,
ALex
Re: Smarty: Access to array element using a variable?
Posted: Wed Jun 13, 2012 1:21 pm
by vilkis
if
outputs 1, then I have no idea why my suggestions do not work for you.
vilkis
Re: Smarty: Access to array element using a variable?
Posted: Wed Jun 13, 2012 1:43 pm
by faglork
vilkis wrote:if
outputs 1, then I have no idea why my suggestions do not work for you.
vilkis
<p>$event : {$event} - {$event|count_characters:true}
gives
$event : 1 - 1
Seems that I am out of luck.
Cheers,
Alex
Re: Smarty: Access to array element using a variable?
Posted: Sat Jun 16, 2012 9:37 am
by maranc
What you have when {$eventlist|@print_r}?
Marek A.