How can we help?

Advanced Reusable HTML Content Usage

At times you will find the need to run complicated smarty logic in your email. We have found it is 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.

As an example, let's say you would like 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.

To accomplish this using the Advanced method, you would start with the HTML Content. Here is 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)}

Now that we have the complex code complete in an Include, we can use this Include in our message.  Notice in our sample code 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 is 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.