|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* THead class file |
|
4
|
|
|
* |
|
5
|
|
|
* @author Marcus Nyeholt <[email protected]> and Qiang Xue <[email protected]> |
|
6
|
|
|
* @link https://github.com/pradosoft/prado |
|
7
|
|
|
* @license https://github.com/pradosoft/prado/blob/master/LICENSE |
|
8
|
|
|
* @package Prado\Web\UI\WebControls |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Prado\Web\UI\WebControls; |
|
12
|
|
|
|
|
13
|
|
|
use Prado\Web\THttpUtility; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* THead class |
|
17
|
|
|
* |
|
18
|
|
|
* THead displays a head element on a page. It displays the content |
|
19
|
|
|
* enclosed in its body and the page title set by the |
|
20
|
|
|
* {@link setTitle Title} property. In addition, stylesheets and JavaScripts registered via |
|
21
|
|
|
* {@link TClientScriptManager::registerStyleSheet}, {@link TClientScriptManager::registerStyleSheetFile} |
|
22
|
|
|
* {@link TClientScriptManager::registerHeadJavaScript}, and |
|
23
|
|
|
* {@link TClientScriptManager::registerHeadJavaScriptFile} will also be displayed |
|
24
|
|
|
* in the head. |
|
25
|
|
|
* THead also manages and displays meta tags through its {@link getMetaTags MetaTags} |
|
26
|
|
|
* property. You can add a meta object to the collection in code dynamically, |
|
27
|
|
|
* or add it in template using the following syntax, |
|
28
|
|
|
* <code> |
|
29
|
|
|
* <com:THead> |
|
30
|
|
|
* <com:TMetaTag HttpEquiv="Pragma" Content="no-cache" /> |
|
31
|
|
|
* <com:TMetaTag Name="keywords" Content="Prado" /> |
|
32
|
|
|
* </com:THead> |
|
33
|
|
|
* </code> |
|
34
|
|
|
* |
|
35
|
|
|
* Note, {@link TPage} has a property {@link TPage::getHead Head} that refers to |
|
36
|
|
|
* the THead control currently on the page. A page can have at most one THead |
|
37
|
|
|
* control. Although not required, it is recommended to place a THead on your page. |
|
38
|
|
|
* Without a THead on the page, stylesheets and javascripts in the current page |
|
39
|
|
|
* theme will not be rendered. |
|
40
|
|
|
* |
|
41
|
|
|
* @author Marcus Nyeholt <[email protected]> and Qiang Xue <[email protected]> |
|
42
|
|
|
* @package Prado\Web\UI\WebControls |
|
43
|
|
|
* @since 3.0 |
|
44
|
|
|
*/ |
|
45
|
|
|
class THead extends \Prado\Web\UI\TControl |
|
46
|
|
|
{ |
|
47
|
|
|
/** |
|
48
|
|
|
* @var TList list of meta name tags to be loaded by {@link THead} |
|
49
|
|
|
*/ |
|
50
|
|
|
private $_metaTags; |
|
|
|
|
|
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Registers the head control with the current page. |
|
54
|
|
|
* This method is invoked when the control enters 'Init' stage. |
|
55
|
|
|
* The method raises 'Init' event. |
|
56
|
|
|
* If you override this method, be sure to call the parent implementation |
|
57
|
|
|
* so that the event handlers can be invoked. |
|
58
|
|
|
* @param TEventParameter $param event parameter to be passed to the event handlers |
|
|
|
|
|
|
59
|
|
|
*/ |
|
60
|
|
|
public function onInit($param) |
|
61
|
|
|
{ |
|
62
|
|
|
parent::onInit($param); |
|
63
|
|
|
$this->getPage()->setHead($this); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Processes an object that is created during parsing template. |
|
68
|
|
|
* This method adds TMetaTag components into the {@link getMetaTags MetaTags} |
|
69
|
|
|
* collection of the head control. |
|
70
|
|
|
* @param string|TComponent $object text string or component parsed and instantiated in template |
|
|
|
|
|
|
71
|
|
|
* @see createdOnTemplate |
|
72
|
|
|
*/ |
|
73
|
|
|
public function addParsedObject($object) |
|
74
|
|
|
{ |
|
75
|
|
|
if ($object instanceof TMetaTag) { |
|
76
|
|
|
$this->getMetaTags()->add($object); |
|
77
|
|
|
} else { |
|
78
|
|
|
parent::addParsedObject($object); |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @return string the page title. |
|
84
|
|
|
*/ |
|
85
|
|
|
public function getTitle() |
|
86
|
|
|
{ |
|
87
|
|
|
return $this->getViewState('Title', ''); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Sets the page title. |
|
92
|
|
|
* This title will be rendered only if the {@link TPage::getTitle Title} property |
|
93
|
|
|
* of the page is empty. |
|
94
|
|
|
* @param string $value the page title. |
|
95
|
|
|
*/ |
|
96
|
|
|
public function setTitle($value) |
|
97
|
|
|
{ |
|
98
|
|
|
$this->setViewState('Title', $value, ''); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @return string base URL of the page. This URL is rendered as the 'href' attribute of <base> tag. Defaults to ''. |
|
103
|
|
|
*/ |
|
104
|
|
|
public function getBaseUrl() |
|
105
|
|
|
{ |
|
106
|
|
|
return $this->getViewState('BaseUrl', ''); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* @param string $url base URL of the page. This URL is rendered as the 'href' attribute of <base> tag. |
|
111
|
|
|
*/ |
|
112
|
|
|
public function setBaseUrl($url) |
|
113
|
|
|
{ |
|
114
|
|
|
$this->setViewState('BaseUrl', $url, ''); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* @return string the URL for the shortcut icon of the page. Defaults to ''. |
|
119
|
|
|
*/ |
|
120
|
|
|
public function getShortcutIcon() |
|
121
|
|
|
{ |
|
122
|
|
|
return $this->getViewState('ShortcutIcon', ''); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* @param string $url the URL for the shortcut icon of the page. |
|
127
|
|
|
*/ |
|
128
|
|
|
public function setShortcutIcon($url) |
|
129
|
|
|
{ |
|
130
|
|
|
$this->setViewState('ShortcutIcon', $url, ''); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* @return TMetaTagCollection meta tag collection |
|
135
|
|
|
*/ |
|
136
|
|
|
public function getMetaTags() |
|
137
|
|
|
{ |
|
138
|
|
|
if (($metaTags = $this->getViewState('MetaTags', null)) === null) { |
|
139
|
|
|
$metaTags = new TMetaTagCollection; |
|
140
|
|
|
$this->setViewState('MetaTags', $metaTags, null); |
|
141
|
|
|
} |
|
142
|
|
|
return $metaTags; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* Renders the head control. |
|
147
|
|
|
* @param THtmlWriter $writer the writer for rendering purpose. |
|
|
|
|
|
|
148
|
|
|
*/ |
|
149
|
|
|
public function render($writer) |
|
150
|
|
|
{ |
|
151
|
|
|
$page = $this->getPage(); |
|
152
|
|
|
$title = $this->getTitle(); |
|
153
|
|
|
$writer->write("<head>\n<title>" . THttpUtility::htmlEncode($title) . "</title>\n"); |
|
154
|
|
|
if (($baseUrl = $this->getBaseUrl()) !== '') { |
|
155
|
|
|
$writer->write('<base href="' . $baseUrl . "\" />\n"); |
|
156
|
|
|
} |
|
157
|
|
|
if (($icon = $this->getShortcutIcon()) !== '') { |
|
158
|
|
|
$writer->write('<link rel="shortcut icon" href="' . $icon . "\" />\n"); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
if (($metaTags = $this->getMetaTags()) !== null) { |
|
162
|
|
|
foreach ($metaTags as $metaTag) { |
|
163
|
|
|
$metaTag->render($writer); |
|
164
|
|
|
$writer->writeLine(); |
|
165
|
|
|
} |
|
166
|
|
|
} |
|
167
|
|
|
$cs = $page->getClientScript(); |
|
168
|
|
|
$cs->renderStyleSheetFiles($writer); |
|
169
|
|
|
$cs->renderStyleSheets($writer); |
|
170
|
|
|
if ($page->getClientSupportsJavaScript()) { |
|
171
|
|
|
$cs->renderHeadScriptFiles($writer); |
|
172
|
|
|
$cs->renderHeadScripts($writer); |
|
173
|
|
|
} |
|
174
|
|
|
parent::render($writer); |
|
175
|
|
|
$writer->write("</head>\n"); |
|
176
|
|
|
} |
|
177
|
|
|
} |
|
178
|
|
|
|