This is a pretty easy one, but it’s something that can come in very handy at times.
Say you want to load a particular script on the page, but only if a particular block loads on that page. As it stands, Magento’s core/template blocks don’t support this.
To do it, include this function from within your custom module’s Block file in app/code/local/Namespace/Modulename/Block
public function addItem($type, $path)
{
$head = $this->getLayout()->getBlock('head');
return $type == 'css' ? $head->addCss($path) : $type == 'javascript' ? $head->addJs($path) : $this ;
}
Then to use it, just include a call to this function from within your block declaration in the layout:
<block type="modulename/blockname" name="modulename.blockname" as="blockname" template="path/to/template/file.phtml">
<action method="addItem"><type>css</type><path>css/path/to/file.css</path></action>
<action method="addItem"><type>javascript</type><path>path/to/file.js</path></action>
</block>
When that block is constructed, it will run the function and your custom scripts will load in the head.
Say you want to load a particular script on the page, but only if a particular block loads on that page. As it stands, Magento’s core/template blocks don’t support this.
To do it, include this function from within your custom module’s Block file in app/code/local/Namespace/Modulename/Block
public function addItem($type, $path)
{
$head = $this->getLayout()->getBlock('head');
return $type == 'css' ? $head->addCss($path) : $type == 'javascript' ? $head->addJs($path) : $this ;
}
Then to use it, just include a call to this function from within your block declaration in the layout:
<block type="modulename/blockname" name="modulename.blockname" as="blockname" template="path/to/template/file.phtml">
<action method="addItem"><type>css</type><path>css/path/to/file.css</path></action>
<action method="addItem"><type>javascript</type><path>path/to/file.js</path></action>
</block>
When that block is constructed, it will run the function and your custom scripts will load in the head.
No comments:
Post a Comment