Mage_Core_Model_Layout Class Reference

Inheritance diagram for Mage_Core_Model_Layout:

Varien_Simplexml_Config

List of all members.

Public Member Functions

 __construct ($data=array())
 getUpdate ()
 setArea ($area)
 getArea ()
 setDirectOutput ($flag)
 getDirectOutput ()
 generateXml ()
 generateBlocks ($parent=null)
 setBlock ($name, $block)
 unsetBlock ($name)
 createBlock ($type, $name='', array $attributes=array())
 addBlock ($block, $blockName)
 getAllBlocks ()
 getBlock ($name)
 addOutputBlock ($blockName, $method='toHtml')
 removeOutputBlock ($blockName)
 getOutput ()
 getMessagesBlock ()
 getBlockSingleton ($type)
 helper ($name)

Protected Member Functions

 _generateBlock ($node, $parent)
 _generateAction ($node, $parent)
 _translateLayoutNode ($node, &$args)
 _getBlockInstance ($block, array $attributes=array())

Protected Attributes

 $_update
 $_blocks = array()
 $_output = array()
 $_area
 $_helpers = array()
 $_directOutput = false


Detailed Description

Definition at line 34 of file Layout.php.


Constructor & Destructor Documentation

__construct ( data = array()  ) 

Enter description here...

Parameters:
array $data

Reimplemented from Varien_Simplexml_Config.

Definition at line 84 of file Layout.php.

00085     {
00086         $this->_elementClass = Mage::getConfig()->getModelClassName('core/layout_element');
00087         $this->setXml(simplexml_load_string('<layout/>', $this->_elementClass));
00088         parent::__construct($data);
00089     }


Member Function Documentation

_generateAction ( node,
parent 
) [protected]

Enter description here...

Parameters:
Varien_Simplexml_Element $node
Varien_Simplexml_Element $parent
Returns:
Mage_Core_Model_Layout

if there is no helper we hope that this is assoc array

Definition at line 280 of file Layout.php.

00281     {
00282         if (isset($node['ifconfig']) && ($configPath = (string)$node['ifconfig'])) {
00283             if (!Mage::getStoreConfigFlag($configPath)) {
00284                 return $this;
00285             }
00286         }
00287 
00288         $method = (string)$node['method'];
00289         if (!empty($node['block'])) {
00290             $parentName = (string)$node['block'];
00291         } else {
00292             $parentName = $parent->getBlockName();
00293         }
00294 
00295         $_profilerKey = 'BLOCK ACTION: '.$parentName.' -> '.$method;
00296         Varien_Profiler::start($_profilerKey);
00297 
00298         if (!empty($parentName)) {
00299             $block = $this->getBlock($parentName);
00300         }
00301         if (!empty($block)) {
00302 
00303             $args = (array)$node->children();
00304             unset($args['@attributes']);
00305 
00306             foreach ($args as $key => $arg) {
00307                 if (($arg instanceof Mage_Core_Model_Layout_Element)) {
00308                     if (isset($arg['helper'])) {
00309                         $helperName = explode('/', (string)$arg['helper']);
00310                         $helperMethod = array_pop($helperName);
00311                         $helperName = implode('/', $helperName);
00312                         $arg = $arg->asArray();
00313                         unset($arg['@']);
00314                         $args[$key] = call_user_func_array(array(Mage::helper($helperName), $helperMethod), $arg);
00315                     } else {
00316                         /**
00317                          * if there is no helper we hope that this is assoc array
00318                          */
00319                         $arr = array();
00320                         foreach($arg as $subkey => $value) {
00321                             $arr[(string)$subkey] = (string)$value;
00322                         }
00323                         if (!empty($arr)) {
00324                             $args[$key] = $arr;
00325                         }
00326                     }
00327                 }
00328             }
00329 
00330             if (isset($node['json'])) {
00331                 $json = explode(' ', (string)$node['json']);
00332                 foreach ($json as $arg) {
00333                     $args[$arg] = Zend_Json::decode($args[$arg]);
00334                 }
00335             }
00336 
00337             $this->_translateLayoutNode($node, $args);
00338             call_user_func_array(array($block, $method), $args);
00339         }
00340 
00341         Varien_Profiler::stop($_profilerKey);
00342 
00343         return $this;
00344     }

