Advertisement
uzielweb

Dynamic Width Joomla Positions in a Template

May 27th, 2016
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.75 KB | None | 0 0
  1.  
  2. <?php
  3.  
  4. // THIS IS A BASIC MODIFICATION OF BLANK TEMPLATE TO SUPPORT AUTOMATIC, CUSTOM OR BOOTSTRAPPED DYNAMIC WIDTHS FOR MODULE POSITIONS
  5. // WORKS IN ANY TEMPLATE :)
  6. // SAY GOOD BYE TO FIXED WIDTH IN MODULE POSITIONS
  7.  
  8. defined('_JEXEC') or die;
  9. include_once JPATH_THEMES . '/' . $this->template . '/logic.php';
  10.  
  11. ?>
  12. <!doctype html>
  13. <html lang="<?php
  14. echo $this->language; ?>">
  15. <head>
  16. <jdoc:include type="head" />
  17. <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" />
  18. <link rel="apple-touch-icon-precomposed" href="<?php
  19. echo $tpath; ?>/images/apple-touch-icon-57x57-precomposed.png">
  20. <link rel="apple-touch-icon-precomposed" sizes="72x72" href="<?php
  21. echo $tpath; ?>/images/apple-touch-icon-72x72-precomposed.png">
  22. <link rel="apple-touch-icon-precomposed" sizes="114x114" href="<?php
  23. echo $tpath; ?>/images/apple-touch-icon-114x114-precomposed.png">
  24. <link rel="apple-touch-icon-precomposed" sizes="144x144" href="<?php
  25. echo $tpath; ?>/images/apple-touch-icon-144x144-precomposed.png">
  26. </head>
  27. <body class="<?php
  28. echo (($menu->getActive() == $menu->getDefault()) ? ('front') : ('site')) . ' ' . $active->alias . ' ' . $pageclass; ?>">  
  29. <?php
  30.  
  31. function positions($position, $style)
  32.     {
  33.  
  34.     // Default width - for one column
  35.     // This gets new value, if there is more than one active position
  36.     // For Bootstrap use
  37.  
  38.     $width = 'col-md-12';
  39.  
  40.     // For Custom width use
  41.     // $width = "100";
  42.     // Number of positions, which have modules
  43.  
  44.     $countOfActivePositions = 0;
  45.  
  46.     // Positions to search modules in
  47.     // Loop over every position
  48.  
  49.     $totalWidth = 0;
  50.     foreach($position as $name => $value)
  51.         {
  52.  
  53.         // If position has modules
  54.  
  55.         if (JFactory::getDocument()->countModules($name))
  56.             {
  57.  
  58.             // Increase active positions count
  59.  
  60.             $countOfActivePositions++;
  61.             $totalWidth = $totalWidth + $value;
  62.             }
  63.         }
  64.  
  65.     if ($countOfActivePositions > 1)
  66.         {
  67.  
  68.         // For Bootstrap with equal widths use
  69.         // $width = "col-md-".(12 / $countOfActivePositions);
  70.         // For Custom with equal widths use
  71.         // $width = (100 / $countOfActivePositions);
  72.  
  73.         }
  74.  
  75.     foreach($position as $name => $value)
  76.         {
  77.         if ($value > 0)
  78.             {
  79.             $width = $value;
  80.             }
  81.  
  82.         if ($totalWidth < 100)
  83.             {
  84.  
  85.             // For Bootstrap remove/comment the $width bellow
  86.             // For custom with equal widths add/uncomment the $width bellow
  87.             // For custom with proporcional widths add/uncomment the $width bellow
  88.             // $width=$value*100/$totalWidth;
  89.             // For Bootstrap with proporcional widths add/uncomment the $width bellow
  90.             // For Bootstrap with equal widths add/uncomment the $width bellow
  91.  
  92.             $width = 'col-md-' . round($value * 12 / $totalWidth);
  93.             }
  94.  
  95.         /**
  96.  
  97.          // For Bootstrap with proporcional widths remove/comment the IF bellow
  98.          // For Custom with with more then 1 position add/uncomment the IF bellow
  99.  
  100.          if (($value > 0) and ($countOfActivaPositions == 2) and (count($position) > 2))
  101.          {
  102.          $width = 100 / 2;
  103.          }
  104.  
  105.          // For Bootstrap with proporcional widths remove/comment the IF bellow
  106.          // For Custom with with more then 1 position add/uncomment the IF bellow
  107.  
  108.          if (($value > 0) and ($countOfActivePositions == 3) and (count($position) > 3))
  109.          {
  110.          $width = 100 / 3;
  111.          }
  112.  
  113.          // For Bootstrap with proporcional widths remove/comment the IF bellow
  114.          // For Custom with with more then 1 position add/uncomment the IF bellow
  115.  
  116.          if (($value > 0) and ($countOfActivePositions == 4) and (count($position) > 4))
  117.          {
  118.          $width = 100 / 4;
  119.          }
  120.  
  121.          */
  122.         if (JFactory::getDocument()->countModules($name))
  123.             {
  124.  
  125.             // For Bootstrap use
  126.  
  127.             echo '<div class="' . $name . ' ' . $width . '"><jdoc:include type="modules" name="' . $name . '" style="' . $style . '" /></div>';
  128.  
  129.             // For Custom Width use
  130.             // echo '<div class="' . $name . ' ' . '" style="float:left; width:' . $width . '%"><jdoc:include type="modules" name="' . $name . '" style="' . $style . '" /></div>';
  131.  
  132.             }
  133.         }
  134.     }
  135.  
  136. // For Bootstrap use the grid divisions by 12 in the width keys like this: 1,2,3,4,5,6,7,8,9,10,11
  137. // Then use the code like this bellow
  138. // echo positions(array('menu' => 4, 'login' => 6, 'nada' => 2), 'block');
  139. // If You wish to use with equal percentages FOREVER use the code like this bellow
  140. // echo positions(array('menu', 'login', 'nada'), 'block');
  141. // If You wish to use with custom percentages use the code like this bellow
  142. // echo positions(array('menu' => 60, 'login' => 20, 'nada' => 20), 'block');
  143.  
  144. ?>
  145. <?php
  146.  
  147. if ($this->countModules('menu', 'login', 'nothing')): ?>  
  148. <?php
  149.  
  150.     // If You wish to use with custom percentages use the code bellow
  151.  
  152.     echo positions(array(
  153.         'menu' => 4,
  154.         'search' => 1,
  155.         'nothing' => 6
  156.     ) , 'block');
  157. ?>
  158. <?php
  159. endif; ?>  
  160. <jdoc:include type="modules" name="debug" />
  161. </body>
  162. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement