Different conditions of displaying in ZOO and JBZoo item templates

8.0/10 rating (1 votes)

Before you read this article, please, look through the information regarding documentation about JBZoo item templates. This will really help you when customizing our app. Quite often there appear lots of questions about adding some conditions in output of catalog elements.

In this article we'll examine some examples of conditions for template positions (e.g. full or teaser). If there are several elements in one position, but only one needs a condition, this element should be moved to a different or new position.

Let's have a first look at some simple examples:

1. Conditions for authorized/anonymous users

To specify conditions for different groups of users, please, insert the following code:

Display only for authorized users:

<?php 
    $user = JFactory::getUser();
    if (!$user->guest) {
        <CODE>
    } 
?>

Display only for anonymous users:

<?php 
    $user = JFactory::getUser();
    if ($user->guest) {
        <CODE>
    }
?>

2. Conditions for a specific user group

User group is determined by ID, which you can look in "Users" -> "Groups":

Group ID

Display only for an "Author" group:

<?php
    $user = JFactory::getUser();
    if (in_array('3', $user->groups) {
        <CODE>
    }
?>

Display only for Super Users:

<?php
    $user = JFactory::getUser();
    if ($user->superadmin) {
        <CODE>
    }
?>

3. Condition for viewing teaser from a category but not from the frontpage

This condition could be applied when you want a teaser template in a category to differ from the one on the frontpage.

<?php
//Checking if current view is a category
(JRequest::getVar('view') == 'category') ? $category = true : $category = false;
 
//Display only on the frontpage
if(!$category){
    <CODE>
}
 
//Display in all categories but not on the frontpage
if($category){
    <CODE>
}
?>

4. Condition for viewing teaser from specific categories

This condition could be applied if you want the teaser template to be different only in specific categories.

Attention: if a category is attached to a Joomla menu item, this condition will not work. In this case, use condition #5.

<?php
//Getting variable with category id
$category_id = JRequest::getInt('category_id');
 
//Display only in category with id=2
if($category_id == '2'){
    <CODE>
}
 
//Display only in categories with id = 5, 6, 7, 8
$categories = array(5,6,7,8);
if(in_array($category_id, $categories)){
    <CODE>
}
?>

5. Condition for a specific menu item

You can see the id of a menu item either by looking at its url (with SEF off) or by going to Joomla menu manager:

Menu ID in url

Menu ID

<?php
//Getting variable with menu id
$itemid = JRequest::getVar('Itemid');
//Display if menu id=130
if($itemid == '130'){
    <CODE>
}
?>

6. Condition for a specific date range (e.g. from March, 21 to April, 1, 2013)

You can enter a specific date or required range (say, from March, 21 to April, 1, 2013)

<?php
    //Getting a variable with initial date
    $timestart = mktime(0,0,0,3,21,2013);
 
    //Getting a variable with final date
    $timestop = mktime(0,0,0,4,1,2013);
 
    //Getting a variable with current date
    $time = time();
 
    //Display only within these dates: 2013.03.21 - 2013.04.01
    if( $timestart < $time && $time < $timestop ){
        <CODE>
    }
?>

7. How to limit an output by words (or symbols)

Limit by 30 words:

<?php
    $desc = JString::trim(strip_tags($this->renderPosition('<POSITION_NAME>')));
 
    $descArr = explode(' ', $desc);
    $descArr_tmp = array();
 
    foreach ($descArr as $word) {
        $word = JString::trim($word);
        if ($word) {
            $descArr_tmp[] = $word;
        }
    }
 
    if (count($descArr_tmp) > 30) {
        $descArr_tmp = array_slice($descArr_tmp, 0, 30);
        echo implode(' ', $descArr_tmp) . ' ...';
    } else {
        echo implode(' ', $descArr_tmp);
    }
?>

Output string lenghth is limited to no more than 250 symbols. At that, words at the end of it will stay integral and multidrop line might be added, if necessary.

<?php
echo $this->app->jbstring->cutByWords($this->renderPosition('<POSITION_NAME>'), 250);

Combined example #1

Example of a teaser template with following conditions:

  • "price" position is displayed only for authorized users
  • "rating" position is displayed only on categories pages
  • description in the "subtitle" position is limited to 30 words
<?php
defined('_JEXEC') or die('Restricted access');
 
$align = $this->app->jbitem->getMediaAlign($item, $layout);
 
//Getting $user object
$user = JFactory::getUser();
 
//Getting category variable
(JRequest::getVar('view') == 'category') ? $category = true : $category = false;
?>
 
<?php if ($this->checkPosition('image')) : ?>
    <div class="item-image align-<?php echo $align;?>">
        <?php echo $this->renderPosition('image');?>
    </div>
<?php endif; ?>
 
<div class="product-props">
    <?php if ($this->checkPosition('title')) : ?>
        <h4 class="item-title"><?php echo $this->renderPosition('title'); ?></h4>
    <?php endif; ?>
 
    <?php //Trimming text in the subtitle position to 30 words ?>
    <p><i>
    <?php
    $desc = JString::trim(strip_tags($this->renderPosition('subtitle')));
    $descArr = explode(' ', $desc);
    $descArr_tmp = array();
    foreach ($descArr as $word) {
        if ($word = JString::trim($word)) {
            $descArr_tmp[] = $word;
        }
    }
 
    if (count($descArr_tmp) > 30) {
        $descArr_tmp = array_slice($descArr_tmp, 0, 30);
        echo implode(' ', $descArr_tmp) . ' ...';
    } else {
        echo implode(' ', $descArr_tmp);
    } ?>
</i></p>
 
<?php  //Adding condition for displaying price position to authorized users only
if ($this->checkPosition('price') && !$user->guest) : ?>
    <p><?php echo $this->renderPosition('price'); ?></p>
<?php endif; ?>
 
<?php if ($this->checkPosition('properties')) : ?>
    <ul>
        <?php echo $this->renderPosition('properties', array('style' => 'list')); ?>
    </ul>
<?php endif; ?>
 
<?php //Adding condition for displaying rating position only in categories pages
    if($category) {
        echo $this->renderPosition('rating', array('style' => 'block'));
    }
?>
</div>
 
<?php echo $this->renderPosition('links', array('style' => 'pipe')); ?>

Combined example #2

Example of full template with following conditions:

  • "anons" position is displayed only for Super Users
  • "meta" position is displayed only from June, 1, 2013 to August, 31, 2013
<?php
defined('_JEXEC') or die('Restricted access');
 
$align = $this->app->jbitem->getMediaAlign($item, $layout);
 
echo $this->renderPosition('title',     array('style' => 'jbtitle'));
echo $this->renderPosition('subtitle',  array('style' => 'jbsubtitle'));
echo $this->renderPosition('likes',     array(
        'style' => 'jbblock',
        'class' => 'align-left'
    )
);
echo $this->renderPosition('rating', array(
        'style' => 'jbblock',
        'class' => 'align-right',
    )
);
 
//Getting $user object
$user = JFactory::getUser();
 
//Getting a variable with initial date
$timestart = mktime(0,0,0,6,1,2013);
 
//Getting a variable with final date
$timestop = mktime(0,0,0,8,31,2013);
 
//Getting a variable with current date
$time = time();
?>
<div class="rborder item-body">
 
    <?php if ($this->checkPosition('image')) : ?>
        <div class="item-image align-<?php echo $align;?>">
            <?php echo $this->renderPosition('image'); ?>
        </div>
    <?php endif; ?>
 
    <?php //Adding condition for displaying anons position only for Super Users
     if ($this->checkPosition('anons') && $user->superadmin) : ?>
        <div class="item-anons">
            <?php echo $this->renderPosition('anons'); ?>
        </div>
    <?php endif; ?>
 
    <?php  //Adding condition for displaying meta position only within specified date range
      if ($this->checkPosition('meta') && $timestart < $time && $time < $timestop) : ?>
        <hr/>
        <div class="item-meta">
            <?php echo $this->renderPosition('meta', array(
                'style'    => 'jbblock',
                'labelTag' => 'strong',
                'tag'      => 'p'
            )); ?>
        </div>
    <?php endif; ?>
    <div class="clear clr"></div>
</div>

Combined example #3

Example of an inline filter template:

  • "fields1" position is displayed only for menu item with id=101
  • "fields2" position is displayed for other menu items
<?php
defined('_JEXEC') or die('Restricted access');
 
//Getting an ID of the current menu item
$itemid = JRequest::getInt('Itemid');
 
if($itemid == '101'): // Condition for displaying fields1 position when menu id=101 ?>
    <div class="filter-inline">
        <?php echo  $this->renderPosition('fields1', array('style' => 'filter.block'));?>
    </div>
    
<?php else: // Display fields2 position in all other menu items ?>
    <div class="filter-inline">
        <?php echo  $this->renderPosition('fields2', array('style' => 'filter.block'));?>
    </div>
 
<?php endif; ?>

Notes

Under the mark “CODE” you can write any PHP or HTML code. For example, position output may look like this:

<?php echo $this->renderPosition('position_name', array('style' => 'list'));?>
API, Template

Still gotta questions, who can help me?

Visit our special JBZoo tech support forum which is specifically meant for JBZoo so the priority of the response and its promptness are much better there than in any other place. With inactive account with a “Plus” sign in a tariff plan you can ask for support in one of these sections or use any of our contacts.