In this article we'll examine some examples of conditions for a template positions (e.g. full or teaser). If there are several elements in one position, but only one needs condition, this element should be moved to a different or new position.
Let's examine some simple examples:
1. Conditions for authorized/anonymous users:
- 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":
- 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 frontpageThis condition can be used when you want a teaser template in a category to be different 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 categoriesThis condition can be used 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 itemYou can see the id of a menu item either by looking at its url (with SEF off) or by going to Joomla menu manager:
<?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):<?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);
}
?>
- limit by 300 symbols:
<?php
echo JString::substr(strip_tags($this->renderPosition('<POSITION_NAME>')),0,300);
?>
Combined example #1Example 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 #2Example 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 #3Example 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; ?>
NotesInstead of the <CODE> label 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'));?>
Сообщение отредактировал SmetDenis: 20 September 2013 - 23:38


Тема закрыта