_generateBlock ( node,
parent 
) [protected]

Enter description here...

Parameters:
Varien_Simplexml_Element $node
Varien_Simplexml_Element $parent
Returns:
Mage_Core_Model_Layout

Definition at line 217 of file Layout.php.

00218     {
00219 
00220         if (!empty($node['class'])) {
00221             $className = (string)$node['class'];
00222         } else {
00223             $className = Mage::getConfig()->getBlockClassName((string)$node['type']);
00224         }
00225 
00226         $blockName = (string)$node['name'];
00227         $_profilerKey = 'BLOCK: '.$blockName;
00228         Varien_Profiler::start($_profilerKey);
00229         $block = $this->addBlock($className, $blockName);
00230         if (!$block) {
00231             return $this;
00232         }
00233 
00234         if (!empty($node['parent'])) {
00235             $parentBlock = $this->getBlock((string)$node['parent']);
00236         } else {
00237             $parentName = $parent->getBlockName();
00238             if (!empty($parentName)) {
00239                 $parentBlock = $this->getBlock($parentName);
00240             }
00241         }
00242         if (!empty($parentBlock)) {
00243             $alias = isset($node['as']) ? (string)$node['as'] : '';
00244             if (isset($node['before'])) {
00245                 $sibling = (string)$node['before'];
00246                 if ('-'===$sibling) {
00247                     $sibling = '';
00248                 }
00249                 $parentBlock->insert($block, $sibling, false, $alias);
00250             } elseif (isset($node['after'])) {
00251                 $sibling = (string)$node['after'];
00252                 if ('-'===$sibling) {
00253                     $sibling = '';
00254                 }
00255                 $parentBlock->insert($block, $sibling, true, $alias);
00256             } else {
00257                 $parentBlock->append($block, $alias);
00258             }
00259         }
00260         if (!empty($node['template'])) {
00261             $block->setTemplate((string)$node['template']);
00262         }
00263 
00264         if (!empty($node['output'])) {
00265             $method = (string)$node['output'];
00266             $this->addOutputBlock($blockName, $method);
00267         }
00268         Varien_Profiler::stop($_profilerKey);
00269 
00270         return $this;
00271     }

_getBlockInstance ( block,
array attributes = array() 
) [protected]

Definition at line 451 of file Layout.php.

00452     {
00453         if (is_string($block)) {
00454             if (strpos($block, '/')!==false) {
00455                 if (!$block = Mage::getConfig()->getBlockClassName($block)) {
00456                     Mage::throwException(Mage::helper('core')->__('Invalid block type: %s', $block));
00457                 }
00458             }
00459             $fileName = mageFindClassFile($block);
00460             if ($fileName!==false) {
00461                 //include_once ($fileName);
00462                 $block = new $block($attributes);
00463             }
00464         }
00465         if (!$block instanceof Mage_Core_Block_Abstract) {
00466             Mage::throwException(Mage::helper('core')->__('Invalid block type: %s', $block));
00467         }
00468         return $block;
00469     }

_translateLayoutNode ( node,
&$  args 
) [protected]

Translate layout node

Parameters:
Varien_Simplexml_Element $node
array $args

Definition at line 352 of file Layout.php.

00353     {
00354         if (isset($node['translate'])) {
00355             $items = explode(' ', (string)$node['translate']);
00356             foreach ($items as $arg) {
00357                 if (isset($node['module'])) {
00358                     $args[$arg] = Mage::helper((string)$node['module'])->__($args[$arg]);
00359                 }
00360                 else {
00361                     $args[$arg] = __($args[$arg]);
00362                 }
00363             }
00364         }
00365     }

addBlock ( block,
blockName 
)

Add a block to registry, create new object if needed

Parameters:
string|Mage_Core_Block_Abstract $blockClass
string $blockName
Returns:
Mage_Core_Block_Abstract

Definition at line 436 of file Layout.php.

00437     {
00438         try {
00439             $block = $this->_getBlockInstance($block);
00440         } catch (Exception $e) {
00441             return false;
00442         }
00443 
00444         $block->setNameInLayout($blockName);
00445         $block->setLayout($this);
00446         $this->_blocks[$blockName] = $block;
00447 
00448         return $block;
00449     }

