1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25:
26:
27:
28: 29: 30: 31: 32: 33: 34:
35: class Mage_Core_Block_Template extends Mage_Core_Block_Abstract
36: {
37: const XML_PATH_DEBUG_TEMPLATE_HINTS = 'dev/debug/template_hints';
38: const XML_PATH_DEBUG_TEMPLATE_HINTS_BLOCKS = 'dev/debug/template_hints_blocks';
39: const XML_PATH_TEMPLATE_ALLOW_SYMLINK = 'dev/template/allow_symlink';
40:
41: 42: 43: 44: 45:
46: protected $_viewDir = '';
47:
48: 49: 50: 51: 52:
53: protected $_viewVars = array();
54:
55: protected $_baseUrl;
56:
57: protected $_jsUrl;
58:
59: 60: 61: 62: 63:
64: protected $_allowSymlinks = null;
65:
66: protected static $_showTemplateHints;
67: protected static $_showTemplateHintsBlocks;
68:
69: 70: 71: 72: 73:
74: protected $_template;
75:
76: 77: 78: 79:
80: protected function _construct()
81: {
82: parent::_construct();
83:
84: 85: 86: 87: 88: 89:
90: if ($this->hasData('template')) {
91: $this->setTemplate($this->getData('template'));
92: }
93: }
94:
95: 96: 97: 98: 99:
100: public function getTemplate()
101: {
102: return $this->_template;
103: }
104:
105: 106: 107: 108: 109: 110:
111: public function setTemplate($template)
112: {
113: $this->_template = $template;
114: return $this;
115: }
116:
117: 118: 119: 120: 121:
122: public function getTemplateFile()
123: {
124: $params = array('_relative'=>true);
125: $area = $this->getArea();
126: if ($area) {
127: $params['_area'] = $area;
128: }
129: $templateName = Mage::getDesign()->getTemplateFilename($this->getTemplate(), $params);
130: return $templateName;
131: }
132:
133: 134: 135: 136:
137: public function getArea()
138: {
139: return $this->_getData('area');
140: }
141:
142: 143: 144: 145: 146: 147: 148:
149: public function assign($key, $value=null)
150: {
151: if (is_array($key)) {
152: foreach ($key as $k=>$v) {
153: $this->assign($k, $v);
154: }
155: }
156: else {
157: $this->_viewVars[$key] = $value;
158: }
159: return $this;
160: }
161:
162: 163: 164: 165: 166: 167:
168: public function setScriptPath($dir)
169: {
170: $scriptPath = realpath($dir);
171: if (strpos($scriptPath, realpath(Mage::getBaseDir('design'))) === 0 || $this->_getAllowSymlinks()) {
172: $this->_viewDir = $dir;
173: } else {
174: Mage::log('Not valid script path:' . $dir, Zend_Log::CRIT, null, null, true);
175: }
176: return $this;
177: }
178:
179: 180: 181: 182: 183:
184: public function getDirectOutput()
185: {
186: if ($this->getLayout()) {
187: return $this->getLayout()->getDirectOutput();
188: }
189: return false;
190: }
191:
192: public function getShowTemplateHints()
193: {
194: if (is_null(self::$_showTemplateHints)) {
195: self::$_showTemplateHints = Mage::getStoreConfig(self::XML_PATH_DEBUG_TEMPLATE_HINTS)
196: && Mage::helper('core')->isDevAllowed();
197: self::$_showTemplateHintsBlocks = Mage::getStoreConfig(self::XML_PATH_DEBUG_TEMPLATE_HINTS_BLOCKS)
198: && Mage::helper('core')->isDevAllowed();
199: }
200: return self::$_showTemplateHints;
201: }
202:
203: 204: 205: 206: 207: 208:
209: public function fetchView($fileName)
210: {
211: Varien_Profiler::start($fileName);
212:
213:
214:
215: extract ($this->_viewVars, EXTR_SKIP);
216: $do = $this->getDirectOutput();
217:
218: if (!$do) {
219: ob_start();
220: }
221: if ($this->getShowTemplateHints()) {
222: echo <<<HTML
223: <div style="position:relative; border:1px dotted red; margin:6px 2px; padding:18px 2px 2px 2px; zoom:1;">
224: <div style="position:absolute; left:0; top:0; padding:2px 5px; background:red; color:white; font:normal 11px Arial;
225: text-align:left !important; z-index:998;" onmouseover="this.style.zIndex='999'"
226: onmouseout="this.style.zIndex='998'" title="{$fileName}">{$fileName}</div>
227: HTML;
228: if (self::$_showTemplateHintsBlocks) {
229: $thisClass = get_class($this);
230: echo <<<HTML
231: <div style="position:absolute; right:0; top:0; padding:2px 5px; background:red; color:blue; font:normal 11px Arial;
232: text-align:left !important; z-index:998;" onmouseover="this.style.zIndex='999'" onmouseout="this.style.zIndex='998'"
233: title="{$thisClass}">{$thisClass}</div>
234: HTML;
235: }
236: }
237:
238: try {
239: $includeFilePath = realpath($this->_viewDir . DS . $fileName);
240: if (strpos($includeFilePath, realpath($this->_viewDir)) === 0 || $this->_getAllowSymlinks()) {
241: include $includeFilePath;
242: } else {
243: Mage::log('Not valid template file:'.$fileName, Zend_Log::CRIT, null, null, true);
244: }
245:
246: } catch (Exception $e) {
247: ob_get_clean();
248: throw $e;
249: }
250:
251: if ($this->getShowTemplateHints()) {
252: echo '</div>';
253: }
254:
255: if (!$do) {
256: $html = ob_get_clean();
257: } else {
258: $html = '';
259: }
260: Varien_Profiler::stop($fileName);
261: return $html;
262: }
263:
264: 265: 266: 267: 268:
269: public function renderView()
270: {
271: $this->setScriptPath(Mage::getBaseDir('design'));
272: $html = $this->fetchView($this->getTemplateFile());
273: return $html;
274: }
275:
276: 277: 278: 279: 280:
281: protected function _toHtml()
282: {
283: if (!$this->getTemplate()) {
284: return '';
285: }
286: $html = $this->renderView();
287: return $html;
288: }
289:
290: 291: 292: 293: 294:
295: public function getBaseUrl()
296: {
297: if (!$this->_baseUrl) {
298: $this->_baseUrl = Mage::getBaseUrl();
299: }
300: return $this->_baseUrl;
301: }
302:
303: 304: 305: 306: 307: 308: 309: 310:
311: public function getJsUrl($fileName='')
312: {
313: if (!$this->_jsUrl) {
314: $this->_jsUrl = Mage::getBaseUrl('js');
315: }
316: return $this->_jsUrl.$fileName;
317: }
318:
319: 320: 321: 322: 323: 324: 325:
326: public function getObjectData(Varien_Object $object, $key)
327: {
328: return $object->getDataUsingMethod((string)$key);
329: }
330:
331: 332: 333: 334: 335:
336: public function getCacheKeyInfo()
337: {
338: return array(
339: 'BLOCK_TPL',
340: Mage::app()->getStore()->getCode(),
341: $this->getTemplateFile(),
342: 'template' => $this->getTemplate()
343: );
344: }
345:
346: 347: 348: 349: 350:
351: protected function _getAllowSymlinks()
352: {
353: if (is_null($this->_allowSymlinks)) {
354: $this->_allowSymlinks = Mage::getStoreConfigFlag(self::XML_PATH_TEMPLATE_ALLOW_SYMLINK);
355: }
356: return $this->_allowSymlinks;
357: }
358: }
359: