Twig

  1. Reference book of available twig-filters
  2. Reference book of available twig-functions
  3. Reference book of available object fields
  4. Canceling of the template rendering
  5. Frequently occurring problems

Twig-templates are used for preparing of printed forms, letters, SMS templates and notifications. They look like HTML-layout with inclusions of twig-markup, so that it is needed basic knowledge of html and twig for creating templates. As a rule, there are number of templates in the system, which you can use as a base.

Read also:

In twig there are following basic language constructions:

In the functional block are used language tags like {% tag %} ... {% endtag %}:

The official twig documentation provides more detailed information about working with it.

Below there are reference book of по twig-filters, twig-functions and objects available in system templates.

Reference book of available twig-filters

Filters can be applied to variable through vertical line. Examples of filter usage:

{{ name|striptags|title }}
{{ list|join(', ') }}

In templates are available most part of standart filters and some special filters. Full list of available filters you may see below.

Reference book of available twig-functions

Example of function range usage:

{% for i in range(0, 3) %}
    {{ i }},
{% endfor %}

Similarly with filters in templates are available most part of standart functions and some special functions. Full list of available functions you may see below.

Reference book of available object fields and methods

The particular set of objects is transmitted into template depending on its type. It is possible to retrieve and output data from objects referring to object fields and methods.

Read Full objects list.

Example of references:

{% if order.fromApi %}
    {{ order.getNickName() }}
{% endif %}

Objects are linked by fields, i.e. referring to field of one object you can get another object. In such cases description of field in reference book is a link to object, which is returned with reference to field.

Example of references:

{% if order.getDeliveryType().getCode() == 'russian-post' %}
    {{ order.trackNumber }}
{% endif %}

Canceling of the template rendering

There can be situation when it is necessary to cancel the template generating. For example, it is needed to send the letter with recommended items by trigger, but if there are no reccomendations found for customer, then it is necessary to cancel the template generating and sending. For cancellation you should use {% cancel %} tag.

Example of usage:

{% set recom = best_selling_products() %}
{% if recom|length > 0 %}
  {# displaying recommended items #}
{% else %}
  {% cancel %}
{% endif %}

If template has been cancelled, it will be fixed at action log:

You can specify the message, which will be displayed at action log in tag.

Example of usage with message:

{% set recom = best_selling_products() %}
{% if recom|length > 0 %}
  {# displaying recommended items #}
{% else %}
  {% cancel "Recommended items not found" %}
{% endif %}

In case of cancellation the letter/SMS/printed form template, which are generating manually, user will see the message, specified in {% cancel %} tag.

Frequently occurring problems

1. How to output the value of order custom field in template

To output data of custom field use the method getCustomField. Example:

{{ order|getCustomField('some_code') }}

In example some_code is symbolic code of custom field.

2. Error occurred while trying to output order field containing the date

It means the problem in case when you see the following error message:

Calling "__tostring" method on a "DateTime" object is not allowed

The fact is that for output order (or customer) fields, which contain dates, to twig-template, it is required apply filter date, to specify the format of date input.

Example:

{{ order.createdAt|date("d.m.Y") }}
    !!!!  3. Error occurred while trying to output field containing the date interval

    It means the problem in case when you see an error message like: 
    
Calling "m" property on a "DateInterval" object is not allowed

The fact is that for output fields, which contain date intervals, to twig-template, it is required apply filter date, to specify the output format.

Example:

{{ period|date("%m") }}

A detailed description of the DateInterval output formats can be found at: http://php.net/manual/ru/dateinterval.format.php

4. How to output current date

To output current date use filter date in the following way:

{{ "now"|date("d.m.Y") }}

5. How to insert the barcode to printed form

You can use the http://www.barcodes4.me/apidocumentation service. Example of data substitution:

<img src="http://www.barcodes4.me/barcode/c39/{{ offer.article }}.png" />

PrintEditHistory
Page last modified on February 03, 2023, at 01:20 PM