Skip to main content

FAQ: Using Twig Markup for Acenda Admin Tools

S
Written by Sam E.

Twig is the templating language behind two places in Acenda: the Value Type tabs on your Posting Templates (also used in Generic Templates and Commonality rules), and the body of a handful of recurring export feeds. Twig lets you pull in platform data, apply conditional logic, and format the result exactly the way a channel or feed expects.


Where you write Twig in Acenda

You'll find Twig most often in a Posting Template: under CHANNELS > Integrations, click Edit on a sales channel, open its Posting Templates, and pick an attribute. Each attribute's value has String, Property and Twig tabs — Twig is where conditional logic and formatting live. The same three tabs appear in Generic Templates and in Commonality rules.

A smaller number of recurring export feeds (for example a Ship Advice Export or Order Export integration) also take a Twig template for the body of the file they produce. These live on that integration's own Edit screen rather than in Posting Templates. Most sellers won't need to touch one of these directly — if you're not sure whether yours has one, contact Acenda Support and we can point you to it.

Whichever screen you're on, use the preview pane next to the editor to see exactly what your formula produces before you save — including a plain-English error message if something doesn't parse.


Key Twig Concepts in Acenda Templates

Concept

Syntax Example

Description

Output Data

{{ variable.field }}

Used to display the value of a variable or a field from the Acenda data structure.

Control Structures

{% if condition %} ... {% endif %}

Used to execute code blocks conditionally (e.g., if, for, set).

Filters

{{ field|lower }}

Used to transform a value on its way out, such as lowercasing text or rounding a number.

Set Variables

{% set var = "value" %}

Used to create or update a variable for use later in the template.

Whitespace Control

{% apply spaceless %} ... {% endapply %}

Used to remove unnecessary whitespace between HTML tags or data elements, which is useful for clean data feeds.

Comments

