Pagename Variables

Pligg CMS assigns ‘Pagename’ variables to PHP scripts as a way for template designers to figure out what script is being used to generate the page. Variables are often used in Pligg templates to determine whether or not you should display content by using template logic, much like how you can construct logical operators with programming languages such as PHP or JavaScript. This is useful when you want to set an ‘active’ state to an element like a navigation menu.

<li class="menu_item {if $pagename eq 'index' || $pagename eq 'published'}active{/if}">Home</li>
<li class="menu_item {if $pagename eq 'new'}active{/if}">New</li>
<li class="menu_item {if $pagename eq 'groups'}active{/if}">Groups</li>
<li class="menu_item {if $pagename eq 'submit'}active{/if}">Submit</li>

Pagename variables are defined in Pligg’s .php files to determine what page you are currently viewing. Typically the $pagename variable is equal to the name of the php file that is generating the page. If you open up submit.php as an example, you will find the code below which assigns the pagename variable “submit”.

// pagename
define('pagename', 'submit');
$main_smarty->assign('pagename', pagename);

You can use {elseif} and {else} modifiers to create more complex logical operators.

{if $pagename eq 'submit'}
    This appears on the submit page
{elseif $pagename eq 'new'}
    This appears on the new page
    This appears on every other page

You can use ‘eq’ (or ‘==’) for equals, and ‘neq’ (or ‘!=’) for does not equal.

{if $pagename eq "story"} 
   You are viewing a story page
{if $pagename neq "story"}  
   You are NOT viewing a story page

Advanced Pagename Use

The homepage is probably the one exception to the naming convention used by $pagename variables. The $pagename variable for the homepage is ‘index’, while all other published pages use the value ‘published’. You can combine logical elements by using two brackets ( || ), which represent an ‘OR’ condition. In the example below we are using the logic “if the pagename is equal to ‘index’ OR if the pagename is equal to ‘published'”.

{if $pagename eq "index" || $pagename eq "published"}
    This appears on both the homepage (index) and published pages (homepage paginated results)

By combining elements with two ampersand symbols ( && ) you are creating an ‘AND’ condition. In order for us to demonstrate this concept me will be using the $isAdmin template variable to check if the user is logged in as an Admin level account. The second part of this condition checks if they are also on the groups page.

{if $isAdmin eq 1 && $pagename eq 'groups'}
    Is Admin user AND is the groups page

My final example that I would like to share is how you can add content to the first page of results for the “New” page.

{if $pagename == 'new' && count($templatelite.get) == 0} 
   First New page 

Have another example that you would like to share? Have a correction for what I’ve posted above? Post a comment and let me know.

Share this post