Using Arrays in email template in Magento

Using Arrays in email template in Magento

August 9, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Using Arrays in email template in Magento

Accessing Array Contents in Magento Email Templates

When working with email templates in Magento, you might find yourself needing to pass an array as a template parameter. However, using this array directly in your template can lead to unexpected results like displaying the word “Array” instead of the actual content.

In your code example, you’re passing an array called $someArray (or sa) to the email template. To access the contents of this array in your template, you need to know how to reference its elements properly.

Understanding Template Variables

In Magento templates, variables are referenced using the {{var}} syntax followed by the variable name. This works fine for simple scalar values like strings or integers. However, when dealing with arrays, things get a bit more complex.

When an array is passed as a template parameter, it’s available to the template under the reference name you provided (sa in your case). But simply using {{var sa}} won’t work because Magento doesn’t know how to display the contents of an array by default. It will just show the word “Array”.

Accessing Array Elements

To access elements within the array, you need to use the dot notation (->) or the bracket notation ([]). Here’s how you can do it:

  1. Dot Notation: If your array keys are strings and contain no spaces, you can reference them using the dot notation like this:

    {{var sa.key}}
    

    For example, if sa is an array with a key called “item”, you would access its value as:

    <p>Order Item: {{var sa.item}}</p>
    
  2. Bracket Notation: If your array keys are numeric or contain spaces, you need to use the bracket notation:

    {{var sa['key']}}
    

    For instance, if sa is an array with a key called “item 1”, you would access its value as:

    <p>Order Item 1: {{var sa['item 1']}}</p>
    

Best Practices

When working with arrays in Magento templates:

  • Keep your array keys simple and meaningful. Avoid using spaces or special characters.
  • If you need to use a key that would require bracket notation, consider renaming it to something simpler.

Remember, the array will be available to all blocks within the template unless you specify otherwise. If you want to limit its availability, you can pass it as a block parameter instead of a global template parameter.

Conclusion

Accessing array contents in Magento email templates requires knowing how to reference array elements properly. By using the correct notation (-> or []), you can display the actual values from your arrays instead of seeing “Array” in the final email.

If you find yourself needing to work with complex data structures like arrays frequently, consider exploring Magento’s object serialization features for more advanced handling and manipulation of these structures within your templates Learn more about object serialization at Laravel Company blog.

Themes Email Pricing

Our network