Skip to main content

FAQ: Using Twig Markup for Acenda Admin Tools

S
Written by Sam E.
Updated over 3 weeks ago

This guide provides an overview of using the Twig markup language within the Acenda admin tool, particularly for product posting templates and data export feeds. Twig is a flexible, fast, and secure templating engine that allows you to dynamically insert and manipulate data from your Acenda platform.


What is Twig, and why is it used in Acenda?

Twig is a modern template language for PHP. In Acenda, it allows you to:

  • Access and display platform data (like product details, order information, and variant options).

  • Apply logic (like if/elseif/else statements) to dynamically format data based on specific conditions.

  • Manipulate data using various filters (like lower, length, round) and functions.

When creating a feed or a posting template, the Twig code you write determines exactly what data is included and in what format.


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 }}`

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.


πŸ’‘ 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.

Twig

{% 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.

Twig

{% apply spaceless %}
{% if ship_advice.sales_channel_id == 1 %}
{% set account_code = "WMT" %}
{# ... other elseif blocks for different sales channels ... #}
{% elseif ship_advice.sales_channel_id == 4 %}
{% 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.

  • 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.


How ChatGPT Can Help with Twig Formulas

ChatGPT (or similar large language models) can be a powerful co-pilot for writing and debugging your Twig formulas.

  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 ChatGPT 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 %}" (ChatGPT would point out that the output variable should be product.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?"

Did this answer your question?