Source for file head.php

Documentation is available at head.php

  1. <?php
  2. /**
  3.  * @package     Joomla.Platform
  4.  * @subpackage  Document
  5.  *
  6.  * @copyright   Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
  7.  * @license     GNU General Public License version 2 or later; see LICENSE
  8.  */
  9.  
  10. defined('JPATH_PLATFORM'or die;
  11.  
  12. /**
  13.  * JDocument head renderer
  14.  *
  15.  * @package     Joomla.Platform
  16.  * @subpackage  Document
  17.  * @since       11.1
  18.  */
  19. {
  20.     /**
  21.      * Renders the document head and returns the results as a string
  22.      *
  23.      * @param   string  $head     (unused)
  24.      * @param   array   $params   Associative array of values
  25.      * @param   string  $content  The script
  26.      *
  27.      * @return  string  The output of the script
  28.      *
  29.      * @since   11.1
  30.      *
  31.      * @note    Unused arguments are retained to preserve backward compatibility.
  32.      */
  33.     public function render($head$params array()$content null)
  34.     {
  35.         ob_start();
  36.         echo $this->fetchHead($this->_doc);
  37.         $buffer ob_get_contents();
  38.         ob_end_clean();
  39.  
  40.         return $buffer;
  41.     }
  42.  
  43.     /**
  44.      * Generates the head HTML and return the results as a string
  45.      *
  46.      * @param   JDocument  $document  The document for which the head will be created
  47.      *
  48.      * @return  string  The head hTML
  49.      *
  50.      * @since   11.1
  51.      */
  52.     public function fetchHead($document)
  53.     {
  54.         // Convert the tagids to titles
  55.         if (isset($document->_metaTags['standard']['tags']))
  56.         {
  57.             $tagsHelper new JHelperTags;
  58.             $document->_metaTags['standard']['tags'implode(', '$tagsHelper->getTagNames($document->_metaTags['standard']['tags']));
  59.         }
  60.  
  61.         // Trigger the onBeforeCompileHead event
  62.         $app JFactory::getApplication();
  63.         $app->triggerEvent('onBeforeCompileHead');
  64.  
  65.         // Get line endings
  66.         $lnEnd $document->_getLineEnd();
  67.         $tab $document->_getTab();
  68.         $tagEnd ' />';
  69.         $buffer '';
  70.  
  71.         // Generate charset when using HTML5 (should happen first)
  72.         if ($document->isHtml5())
  73.         {
  74.             $buffer .= $tab '<meta charset="' $document->getCharset('" />' $lnEnd;
  75.         }
  76.  
  77.         // Generate base tag (need to happen early)
  78.         $base $document->getBase();
  79.         if (!empty($base))
  80.         {
  81.             $buffer .= $tab '<base href="' $document->getBase('" />' $lnEnd;
  82.         }
  83.  
  84.         // Generate META tags (needs to happen as early as possible in the head)
  85.         foreach ($document->_metaTags as $type => $tag)
  86.         {
  87.             foreach ($tag as $name => $content)
  88.             {
  89.                 if ($type == 'http-equiv' && !($document->isHtml5(&& $name == 'content-type'))
  90.                 {
  91.                     $buffer .= $tab '<meta http-equiv="' $name '" content="' htmlspecialchars($content'" />' $lnEnd;
  92.                 }
  93.                 elseif ($type == 'standard' && !empty($content))
  94.                 {
  95.                     $buffer .= $tab '<meta name="' $name '" content="' htmlspecialchars($content'" />' $lnEnd;
  96.                 }
  97.             }
  98.         }
  99.  
  100.         // Don't add empty descriptions
  101.         $documentDescription $document->getDescription();
  102.         if ($documentDescription)
  103.         {
  104.             $buffer .= $tab '<meta name="description" content="' htmlspecialchars($documentDescription'" />' $lnEnd;
  105.         }
  106.  
  107.         // Don't add empty generators
  108.         $generator $document->getGenerator();
  109.         if ($generator)
  110.         {
  111.             $buffer .= $tab '<meta name="generator" content="' htmlspecialchars($generator'" />' $lnEnd;
  112.         }
  113.  
  114.         $buffer .= $tab '<title>' htmlspecialchars($document->getTitle()ENT_COMPAT'UTF-8''</title>' $lnEnd;
  115.  
  116.         // Generate link declarations
  117.         foreach ($document->_links as $link => $linkAtrr)
  118.         {
  119.             $buffer .= $tab '<link href="' $link '" ' $linkAtrr['relType''="' $linkAtrr['relation''"';
  120.             if ($temp JArrayHelper::toString($linkAtrr['attribs']))
  121.             {
  122.                 $buffer .= ' ' $temp;
  123.             }
  124.             $buffer .= ' />' $lnEnd;
  125.         }
  126.  
  127.         // Generate stylesheet links
  128.         foreach ($document->_styleSheets as $strSrc => $strAttr)
  129.         {
  130.             $buffer .= $tab '<link rel="stylesheet" href="' $strSrc '" type="' $strAttr['mime''"';
  131.             if (!is_null($strAttr['media']))
  132.             {
  133.                 $buffer .= ' media="' $strAttr['media''" ';
  134.             }
  135.             if ($temp JArrayHelper::toString($strAttr['attribs']))
  136.             {
  137.                 $buffer .= ' ' $temp;
  138.             }
  139.             $buffer .= $tagEnd $lnEnd;
  140.         }
  141.  
  142.         // Generate stylesheet declarations
  143.         foreach ($document->_style as $type => $content)
  144.         {
  145.             $buffer .= $tab '<style type="' $type '">' $lnEnd;
  146.  
  147.             // This is for full XHTML support.
  148.             if ($document->_mime != 'text/html')
  149.             {
  150.                 $buffer .= $tab $tab '<![CDATA[' $lnEnd;
  151.             }
  152.  
  153.             $buffer .= $content $lnEnd;
  154.  
  155.             // See above note
  156.             if ($document->_mime != 'text/html')
  157.             {
  158.                 $buffer .= $tab $tab ']]>' $lnEnd;
  159.             }
  160.             $buffer .= $tab '</style>' $lnEnd;
  161.         }
  162.  
  163.         // Generate script file links
  164.         foreach ($document->_scripts as $strSrc => $strAttr)
  165.         {
  166.             $buffer .= $tab '<script src="' $strSrc '"';
  167.             if (!is_null($strAttr['mime']))
  168.             {
  169.                 $buffer .= ' type="' $strAttr['mime''"';
  170.             }
  171.             if ($strAttr['defer'])
  172.             {
  173.                 $buffer .= ' defer="defer"';
  174.             }
  175.             if ($strAttr['async'])
  176.             {
  177.                 $buffer .= ' async="async"';
  178.             }
  179.             $buffer .= '></script>' $lnEnd;
  180.         }
  181.  
  182.         // Generate script declarations
  183.         foreach ($document->_script as $type => $content)
  184.         {
  185.             $buffer .= $tab '<script type="' $type '">' $lnEnd;
  186.  
  187.             // This is for full XHTML support.
  188.             if ($document->_mime != 'text/html')
  189.             {
  190.                 $buffer .= $tab $tab '<![CDATA[' $lnEnd;
  191.             }
  192.  
  193.             $buffer .= $content $lnEnd;
  194.  
  195.             // See above note
  196.             if ($document->_mime != 'text/html')
  197.             {
  198.                 $buffer .= $tab $tab ']]>' $lnEnd;
  199.             }
  200.             $buffer .= $tab '</script>' $lnEnd;
  201.         }
  202.  
  203.         // Generate script language declarations.
  204.         if (count(JText::script()))
  205.         {
  206.             $buffer .= $tab '<script type="text/javascript">' $lnEnd;
  207.             $buffer .= $tab $tab '(function() {' $lnEnd;
  208.             $buffer .= $tab $tab $tab 'var strings = ' json_encode(JText::script()) ';' $lnEnd;
  209.             $buffer .= $tab $tab $tab 'if (typeof Joomla == \'undefined\') {' $lnEnd;
  210.             $buffer .= $tab $tab $tab $tab 'Joomla = {};' $lnEnd;
  211.             $buffer .= $tab $tab $tab $tab 'Joomla.JText = strings;' $lnEnd;
  212.             $buffer .= $tab $tab $tab '}' $lnEnd;
  213.             $buffer .= $tab $tab $tab 'else {' $lnEnd;
  214.             $buffer .= $tab $tab $tab $tab 'Joomla.JText.load(strings);' $lnEnd;
  215.             $buffer .= $tab $tab $tab '}' $lnEnd;
  216.             $buffer .= $tab $tab '})();' $lnEnd;
  217.             $buffer .= $tab '</script>' $lnEnd;
  218.         }
  219.  
  220.         foreach ($document->_custom as $custom)
  221.         {
  222.             $buffer .= $tab $custom $lnEnd;
  223.         }
  224.  
  225.         return $buffer;
  226.     }
  227. }

Documentation generated on Tue, 19 Nov 2013 15:04:12 +0100 by phpDocumentor 1.4.3