addOutputBlock ( blockName,
method = 'toHtml' 
)

Add a block to output

Parameters:
string $blockName
string $method

Definition at line 503 of file Layout.php.

00504     {
00505         //$this->_output[] = array($blockName, $method);
00506         $this->_output[$blockName] = array($blockName, $method);
00507         return $this;
00508     }

createBlock ( type,
name = '',
array attributes = array() 
)

Block Factory

Parameters:
string $type
string $blockName
array $attributes
Returns:
Mage_Core_Block_Abstract

Definition at line 399 of file Layout.php.

00400     {
00401         try {
00402             $block = $this->_getBlockInstance($type, $attributes);
00403         } catch (Exception $e) {
00404             Mage::logException($e);
00405             return false;
00406         }
00407 
00408         if (empty($name) || '.'===$name{0}) {
00409             $block->setIsAnonymous(true);
00410             if (!empty($name)) {
00411                 $block->setAnonSuffix(substr($name, 1));
00412             }
00413             $name = 'ANONYMOUS_'.sizeof($this->_blocks);
00414         }
00415         elseif (isset($this->_blocks[$name])) {
00416             Mage::throwException(Mage::helper('core')->__('Block with name "%s" already exists', $name));
00417         }
00418 
00419         $block->setType($type);
00420         $block->setNameInLayout($name);
00421         $block->addData($attributes);
00422         $block->setLayout($this);
00423 
00424         $this->_blocks[$name] = $block;
00425 
00426         return $this->_blocks[$name];
00427     }

generateBlocks ( parent = null  ) 

Create layout blocks from configuration

Parameters:
Mage_Core_Layout_Element|null $parent

Definition at line 183 of file Layout.php.

00184     {
00185         if (empty($parent)) {
00186             $parent = $this->getNode();
00187         }
00188         foreach ($parent as $node) {
00189             $attributes = $node->attributes();
00190             if ((bool)$attributes->ignore) {
00191                 continue;
00192             }
00193             switch ($node->getName()) {
00194                 case 'block':
00195                     $this->_generateBlock($node, $parent);
00196                     $this->generateBlocks($node);
00197                     break;
00198 
00199                 case 'reference':
00200                     $this->generateBlocks($node);
00201                     break;
00202 
00203                 case 'action':
00204                     $this->_generateAction($node, $parent);
00205                     break;
00206             }
00207         }
00208     }

generateXml (  ) 

Loyout xml generation

Returns:
Mage_Core_Model_Layout

Definition at line 153 of file Layout.php.

00154     {
00155         $xml = $this->getUpdate()->asSimplexml();
00156         $removeInstructions = $xml->xpath("//remove");
00157         foreach ($removeInstructions as $infoNode) {
00158             $attributes = $infoNode->attributes();
00159             if ($acl = (string)$attributes->acl && !Mage::getSingleton('admin/session')->isAllowed($acl)) {
00160                 $block->addAttribute('ignore', true);
00161             }
00162             if ($blockName = (string)$attributes->name) {
00163                 $ignoreNodes = $xml->xpath("//block[@name='".$blockName."']");
00164                 foreach ($ignoreNodes as $block) {
00165                     $block->addAttribute('ignore', true);
00166                 }
00167                 $ignoreNodes = $xml->xpath("//reference[@name='".$blockName."']");
00168                 foreach ($ignoreNodes as $block) {
00169                     $block->addAttribute('ignore', true);
00170                 }
00171             }
00172         }
00173 
00174         $this->setXml($xml);
00175         return $this;
00176     }

getAllBlocks (  ) 

Retrieve all blocks from registry as array

Returns:
array

Definition at line 477 of file Layout.php.

00478     {
00479         return $this->_blocks;
00480     }

getArea (  ) 

Retrieve layout area

Returns:
string

Definition at line 121 of file Layout.php.

00122     {
00123         return $this->_area;
00124     }

getBlock ( name  ) 

Get block object by name

Parameters:
string $name
Returns:
Mage_Core_Block_Abstract

Definition at line 488 of file Layout.php.

