Conditional Template - Smarty and Arrays

The place to talk about things that are related to CMS Made simple, but don't fit anywhere else.
Post Reply
tobre
New Member
New Member
Posts: 5
Joined: Wed May 26, 2010 1:58 pm

Conditional Template - Smarty and Arrays

Post by tobre »

I would like to conditionally hide elements in a template based on whether or not a page-alias is in a predefined array, like so...

Code: Select all

{if (in_array($page_alias,$someArray))}
  show this
{/if}
My problem is: How and where would I best define that array?

Code: Select all

$someArray=array( 'alias1', 'alias2', 'alias3','alias4' );

--- addendum ---
??? How is working with arrays in templates not a valid topic for the development discussions board?
Last edited by tobre on Sun May 30, 2010 9:14 pm, edited 1 time in total.
User avatar
kermit
Power Poster
Power Poster
Posts: 693
Joined: Thu Jan 26, 2006 11:46 am

Re: Conditional Template - Smarty and Arrays

Post by kermit »

the most basic solution would be a udt....

Code: Select all

// assignarray
global $gCms;
$myArray = array("index","do-dads","gizmos","some-other-page");
$smarty->assign('alternatelayout',$myArray);
you could get fancy and pass a parameter containing page aliases to populate the array.. i'll leave that up to you.

then in your template or page data:

Code: Select all

{assignarray}
followed by the smarty logic

Code: Select all

{if in_array($page_alias,$alternatelayout)}
 do this or show that
{else}
 otherwise do this instead
{/if}
to examine the array contents in smarty use

Code: Select all

{$alternatelayout|@print_r}
eternity (n); 1. infinite time, 2. a seemingly long or endless time, 3. the length of time it takes a frozen pizza to cook when you're starving.
4,930,000,000 (n); 1. a very large number, 2. the approximate world population in 1986 when Microsoft Corp issued its IPO. 3. Microsoft's net profit (USD) for the quarter (3 months) ending 31 March 2007.
CMSMS migration and setup services | Hosting with CMSMS installed and ready to go | PM me for Info
Post Reply

Return to “The Lounge”