How can we help?

Advanced reusable HTML content usage


At times, you'll find the need to run complicated Smarty logic in your email. We've found that it's often easier to extract the Smarty code logic into it's own HTML content include—and then keep the message content to mostly HTML with a few Smarty variables that were built up in the HTML content.

Example use case

As an example, let's say you want to loop through all of the items in a cart abandonment message and only use one of the cart items in the message. Additionally, let's say you need to look the cart item up in your product catalog to get the current image URL.

1. To accomplish this using the advanced method, you would start with the HTML content. Here's an example:

{$data.cart_item.itemPrice = 0}
{$data.has_cart = 0}
{foreach $contact.cart.cartitems as $cartitem}
  {if $data.cart_item.itemPrice < $cartitem.itemPrice}
    {$data.cart_item = $cartitem}
    {$data.has_cart = 1}

{if $data.has_cart == 0}
  {$data.cart_item.productID = 'some_default_product'}
{$product = $products[0]}
{$data.cart_item.url = $product.url}
{$data.cart_item.images = $product.images}
{if $data.has_cart == 0} {$ = $}
{$data.cart_item.itemPrice = $product.itemPrice} {/if} <h1>Debug Data</h1> {$utils->jsonPrettyPrint($contact)} <h1>Debug Contact</h1> {$utils->jsonPrettyPrint($contact)}

2. Now that we have the complex code complete in an include, we can use this include in our message.

  • Notice in our sample code that we scope to parent, which means execute the Include before the message. 
  • Additionally, we assign the HTML output to a variable called blackhole. This variable is never used, which means we will not use the HTML output of the include, only the $data variable that is built up.

Here's an example:

{include 'content:cart_data' scope='parent' assign='blackhole'}
<img src="{$data.cart_item.images[0]}"><br>
{$} ${$data.cart_item.itemPrice}



Please sign in to leave a comment.