{# This is a comment #}

Used to document your code; it is ignored when the template is rendered.

Useful filters: raw (output real HTML/characters instead of escaped text), lower (lowercase), length (count of an array or string), round(2) (round a number to a given number of decimals), default('N/A') (fall back to a value when the variable is empty).

Variables you'll see most often — which ones are available depends on where you're writing Twig:

  • In a Posting Template or Generic Template: product.sku, product.variant_options (the parent's variant axes, e.g. color/size), and variant.fields.<name> for a custom field on the specific variant.

  • In an order or ship advice export feed: order.shipping_information.first_name / .last_name and other shipping fields, plus ship_advice.sales_channel_id and ship_advice.ship_advice_item (a list of line items, each with its own .sku, .quantity, .unit_price, etc.).

Heads up: A sales channel's numeric ID (sales_channel_id) is assigned when that channel is connected on your account, so the same channel can have a different ID from one Acenda account to the next. Don't hardcode "1 means Walmart" — open CHANNELS > Integrations and check the ID for your own account before branching on it.


Example 1: Mapping Data with Conditional Logic (Posting Template)

This example shows how to correctly map the color variant from your Acenda product data to a specific field in a target platform's posting template, checking multiple possible variant option locations.

{% apply spaceless %}
{% if product.variant_options[0].name|lower == 'color' or product.variant_options[1].name|lower == 'color' %}
{% if product.variant_options[0].values|length > 1 and product.variant_options[0].name|lower == 'color' %}
{{ variant.fields.color|raw }}
{% elseif product.variant_options[1].values|length > 1 and product.variant_options[1].name|lower == 'color' %}
{{ variant.fields.color|raw }}
{% endif %}
{% else %}
{% endif %}
{% endapply %}

Key Takeaways:

  • {% apply spaceless %}: Ensures the output is compact, which is often required for posting templates.

  • product.variant_options[0].name|lower == 'color': Checks if the first variant option's name (converted to lowercase) is 'color'. The index [0] refers to the first option and [1] to the second.

  • product.variant_options[0].values|length > 1: Ensures that this option actually has variants (more than one value), preventing output if it's not a real variant.

  • {{ variant.fields.color|raw }}: Outputs the specific color value for the current variant and uses the raw filter to ensure special characters aren't escaped.


Example 2: Creating a Recurring Export Order Feed

This example demonstrates setting variables and iterating through items to create a structured order feed.

{% apply spaceless %}
{% if ship_advice.sales_channel_id == 101 %}
{% set account_code = "WMT" %}
{# ... other elseif blocks for your other sales channels ... #}
{% elseif ship_advice.sales_channel_id == 104 %}
{% set account_code = "TGT" %}
{% endif %}

{% set ship_first_name = order.shipping_information.first_name %}
{# ... logic to combine first name and company if necessary ... #}

{% set ship_last_name = order.shipping_information.last_name %}
{% if order.shipping_information.last_name == "NA" %}
{% set ship_last_name = '' %}
{% endif %}
{% endapply %}

{# Shipping Info #}{{ ship_first_name }}
{{ ship_last_name }}
{# ... outputting other shipping fields ... #}

{% for item in ship_advice.ship_advice_item %}
{% apply spaceless %}
{% set adjusted_price = item.unit_price - (item.total_item_discount) / item.quantity %}
{% endapply %}
{{ item.sku }}|{{ item.product_name }};Sku: {{ item.sku }};Model#: {{ item.sku }}|{{ item.quantity }}|{{ item.unit_price }}|{{ item.sku }}|{{ adjusted_price|round(2) }}
{% endfor %}

Key Takeaways:

  • Setting Variables: {% set account_code = "WMT" %} stores a value based on a condition, making the final output cleaner. (The numeric IDs above are examples only — use your own account's sales_channel_id values.)

  • Conditional Formatting: The logic for ship_first_name and ship_last_name cleans up the data before output (e.g., removing "NA").

  • Looping: {% for item in ship_advice.ship_advice_item %} iterates over each line item in the order, allowing you to output repeated data blocks for the feed.

  • Calculations: {% set adjusted_price = item.unit_price - (item.total_item_discount) / item.quantity %} shows you can perform basic math within the template.

  • {{ adjusted_price|round(2) }}: The round(2) filter ensures the price is formatted to two decimal places.


Using an AI Assistant with Twig

ChatGPT (or a similar AI assistant) can be a powerful co-pilot for writing and debugging your Twig formulas. If you already use an AI assistant with Acenda, see Connect Your AI Assistant to Acenda for what that connection currently supports.

  1. Generate Initial Code: Provide a clear description of the data you need to access and the logic required.

    Example Prompt: "Write me a Twig formula for an Acenda posting template. I need to check if a product's custom field called 'material' is not empty. If it has a value, output it. Otherwise, output 'N/A'."

  2. Translate Logic to Twig: If you know the logic in plain English or another language (like PHP or Python), ask your assistant to translate it into Acenda-specific Twig syntax.

  3. Debugging/Reviewing: Paste your existing Twig code and the problem you're encountering.

    Example Prompt: "This Twig formula isn't outputting anything. I'm trying to access the product SKU. The field is called product.sku. Can you review this for errors? {% if product.sku is not null %}{{ sku }}{% endif %}" (A good assistant would point out that the output variable should be product.sku, not sku.)

  4. Formatting and Filtering: Ask for the correct filters to achieve a desired format.

    Example Prompt: "How do I use a Twig filter to ensure a price variable is always shown with two decimal places in Acenda?"


Troubleshooting & FAQ

My formula outputs nothing. Twig doesn't raise an error for an unknown or empty variable — it just renders blank. Double-check the variable path (for example variant.fields.description, not fields.variant.description), and use the preview pane to see exactly what your formula returns before you save.

A mapped field renders blank — will that push an empty value to the channel? No. When a mapped attribute resolves to nothing, Acenda leaves that attribute out of what it sends rather than pushing a blank value, so the channel keeps whatever it already has for that attribute until the field is filled in and re-rendered.

My HTML tags are showing up as plain text instead of real formatting. Twig escapes HTML by default. Add the raw filter to output real markup, for example {{ product.fields.description|raw }}. Keep in mind Acenda itself doesn't strip HTML out of what you send — but some channels do. See What HTML tags can sellers use on their Target+ product descriptions? if you're troubleshooting Target+ specifically.

There's extra whitespace or blank lines in my output. Wrap the block in {% apply spaceless %} ... {% endapply %} to strip whitespace between tags and lines.

My template has a syntax error. The preview pane shows the error message directly, without saving — fix it there first rather than guessing from the saved output.

Didn't find what you need here? Contact Acenda Support and we can help with your specific template.


Did this answer your question?