00489     {
00490         if (isset($this->_blocks[$name])) {
00491             return $this->_blocks[$name];
00492         } else {
00493             return false;
00494         }
00495     }

getBlockSingleton ( type  ) 

Enter description here...

Parameters:
string $type
Returns:
Mage_Core_Helper_Abstract

Definition at line 552 of file Layout.php.

00553     {
00554         if (!isset($this->_helpers[$type])) {
00555             if (!$className = Mage::getConfig()->getBlockClassName($type)) {
00556                 Mage::throwException(Mage::helper('core')->__('Invalid block type: %s', $type));
00557             }
00558 
00559             $helper = new $className();
00560             if ($helper) {
00561                 if ($helper instanceof Mage_Core_Block_Abstract) {
00562                     $helper->setLayout($this);
00563                 }
00564                 $this->_helpers[$type] = $helper;
00565             }
00566         }
00567         return $this->_helpers[$type];
00568     }

getDirectOutput (  ) 

Retrieve derect output flag

Returns:
bool

Definition at line 143 of file Layout.php.

00144     {
00145         return $this->_directOutput;
00146     }

getMessagesBlock (  ) 

Retrieve messages block

Returns:
Mage_Core_Block_Messages

Definition at line 538 of file Layout.php.

00539     {
00540         if ($block = $this->getBlock('messages')) {
00541             return $block;
00542         }
00543         return $this->createBlock('core/messages', 'messages');
00544     }

getOutput (  ) 

Get all blocks marked for output

Returns:
string

Definition at line 521 of file Layout.php.

00522     {
00523         $out = '';
00524         if (!empty($this->_output)) {
00525             foreach ($this->_output as $callback) {
00526                 $out .= $this->getBlock($callback[0])->$callback[1]();
00527             }
00528         }
00529 
00530         return $out;
00531     }

getUpdate (  ) 

Layout update instance

Returns:
Mage_Core_Model_Layout_Update

Definition at line 96 of file Layout.php.

00097     {
00098         if (!$this->_update) {
00099             $this->_update = Mage::getModel('core/layout_update');
00100         }
00101         return $this->_update;
00102     }

helper ( name  ) 

Retrieve helper object

Parameters:
string $name
Returns:
Mage_Core_Helper_Abstract

Definition at line 576 of file Layout.php.

00577     {
00578         $helper = Mage::app()->getHelper($name);
00579         if (!$helper) {
00580             return false;
00581         }
00582         return $helper->setLayout($this);
00583     }

removeOutputBlock ( blockName  ) 

Definition at line 510 of file Layout.php.

00511     {
00512         unset($this->_output[$blockName]);
00513         return $this;
00514     }

setArea ( area  ) 

Set layout area

Parameters:
string $area
Returns:
Mage_Core_Model_Layout

Definition at line 110 of file Layout.php.

00111     {
00112         $this->_area = $area;
00113         return $this;
00114     }

setBlock ( name,
block 
)

Save block in blocks registry

Parameters:
string $name
Mage_Core_Model_Layout $block

Definition at line 373 of file Layout.php.

00374     {
00375         $this->_blocks[$name] = $block;
00376         return $this;
00377     }

setDirectOutput ( flag  ) 

Declaring layout direct output flag

Parameters:
bool $flag
Returns:
Mage_Core_Model_Layout

Definition at line 132 of file Layout.php.

00133     {
00134         $this->_directOutput = $flag;
00135         return $this;
00136     }

unsetBlock ( name  ) 

Remove block from registry

Parameters:
string $name

Definition at line 384 of file Layout.php.

00385     {
00386         $this->_blocks[$name] = null;
00387         unset($this->_blocks[$name]);
00388         return $this;
00389     }


Member Data Documentation

$_area [protected]

Definition at line 63 of file Layout.php.

$_blocks = array() [protected]

Definition at line 49 of file Layout.php.

$_directOutput = false [protected]

Definition at line 77 of file Layout.php.

$_helpers = array() [protected]

Definition at line 70 of file Layout.php.

$_output = array() [protected]

Definition at line 56 of file Layout.php.

$_update [protected]

Definition at line 42 of file Layout.php.


The documentation for this class was generated from the following file:

Generated on Sat Jul 4 17:23:57 2009 for Magento by  doxygen 1.5.8