Smarty Foreach Loop

travelvice
travelvice's picture

Joined: 2005-07-16
Posts: 112
Posted: Sun, 2008-01-06 00:01

Howdy,

I've ripped this lovely piece of code out of the breadcrumb template and am using it in a custom theme I'm working on.

Problem is, I want to modify it to show the top-most parent album the user is currently in, on the level below the root.

I'm use to using i++ type counts in foreach loops to help me select the depth of the loop I want -- perhaps someone can think of an alternate solution, as I'm not sure how to go about doing this with Smarty

This code displays the album name with hyperlink of the level above the current view:


  {foreach name=parent from=$theme.parents item=parent}
      {if $smarty.foreach.parent.last}
        <a href="{g->url params=$parent.urlParams}"> view images from {$parent.title|markup:strip|default:$parent.pathComponent}</a>
      {/if}
  {/foreach}

 
floridave
floridave's picture

Joined: 2003-12-22
Posts: 23886
Posted: Sun, 2008-01-06 00:51
Quote:
show the top-most parent album the user is currently in, on the level below the root.

I don't understand.
What if the user is on the root album-> What should it show?
What if the user is one the level below the root-> What should it show?
What is the user is 4 levels deep what level-> What should it show?

Dave
_____________________________________________
Blog & G2 || floridave - Gallery Team

 
travelvice
travelvice's picture

Joined: 2005-07-16
Posts: 112
Posted: Sun, 2008-01-06 01:06

Hi there,

Quote:
What if the user is on the root album-> What should it show?

Nothing -- either a different template for the root will be used or this will be done via code

Quote:
What if the user is one the level below the root-> What should it show?
What is the user is 4 levels deep what level-> What should it show?

It would still show the topmost level of the tree, below the root

I'm using my Gallery for countries and cities. When a person is on the level below the root, or 4 levels deep, I'd still like to reference the name of the country they are in. It doesn't have to include the hyperlink to the album, just the album name will do. :)

 
floridave
floridave's picture

Joined: 2003-12-22
Posts: 23886
Posted: Sun, 2008-01-06 03:11

Still a bit confused but; I think this is what you are after:

  {foreach name=parent from=$theme.parents item=parent}
  {if $smarty.foreach.parent.iteration ==2}
  <a href="{g->url params=$parent.urlParams}">{$parent.title|markup:strip|default:$parent.pathComponent}</a>
  link to first child from parent
  {/if}
  {/foreach}

Dave
_____________________________________________
Blog & G2 || floridave - Gallery Team

 
travelvice
travelvice's picture

Joined: 2005-07-16
Posts: 112
Posted: Sun, 2008-01-06 03:26
Quote:
{if $smarty.foreach.parent.iteration ==2}

This is exactly the Smarty syntax I was looking for -- works perfectly on all albums lower than the first under the root.

..that is to say:

ROOT
 *album 1
  -album 2    <--- works starting at this level
   #album 3

I still need the name of the album at the "album 1" level -- thoughts on how that can be captured in conjunction with the current ==2 functionality?

 
travelvice
travelvice's picture

Joined: 2005-07-16
Posts: 112
Posted: Sun, 2008-01-06 19:56

I've got a good working concept of this now, but could use the code snippet that displays the current album the visitor is in.

Thanks for the brain power!

 
floridave
floridave's picture

Joined: 2003-12-22
Posts: 23886
Posted: Sun, 2008-01-06 20:28

Enable FAQ: How to set/use Gallery in debug mode? and in the popup you will see all the smarty variable available for the page you are on.
A smarty debug console will popup showing all the data available to that page (make sure your browser allows popups, at least for your server's domain). To access this data connect the names at each level of indentation with dots.. some examples with some modifiers added:
{$theme.item.title}
{$theme.parent.title|markup:strip} {$theme.item.title|default:$theme.item.pathComponent|markup:strip} {$user.username} {$theme.item.height) {$theme.item.width}

Dave
_____________________________________________
Blog & G2 || floridave - Gallery Team

 
travelvice
travelvice's picture

Joined: 2005-07-16
Posts: 112
Posted: Sun, 2008-01-06 23:46

That looks great Dave, thank you.

Reference for others, the (reduced) code block looks like this:

{foreach name=parent from=$theme.parents item=parent}
   {if $smarty.foreach.parent.total ==1}
   {* DISPLAY CURRENT ALBUM TITLE *}
    {$theme.item.title|default:$theme.item.pathComponent|markup:strip}
   {elseif $smarty.foreach.parent.iteration ==2}
    {* DISPLAY FIRST CHILD AFTER ROOT OF CURRENT ALBUM TREE *}
    {$parent.title|markup:strip|default:$parent.pathComponent}
  {/if}
{/foreach}
 
floridave
floridave's picture

Joined: 2003-12-22
Posts: 23886
Posted: Mon, 2008-01-07 02:55

Glad you got it sorted and thanks for posting back code for others.

Dave
_____________________________________________
Blog & G2 || floridave - Gallery Team