bottom

Joomla! 1.5 Templates - Conditional Statements

tags: 
Conditionals What They Do
<?php if($this->countModules('condition')) { ?>
     do something
<?php } else { ?>
     do something else
<?php } ?>
Controls what actions the template's index.php file takes depending on the current state of a module or a
set of modules
<?php if(JRequest::getVar('view') == ('current_view') { ?>
    do something
<?php } else { ?>
     do something else
<?php } ?>
Control what actions the template's index.php file takes depending on the current view, e.g., article, category, section, contact, etc.  Although this can be used to determine if the current view is the front page ('frontpage'), the Joomla! core developers recommend use of JSite::getMenu(), $menu->getActive, and $menu->getDefault to determine if the current view is the front page.
<?php $menu = &JSite::getMenu(); ?>
<?php if ($menu->getActive() == $menu->getDefault()) { ?>
     do something
<?php } else { ?>
    do something else
<?php } ?>
Control what actions the template's index.php file takes if it is the front page versus other pages.
<?php

$userattr = JFactory::getUser();
$condition = $userattr->get('attribute')
if($condition == 'return value') { ?>
do something

<?php } ?>
'attribute' is one of the following;

'id' – user id
'name' – user name
'aid' – defines the user access identifier and acts in the same way as the gid in Joomla! 1.0.x (normally this is 0 for for public, 1 for registered and for for special). The aid is used against the systems access database fields to define if the user can access a certain database res
'gid' – defines the actual acl user group identifier (19, author, 20 editor, 22 publisher, 23 manager, 24 administrator, 25 super administrator
'guest' – defines if a user is logged in or not (when logged in guest is 0)
'usertype' – user type (Registered, Author, Editor, Publisher, Super Administrator, Adminiistrator, Manager)

Note that this may still be a "work in progress" and may change with future releases."  For more about this take a look here.

Examples of Conditional Usage
<?php if($this->countModules('condition')) ?>
    if($this->countModules('user1') – evaluates to true (returns 1) if there are active (i.e., published) module(s) in user1 position

    if($this->countModules('user1 and user2') – returns 1 if there are active module(s) in user1 and user2 positions

    if($this->countModules('user1 or user2') – returns 1 if there are active module(s) in user1 or user2 positions

    $your_local_var = $this->countModules('user1 + user2') – returns count of active module(s) in user1 + user2 positions

<?php if($this->countModules('condition')) ?>
<?php if($this->countModules('top')) { ?>
&lt!-- if any active modules in the "top" position, load them now and use xhtml encapsulation -->
    <jdoc:include type="modules" name="top" style="xhtml" />
<?php } ?>
<?php $menu = &JSite::getMenu(); ?>
<?php if ($menu->getActive() == $menu->getDefault()) ?>
<div id="leftbox"> <!-- always load left position modules -->
    <jdoc:include type="modules" name="left" style="xhtml" />
</div> <!-- close leftbox -->>
<!-- check to see if on front page -->
<?php $menu = &JSite::getMenu(); ?>
<?php if ($menu->getActive() == $menu->getDefault()) { ?>
    
    <div id="rightbox"> <!-- load right module position -->
       <jdoc:include type="modules" name="right" style="xhtml" />
    </div> <!-- close rightmodule position -->
    <div id="centerboxfp"> <!-- do three-column front page style -->
<?php } else { ?>
    <!-- not front page, do two-column style -->
    <div id="centerbox">
<?php } ?>
    ...
    ...
    ...
</div> &lt!-- close centerboxfp or centerbox -->
<?php $userattr = JFactory::getUser(); $condition = $userattr->get('attribute'); ?>
<?php
$userattr = JFactory::getUser;
$thisuser = $userattr ->get('guest');
if($thisuser == 0) { ?>   // is this a guest user?
    do guest user stuff   //yes
<?php } else { ?>
    do logged-in user stuff   //no
<?php } ?>

Visitors

World Map

See what parts of the World have visited HR's Industrial Strength Web Portal.  Click on the different colored regions to find out.North and Central AmericaSouth AmericaEuropeAfricaMiddle EastOceania
Asia

World map courtesy of
graphicmaps.com

Take a Survey

Take the ALA Survey!

Syndicate content