1: <?php
2: /**
3: * Magento
4: *
5: * NOTICE OF LICENSE
6: *
7: * This source file is subject to the Open Software License (OSL 3.0)
8: * that is bundled with this package in the file LICENSE.txt.
9: * It is also available through the world-wide-web at this URL:
10: * http://opensource.org/licenses/osl-3.0.php
11: * If you did not receive a copy of the license and are unable to
12: * obtain it through the world-wide-web, please send an email
13: * to license@magentocommerce.com so we can send you a copy immediately.
14: *
15: * DISCLAIMER
16: *
17: * Do not edit or add to this file if you wish to upgrade Magento to newer
18: * versions in the future. If you wish to customize Magento for your
19: * needs please refer to http://www.magentocommerce.com for more information.
20: *
21: * @category Mage
22: * @package Mage_Page
23: * @copyright Copyright (c) 2012 Magento Inc. (http://www.magentocommerce.com)
24: * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25: */
26:
27: /**
28: * A generic wrapper block that renders its children and supports a few parameters of the wrapper HTML-element
29: */
30: class Mage_Page_Block_Html_Wrapper extends Mage_Core_Block_Abstract
31: {
32: /**
33: * Whether block should render its content if there are no children (no)
34: * @var bool
35: */
36: protected $_dependsOnChildren = true;
37:
38: /**
39: * Render the wrapper element html
40: * Supports different optional parameters, set in data by keys:
41: * - element_tag_name (div by default)
42: * - element_id
43: * - element_class
44: * - element_other_attributes
45: *
46: * Renders all children inside the element.
47: *
48: * @return string
49: */
50: protected function _toHtml()
51: {
52: $html = empty($this->_children) ? '' : trim($this->getChildHtml('', true, true));
53: if ($this->_dependsOnChildren && empty($html)) {
54: return '';
55: }
56: if ($this->_isInvisible()) {
57: return $html;
58: }
59: $id = $this->hasElementId() ? sprintf(' id="%s"', $this->getElementId()) : '';
60: $class = $this->hasElementClass() ? sprintf(' class="%s"', $this->getElementClass()) : '';
61: $otherParams = $this->hasOtherParams() ? ' ' . $this->getOtherParams() : '';
62: return sprintf('<%1$s%2$s%3$s%4$s>%5$s</%1$s>', $this->getElementTagName(), $id, $class, $otherParams, $html);
63: }
64:
65: /**
66: * Wrapper element tag name getter
67: * @return string
68: */
69: public function getElementTagName()
70: {
71: $tagName = $this->_getData('html_tag_name');
72: return $tagName ? $tagName : 'div';
73: }
74:
75: /**
76: * Setter whether this block depends on children
77: * @param $depends
78: * @return Mage_Page_Block_Html_Wrapper
79: */
80: public function dependsOnChildren($depends = '0')
81: {
82: $this->_dependsOnChildren = (bool)(int)$depends;
83: return $this;
84: }
85:
86: /**
87: * Whether the wrapper element should be eventually rendered
88: * If it becomes "invisible", the behaviour will be somewhat similar to core/text_list
89: *
90: * @return bool
91: */
92: protected function _isInvisible()
93: {
94: if (!$this->hasMayBeInvisible()) {
95: return false;
96: }
97: foreach ($this->_children as $child) {
98: if ($child->hasWrapperMustBeVisible()) {
99: return false;
100: }
101: }
102: return true;
103: }
104: }
105: