1

I don't know what 'as' really means in the context of layout XML files.

For instance, I could add in something like to some product page:

<block type="custom/whatever" name="custom.whatever.block" template="whatever.phtml" />

Or, I could blindly copy what generally happens and put this together:

<block type="custom/whatever" name="custom.whatever.block" as='supercalifragilisticexpialidocious' template="whatever.phtml" />

Then in my product page template I could do this:

<?php echo $this->getChildHtml('supercalifragilisticexpialidocious') ?>

Or I could do:

<?php echo $this->getChildHtml('custom.whatever.block') ?>

With a fair expectation that my custom/whatever block will render on the page using the whatever.phtml template regardless of whether I use the full name or the 'as' alias.

I am sure this question has been asked before, however, 'as' is one of those words that doesn't really suit Bing! or other search engines particularly well.

3 Answers 3

5

As per Magento Standard

as Block alias : An alias name by which a template calls the block in which this attribute is assigned. Sometimes it's necessary to specify the alias for a specific block type.

For Example

<reference name="content">
    <block type="cms/block" name="additional.info" as="additionalInfo">
         <action method="setBlockId"><id>additional_info</id></action>
    </block>
</reference>

In your phtml file you call using.

<?php echo $this->getChildHtml('additionalInfo') ?>
3

as - This is the name by which a template calls the block in which this attribute is assigned. When you see the getChildHtml('block_name') PHP method called from a template, you can be sure it is referring to the block whose attribute 'as' is assigned the name 'block_name'.

ie. The method <?=$this->getChildHtml('header')?> in the a skeleton template correlates to <block as="header"> )

See Intro to Layouts

1

A general block that is using in layout file will look like this.

<block type="custom/whatever" name="custom.whatever.block" as='useme' template="whatever.phtml" />

I will go through each attribute in short now

1. type :- This is the most important part of a block. Without specifying a type, a block will not become a real block !. Type points to the actual behaviour of a particular block. This means blocks with different type behaves differently and they dont have any relation between themselves except they may be inherits from same parent classes. When we specify a type of a block, it is required that, it is defined backside of the module. For an example type="core/template" needs Mage_Core_Block_Template class to be defined. Otherwise Magento will not identify your block.

2. name :- This attribute is clear by its "name" itself. We are giving a name for this block in order to identify this block uniquely. You need to always use a unique name for your block. The use of name of a block is very much important to understand. Suppose you need to use above mentioned block to be used further inside any other layout xml files, then in that case, we will "refer" the above block by its name. ie <reference name="custom.whatever.block" />. Note that name of the block is actually using everywhere for future reference.

3. as :- This is the alias name of a block. as value is mainly using in phtml file in order to call a block. Please note, if you use name field to refer a block inside a phtml file, it simply wont work. I think as is perfect to denotation for alias name. You can read it as use this block as "useme" in a phtml file (as per above example).

A block is invoked in a phtml like this

 <?php echo $this->getChildHtml('useme'); //useme is `as` value.

Now magento will get the block and render its content in that place.

4. template :- This is used to set a template file to a block. template file will hold design part of a block. A template file is actually an instance of type of the block to which that block is connected. In the above case whatever.phtml will be an instance of Namespace_Modulename_Block_Whatever where Namespace_Modulename is hypothetical custom module that holds this block.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.