Skip to content

Instantly share code, notes, and snippets.

@astorm
Created September 2, 2011 13:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save astorm/1188644 to your computer and use it in GitHub Desktop.
Save astorm/1188644 to your computer and use it in GitHub Desktop.
Moving a Magento Block Object
<layout>
<catalog_category_default>
<!-- block gets added -->
<reference name="content">
<block type="core/text" name="are_you_sure"></block>
</reference>
</catalog_category_default>
<catalog_category_default>
<!-- later, a different handle gets a reference and sets block state -->
<reference name="are_you_sure">
<action method="setText"><text>Hello World</text></action>
</reference>
</catalog_category_default>
<catalog_category_default>
<!-- we want to move a block, so we unset where it is -->
<reference name="content">
<action method="unsetChild"><child>are_you_sure</child></action>
</reference>
</catalog_category_default>
<catalog_category_default>
<!-- WRONG WAY: we re-add the block, but we've lost state -->
<reference name="right">
<block type="core/text" name="are_you_sure"/>
</reference>
</catalog_category_default>
<catalog_category_default>
<!-- Instead, use the insert method. This method accepts both a block object
OR a string. If you pass it a string, it gets a refernce to the block by that
name. The block, although unset, still exists on the layout object, which is why
this works -->
<reference name="right">
<action method="insert"><block>are_you_sure</block></action>
</reference>
<!--
if (is_string($block)) {
$block = $this->getLayout()->getBlock($block);
}
-->
</catalog_category_default>
</layout>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment