Template editing - Magento Product Attributes

Magento Product Attributes are available to use in the template. 


Attributes defined in Magento have an Attribute Code and an Attribute Label. The screen shot shows a few of the Attributes defined in the Magento sample data. 

Attributes available as a data set

A dataset called AttributeTable is available to the template.

Render all the attributes as a table


That will just output all of the attributes as a table, with Label and Value as headers. This is standard behaviour for the template engine. Given a set without any specific instructions will just render a table.

Explicitly iterating

<ul><br>  {for AttributeTable}<br>    <li>{Label} - {Value}</li><br>  {/for}<br>/ul>

That will create an unordered list of all all attributes, where each list item will be rendered as Label - Value

Conditions in the iteration

It is possible to take control of the iteration by using the conditions.

That will create an unordered list of all all attributes, where each list item will be rendered as Label - Value. 

<ul><br>  {for AttributeTable}<br>    {if Value}<br>      <li>{Label} - {Value}</li><br>    {/if}<br>    {/for}<br></ul>

Same as the example above, but all attributes without a value will be skipped.

Groups exposed as sets

When attributes are grouped in Magento, the group name is used to create a set of attributes for that group. It's the same as the complete set, just append the group name.


That will render the attributes in the electronics group as a table.

{set attributegroup="Electronics"}
<ul><br>    {for AttributeTable}<br>    <li>{Label} - {Value}</li><br>    {/for}<br></ul><br>{/set}

By setting the attributegroup variable before running the iteration, you filter the set and have control over how the set is rendered by using explicit iteration. 

Template Tag options

The Attribute data is exposed in a number of different ways. Which option you choose will depend on what data the attribute contains and what your are trying to achieve.

Using accessories_size as an example:

Tag Use
{accessories_size} Output the raw data. If the attribute value contains HTML, the HTML tags will be shown
{include field="accessories_size"} Render the data. If the attribute value contains HTML, it will be displayed as intended.

Conditional Rendering

The template execution supports conditional rendering.

Render only if data present

If you only want to output something if the Attribute Value contains some text, use the following syntax:

{if accessories_size}
	The size is {include field="accessories_size"}
Why append HTML to the tag?

As the attribute can contain HTML it needs to be executed on the template, not just rendered. So given the above example {AccessoriesSize} is actually a macro for (include field="AccessoriesSize"}. 

Render based on values

If you want to render something different is the Attribute Value contains specific data, you can use the following syntax:

{if AccessoriesSizeHTML = "8"}The Size is Eight{else}PAccessoriesSize}{/if}

Rendering of HTML

The tags shown in the Template Tags section above will automatically cause any HTML in the Attribute Values to be displayed correctly. If you want the raw HTML to be rendered onto the page instead, just append HTML to the tag name. For example instead of {AccessoriesSize} use {AccessoriesSizeHTML}. This will cause any HTML elements in the Attribute Value to be shown.

<< See all Channel Cloud articles