Smarty: Access to array element using a variable?

Have a question or a suggestion about a 3rd party addon module or plugin?
Let us know here.
Locked
faglork

Smarty: Access to array element using a variable?

Post 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
vilkis

Re: Smarty: Access to array element using a variable?

Post by vilkis »

This should work:

Code: Select all

{assign var=eventlist value=$xml->veranstaltung}
{assign var=element value=$eventlist.$event}
{$element->datum}
vilkis
faglork

Re: Smarty: Access to array element using a variable?

Post by faglork »

It doesn't :-(

Cheers,
Alex
vilkis

Re: Smarty: Access to array element using a variable?

Post 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
vilkis

Re: Smarty: Access to array element using a variable?

Post by vilkis »

Both codes should work. Check if the value of $event is 1.
vilkis
faglork

Re: Smarty: Access to array element using a variable?

Post 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
vilkis

Re: Smarty: Access to array element using a variable?

Post by vilkis »

if

Code: Select all

{$event|count_characters:true}
outputs 1, then I have no idea why my suggestions do not work for you.

vilkis
faglork

Re: Smarty: Access to array element using a variable?

Post by faglork »

vilkis wrote:if

Code: Select all

{$event|count_characters:true}
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
maranc
Forum Members
Forum Members
Posts: 249
Joined: Tue May 04, 2010 5:04 pm

Re: Smarty: Access to array element using a variable?

Post by maranc »

What you have when {$eventlist|@print_r}?

Marek A.
Locked

Return to “Modules/Add-Ons”