Перейти к содержимому


Фотография
- - - - -

Different conditions of displaying in ZOO and JBZoo item templates

documentation template

  • Закрытая тема Тема закрыта
В этой теме нет ответов

#1 Kess

Kess

Отправлено 20 September 2013 - 02:37

Often there are a lot of questions about using some conditions in catalogue elements output.
 
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:
  1. <?php
  2. $user = JFactory::getUser();
  3. if (!$user->guest) {
  4. <CODE>
  5. }
  6. ?>
  • display only for anonymous users:
  1. <?php
  2. $user = JFactory::getUser();
  3. if ($user->guest) {
  4. <CODE>
  5. }
  6. ?>
2. Conditions for a specific user group:
 
User group is determined by ID, which you can look in "Users" -> "Groups":
 
ssm203a402f_1280x0_200x0.png
  • display only for an "Author" group:
  1. <?php
  2. $user = JFactory::getUser();
  3. if (in_array('3', $user->groups) {
  4. <CODE>
  5. }
  6. ?>
  • display only for Super Users:
  1. <?php
  2. $user = JFactory::getUser();
  3. if ($user->superadmin) {
  4. <CODE>
  5. }
  6. ?>
3. Condition for viewing teaser from a category but not from the frontpage
 
This condition can be used when you want a teaser template in a category to be different from the one on the frontpage.


  1. <?php
  2. //Checking if current view is a category
  3. (JRequest::getVar('view') == 'category') ? $category = true : $category = false;
  4. //Display only on the frontpage
  5. if(!$category){
  6. <CODE>
  7. }
  8. //Display in all categories but not on the frontpage
  9. if($category){
  10. <CODE>
  11. }
  12. ?>
4. Condition for viewing teaser from specific categories
 
This 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.


  1. <?php
  2. //Getting variable with category id
  3. $category_id = JRequest::getInt('category_id');
  4. //Display only in category with id=2
  5. if($category_id == '2'){
  6. <CODE>
  7. }
  8. //Display only in categories with id = 5, 6, 7, 8
  9. $categories = array(5,6,7,8);
  10. if(in_array($category_id, $categories)){
  11. <CODE>
  12. }
  13. ?>
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:
 
ssm0fb2eff4_200x0.png
 
ssmef1a175f_200x0.png


  1. <?php
  2. //Getting variable with menu id
  3. $itemid = JRequest::getVar('Itemid');
  4. //Display if menu id=130
  5. if($itemid == '130'){
  6. <CODE>
  7. }
  8. ?>
6. Condition for a specific date range (e.g. from March, 21 to April, 1, 2013):


  1. <?php
  2. //Getting a variable with initial date
  3. $timestart = mktime(0,0,0,3,21,2013);
  4. //Getting a variable with final date
  5. $timestop = mktime(0,0,0,4,1,2013);
  6. //Getting a variable with current date
  7. $time = time();
  8. //Display only within these dates: 2013.03.21 - 2013.04.01
  9. if( $timestart < $time && $time < $timestop ){
  10. <CODE>
  11. }
  12. ?>
7. How to limit an output by words (or symbols)
  • limit by 30 words:
  1. <?php
  2. $desc = JString::trim(strip_tags($this->renderPosition('<POSITION_NAME>')));
  3. $descArr = explode(' ', $desc);
  4. $descArr_tmp = array();
  5. foreach ($descArr as $word) {
  6. $word = JString::trim($word);
  7. if ($word) {
  8. $descArr_tmp[] = $word;
  9. }
  10. }
  11. if (count($descArr_tmp) > 30) {
  12. $descArr_tmp = array_slice($descArr_tmp, 0, 30);
  13. echo implode(' ', $descArr_tmp) . ' ...';
  14. } else {
  15. echo implode(' ', $descArr_tmp);
  16. }
  17. ?>
  • limit by 300 symbols:
  1. <?php
  2. echo JString::substr(strip_tags($this->renderPosition('<POSITION_NAME>')),0,300);
  3. ?>
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
  1. <?php
  2. defined('_JEXEC') or die('Restricted access');
  3. $align = $this->app->jbitem->getMediaAlign($item, $layout);
  4. //Getting $user object
  5. $user = JFactory::getUser();
  6. //Getting category variable
  7. (JRequest::getVar('view') == 'category') ? $category = true : $category = false;
  8. ?>
  9. <?php if ($this->checkPosition('image')) : ?>
  10. <div class="item-image align-<?php echo $align;?>">
  11. <?php echo $this->renderPosition('image');?>
  12. </div>
  13. <?php endif; ?>
  14. <div class="product-props">
  15. <?php if ($this->checkPosition('title')) : ?>
  16. <h4 class="item-title"><?php echo $this->renderPosition('title'); ?></h4>
  17. <?php endif; ?>
  18. <?php //Trimming text in the subtitle position to 30 words ?>
  19. <p><i>
  20. <?php
  21. $desc = JString::trim(strip_tags($this->renderPosition('subtitle')));
  22. $descArr = explode(' ', $desc);
  23. $descArr_tmp = array();
  24. foreach ($descArr as $word) {
  25. if ($word = JString::trim($word)) {
  26. $descArr_tmp[] = $word;
  27. }
  28. }
  29. if (count($descArr_tmp) > 30) {
  30. $descArr_tmp = array_slice($descArr_tmp, 0, 30);
  31. echo implode(' ', $descArr_tmp) . ' ...';
  32. } else {
  33. echo implode(' ', $descArr_tmp);
  34. } ?>
  35. </i></p>
  36. <?php //Adding condition for displaying price position to authorized users only
  37. if ($this->checkPosition('price') && !$user->guest) : ?>
  38. <p><?php echo $this->renderPosition('price'); ?></p>
  39. <?php endif; ?>
  40. <?php if ($this->checkPosition('properties')) : ?>
  41. <ul>
  42. <?php echo $this->renderPosition('properties', array('style' => 'list')); ?>
  43. </ul>
  44. <?php endif; ?>
  45. <?php //Adding condition for displaying rating position only in categories pages
  46. if($category) {
  47. echo $this->renderPosition('rating', array('style' => 'block'));
  48. }
  49. ?>
  50. </div>
  51. <?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
  1. <?php
  2. defined('_JEXEC') or die('Restricted access');
  3. $align = $this->app->jbitem->getMediaAlign($item, $layout);
  4. echo $this->renderPosition('title', array('style' => 'jbtitle'));
  5. echo $this->renderPosition('subtitle', array('style' => 'jbsubtitle'));
  6. echo $this->renderPosition('likes', array(
  7. 'style' => 'jbblock',
  8. 'class' => 'align-left'
  9. )
  10. );
  11. echo $this->renderPosition('rating', array(
  12. 'style' => 'jbblock',
  13. 'class' => 'align-right',
  14. )
  15. );
  16. //Getting $user object
  17. $user = JFactory::getUser();
  18. //Getting a variable with initial date
  19. $timestart = mktime(0,0,0,6,1,2013);
  20. //Getting a variable with final date
  21. $timestop = mktime(0,0,0,8,31,2013);
  22. //Getting a variable with current date
  23. $time = time();
  24. ?>
  25. <div class="rborder item-body">
  26. <?php if ($this->checkPosition('image')) : ?>
  27. <div class="item-image align-<?php echo $align;?>">
  28. <?php echo $this->renderPosition('image'); ?>
  29. </div>
  30. <?php endif; ?>
  31. <?php //Adding condition for displaying anons position only for Super Users
  32. if ($this->checkPosition('anons') && $user->superadmin) : ?>
  33. <div class="item-anons">
  34. <?php echo $this->renderPosition('anons'); ?>
  35. </div>
  36. <?php endif; ?>
  37. <?php //Adding condition for displaying meta position only within specified date range
  38. if ($this->checkPosition('meta') && $timestart < $time && $time < $timestop) : ?>
  39. <hr/>
  40. <div class="item-meta">
  41. <?php echo $this->renderPosition('meta', array(
  42. 'style' => 'jbblock',
  43. 'labelTag' => 'strong',
  44. 'tag' => 'p'
  45. )); ?>
  46. </div>
  47. <?php endif; ?>
  48. <div class="clear clr"></div>
  49. </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
  1. <?php
  2. defined('_JEXEC') or die('Restricted access');
  3. //Getting an ID of the current menu item
  4. $itemid = JRequest::getInt('Itemid');
  5. if($itemid == '101'): // Condition for displaying fields1 position when menu id=101 ?>
  6. <div class="filter-inline">
  7. <?php echo $this->renderPosition('fields1', array('style' => 'filter.block'));?>
  8. </div>
  9. <?php else: // Display fields2 position in all other menu items ?>
  10. <div class="filter-inline">
  11. <?php echo $this->renderPosition('fields2', array('style' => 'filter.block'));?>
  12. </div>
  13. <?php endif; ?>
Notes
 
Instead of the <CODE> label you can write any PHP or HTML code. For example, position output may look like this:


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

Сообщение отредактировал SmetDenis: 20 September 2013 - 23:38

  • 1





Темы с аналогичным тегами documentation, template

Click to return to top of page in style!