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