Joomla! 1.5 Templates - Conditional Statements
| 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 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')) { ?> <!-- 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> <!-- 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 } ?> |


