Feed Syntax: Common Directives and Operators

Common Directives

For a complete list of directives, read the Directives section of the Template Toolkit manual.

GET

The most common directive available is GET. The GET directive simply retrieves and outputs the value of the named variable. GET is special in that the GET keyword is actually optional. The two tags shown below are identical:

{ GET sku.sku_num }
{ sku.sku_num }

You can also create expressions using logical and mathematic operators (see below):

{ family.brand || product.brand }
{ (sku.msrp_price.dollars - sku.unit_cost.dollars) / sku.msrp_price.dollars }

SET

Another common directive is SET. The SET directive allows you to assign new values to existing variables or to create new temporary variables. Like the GET directive, SET is special in that the SET keyword is actually optional. The two tags below are identical. Note that a single equal sign (=) must be used to set a new value for a variable.

{ SET name = product.name }
{ name = product.name}

Variables may be assigned the values of attributes, other variables, unquoted numbers, or literal text ('in single quotes').

Like the GET directive, you may create expressions using logical and mathematic operators:

{ keywords1  = attr.Specialty || attr.FabricType }
{ price = sku.unit_cost.dollars / 0.4 }

Conditional Processing: IF/UNLESS/ELSE/ELSIF

Conditional processing directives are used to process blocks based on a condition that you set. A simple IF statement might look like this:

{ IF family.num_skus > 1 }Size and Flavor{ END }

In the example above, family.num_skus > 1 is the condition. If the product meets that condition--i.e. if the product's family contains more than one SKU--the value "Size and Flavor" will be output. Note that for all conditional processing directives, the statement must have a matching END directive.

The UNLESS directive is the opposite of IF. Let's look at the same example but swapping out IF for UNLESS:

{ UNLESS family.num_skus > 1 }Size and Flavor{ END }

In this example, if a product meets the condition set, it will be excluded from outputting "Size and Flavor".

IF and UNLESS directives can be combined with ELSE and ELSIF statements to set multiple conditions with different outputs for each condition. Each directive will be evaluated in the order entered in the block.

{ IF family.num_skus > 5 }Size and Flavor{ ELSIF family.num_skus > 1 }Size{ ELSE }Flavor{ END }

Note that for IF and ELSIF directives, you must specify a condition. ELSE acts as a backstop, affecting any products that do not meet any of the IF and ELSIF conditions.

To set conditions, you may use any comparison or logical operators. You should also note that you may output other attributes or variables, not just static values.

{ IF family.num_skus > 5 && attr.Size != 'N/A' }{ attr.Size || attr.Flavor }{ ELSIF family.num_skus > 1 || attr.Flavor == 'N/A'}{ attr.Size }{ ELSE }Flavor{ END }

Common Operators

Look to the table below for a list of the most common operators used and their respective functions. For more on operators, refer to this Perl operators documentation.

Operator
Type
Function
+
mathematic
addition
-
mathematic
subtraction
*
mathematic
multiplication
/
mathematic
division
==
comparison
equals
!=
comparison
does not equal
<
comparison
less than
>
comparison
greater than
<=
comparison
less than or equal
>=
comparison
greater than or equal
&&
logical
AND
||
logical
OR
!
logical
NOT

Conditional Processing: Ternary Operator

The ternary operator, represented by a question mark and colon (? :), functions in a similar fashion to the conditional directives shown above. A simple use of the ternary operator might look like this:

{ attr.Keywords ? 'mens fashion' : 'womens fashion' }

In the example above, the portion before the question mark is the condition being evaluated. If it's true for the product--that is, if there is any value for attr.Keywords--RO will output the value to the left of the colon; if it's not true, RO will output the value to the right.

Like the conditional directives above, you may use any comparison or logical operators to set the condition. You may also output different attributes or variables.

Have more questions? Submit a request

0 Comments

Please sign in to leave a comment.
Powered by Zendesk