What Is The Difference Between The “name” and “as” Attributes For Blocks in Magento?

What Is The Difference Between The “name” and “as” Attributes For Blocks in Magento?

Before answering the question mentioned in the title, we should learn the function of every attribute.

“name” is a block identifier in XML layout with the help of which a block can be applied. “name” attribute should be unique within a page. It is appropriate to compare this attribute with “id” for HTML tags. If we do not assign the attribute, it is assigned automatically by the system in the ANONYMOUS_n format (“n” is a sequence element number with unspecified “id” attribute).

“as” assigns a moniker under which this block is identified within its parent. The attribute should be unique within a parent block. As a rule, “as” is shorter than “name” that is considered to be convenient when calling a block in template files. If we do not assign “as” attribute, it is automatically equal to “name” attribute.

Let’s consider the example of using “as” attribute when calling a block in the template. In the picture below there is a block which outputs a link at the top of the site. There are the following attributes name=”top.links” and as=”topLinks”.

What is the difference between the name and as attributes for blocks_1

In order to output the block in the header of the template, we apply the block call construction. Here as an identifier, we should apply “as” attribute.

What is the difference between the name and as attributes for blocks_2

In the example given “name” and “as” have the same length and probably the reason to use “as” seems unclear, but when the length of the “name” attribute gets longer, it gets apparent the necessity of “as” usage.

What is the difference between the name and as attributes for blocks_3

To sum up, “name” attribute is a unique block identifier (as a rule, it’s long and exhaustive), but “as” attribute is a moniker (it is short as a rule), which is used when calling a block in template files. The main function of “as” attribute is simplifying blocks.

What is the processing order of layout XML instructions?

In Magento there is the following order of layout XML instructions (from highest priority to lowest):

  • User XML layout in admin panel;
  • Custom theme layout (current theme);
  • Default theme layout (parent theme);
  • Base theme layout (basic theme).

It should be mentioned that within layout files user handles will have a higher priority than default handle. In other words, XML layout is processed in the same order as fallback stages starting from current theme and finishing with basic one (you can find additional info here).  You should comprehend that layouts with a higher priority not always should override layouts with a lower priority. As a rule, it is enough when they’re extended (you can find additional info here). Since Magento form the ultimate layout by using all layout files according to the priorities described above.



Post a new comment

top