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

Pass 3-dim Array to Template

 
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 -> General
View previous topic :: View next topic  
Author Message
luftikus143
Smarty Rookie


Joined: 14 Apr 2008
Posts: 8

PostPosted: Fri Dec 19, 2008 1:11 pm    Post subject: Pass 3-dim Array to Template Reply with quote

Hi there,

I have elements retrieved from a database like this:

Short Name 1,Name 1, Link 1, Short Name 2,Name 2, Link 2, Short Name 3,Name 3, Link 3

And that should be split up into something like

<a href="Link1" title="Name 1">Short Name 1</a>
<a href="Link2" title="Name 2">Short Name 2</a>
....

I can do this in the PHP part of the file, but I would prefer - I think this would be more "correct" - in the Template file.

But with a foreach(...), I only know of using this for a 2-dim array, not 3 as in my case.

Can anyone give me a hint how this would work?

Thanks a lot!


____________________________________________________________________

Lean Back and Relax - Enjoy some Nature Photography
http://photoblog.la-famille-schwarzer.de

Appetite for Global Data? UNEP GEO Data Portal:
http://geodata.grid.unep.ch
____________________________________________________________________
Back to top
View user's profile Send private message
Celeb
Administrator


Joined: 17 Apr 2007
Posts: 1025
Location: Vienna

PostPosted: Mon Dec 29, 2008 8:47 am    Post subject: Reply with quote

A 2-dimensional associative array suffices:
Code:
array(
  array(
    'name'  => '...',
    'short' => '...',
    'link'  => '...'),
  array(
    'name'  => '...',
    'short' => '...',
    'link'  => '...'),
  array(
    'name'  => '...',
    'short' => '...',
    'link'  => '...')
);

Code:
{foreach from=$myArray item=foo}
  <a href="{$foo.link}" title="{$foo.name}">{$foo.short}</a>
{/foreach}

_________________
Darn computers always do what I tell them to instead of what I want them to do.
Back to top
View user's profile Send private message
luftikus143
Smarty Rookie


Joined: 14 Apr 2008
Posts: 8

PostPosted: Mon Dec 29, 2008 10:40 am    Post subject: Reply with quote

Thanks a lot.

Just for completion, here is how it worked for me:

Code:
{foreach from=$top_links key=k item=foo}
  <li style="width: {$top_links_width}%"><a class="linksWhite" href="{$foo.url}" target="_blank" title="{$foo.name}">{$foo.short}</a></li>
{/foreach}

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 -> General 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