*/ class Mage_Core_Block_Template extends Mage_Core_Block_Abstract { const XML_PATH_DEBUG_TEMPLATE_HINTS = 'dev/debug/template_hints'; const XML_PATH_DEBUG_TEMPLATE_HINTS_BLOCKS = 'dev/debug/template_hints_blocks'; const XML_PATH_TEMPLATE_ALLOW_SYMLINK = 'dev/template/allow_symlink'; /** * View scripts directory * * @var string */ protected $_viewDir = ''; /** * Assigned variables for view * * @var array */ protected $_viewVars = array(); protected $_baseUrl; protected $_jsUrl; /** * Is allowed symlinks flag * * @var bool */ protected $_allowSymlinks = null; protected static $_showTemplateHints; protected static $_showTemplateHintsBlocks; /** * Path to template file in theme. * * @var string */ protected $_template; /** * Internal constructor, that is called from real constructor * */ protected function _construct() { parent::_construct(); /* * In case template was passed through constructor * we assign it to block's property _template * Mainly for those cases when block created * not via Mage_Core_Model_Layout::addBlock() */ if ($this->hasData('template')) { $this->setTemplate($this->getData('template')); } } /** * Get relevant path to template * * @return string */ public function getTemplate() { return $this->_template; } /** * Set path to template used for generating block's output. * * @param string $template * @return Mage_Core_Block_Template */ public function setTemplate($template) { $this->_template = $template; return $this; } /** * Get absolute path to template * * @return string */ public function getTemplateFile() { $params = array('_relative'=>true); $area = $this->getArea(); if ($area) { $params['_area'] = $area; } $templateName = Mage::getDesign()->getTemplateFilename($this->getTemplate(), $params); return $templateName; } /** * Get design area * @return string */ public function getArea() { return $this->_getData('area'); } /** * Assign variable * * @param string|array $key * @param mixed $value * @return Mage_Core_Block_Template */ public function assign($key, $value=null) { if (is_array($key)) { foreach ($key as $k=>$v) { $this->assign($k, $v); } } else { $this->_viewVars[$key] = $value; } return $this; } /** * Set template location directory * * @param string $dir * @return Mage_Core_Block_Template */ public function setScriptPath($dir) { $scriptPath = realpath($dir); if (strpos($scriptPath, realpath(Mage::getBaseDir('design'))) === 0 || $this->_getAllowSymlinks()) { $this->_viewDir = $dir; } else { Mage::log('Not valid script path:' . $dir, Zend_Log::CRIT, null, null, true); } return $this; } /** * Check if direct output is allowed for block * * @return bool */ public function getDirectOutput() { if ($this->getLayout()) { return $this->getLayout()->getDirectOutput(); } return false; } public function getShowTemplateHints() { if (is_null(self::$_showTemplateHints)) { self::$_showTemplateHints = Mage::getStoreConfig(self::XML_PATH_DEBUG_TEMPLATE_HINTS) && Mage::helper('core')->isDevAllowed(); self::$_showTemplateHintsBlocks = Mage::getStoreConfig(self::XML_PATH_DEBUG_TEMPLATE_HINTS_BLOCKS) && Mage::helper('core')->isDevAllowed(); } return self::$_showTemplateHints; } /** * Retrieve block view from file (template) * * @param string $fileName * @return string */ public function fetchView($fileName) { Varien_Profiler::start($fileName); // EXTR_SKIP protects from overriding // already defined variables extract ($this->_viewVars, EXTR_SKIP); $do = $this->getDirectOutput(); if (!$do) { ob_start(); } if ($this->getShowTemplateHints()) { echo <<
{$fileName}
HTML; if (self::$_showTemplateHintsBlocks) { $thisClass = get_class($this); echo <<{$thisClass} HTML; } } try { $includeFilePath = realpath($this->_viewDir . DS . $fileName); if (strpos($includeFilePath, realpath($this->_viewDir)) === 0 || $this->_getAllowSymlinks()) { include $includeFilePath; } else { Mage::log('Not valid template file:'.$fileName, Zend_Log::CRIT, null, null, true); } } catch (Exception $e) { ob_get_clean(); throw $e; } if ($this->getShowTemplateHints()) { echo ''; } if (!$do) { $html = ob_get_clean(); } else { $html = ''; } Varien_Profiler::stop($fileName); return $html; } /** * Render block * * @return string */ public function renderView() { $this->setScriptPath(Mage::getBaseDir('design')); $html = $this->fetchView($this->getTemplateFile()); return $html; } /** * Render block HTML * * @return string */ protected function _toHtml() { if (!$this->getTemplate()) { return ''; } $html = $this->renderView(); return $html; } /** * Get base url of the application * * @return string */ public function getBaseUrl() { if (!$this->_baseUrl) { $this->_baseUrl = Mage::getBaseUrl(); } return $this->_baseUrl; } /** * Get url of base javascript file * * To get url of skin javascript file use getSkinUrl() * * @param string $fileName * @return string */ public function getJsUrl($fileName='') { if (!$this->_jsUrl) { $this->_jsUrl = Mage::getBaseUrl('js'); } return $this->_jsUrl.$fileName; } /** * Get data from specified object * * @param Varien_Object $object * @param string $key * @return mixed */ public function getObjectData(Varien_Object $object, $key) { return $object->getDataUsingMethod((string)$key); } /** * Get cache key informative items * * @return array */ public function getCacheKeyInfo() { return array( 'BLOCK_TPL', Mage::app()->getStore()->getCode(), $this->getTemplateFile(), 'template' => $this->getTemplate() ); } /** * Get is allowed symliks flag * * @return bool */ protected function _getAllowSymlinks() { if (is_null($this->_allowSymlinks)) { $this->_allowSymlinks = Mage::getStoreConfigFlag(self::XML_PATH_TEMPLATE_ALLOW_SYMLINK); } return $this->_allowSymlinks; } }