Content Templates - TWIG

urbananglerurbanangler Member Posts: 1

Having some issues trying to create macros in content templates {% macro %}.

I have successfully done this within the theme code (.rain files) but I can also troubleshoot to see if I am on the right track (with "tb/show" on the front end or via HTML & JSON through the browser).

In this case it is not as clear with the preview button (through browser). It is not representative of what the actual PDF looks like so printing PDF each time you make a change is rather tedious.

In this case I was hoping to alter some data in the {{}} & {{product.vaiant}}field. Specifically using the |replace filter. I would love to employ a macro in this case because 1) it will be reused within the doc and 2) my replace list is quite large.

If anyone can help me with the following.

Content Template - Invoice

  • Product Variant
{% macro productVariant(products) %}
  {% for product in products %}
    {% if 'One Size' in product.variant %}
     {{ '' }}
   {% else %}
     — {{ product.variant|replace ({'Size:':'','Color:':'','"':'',',':' - ','Size : ':'','Color : ':'','Single or 3-pack:':'','Type:':'','Reel or Spool: ':''}) }}
    {% endif %}
  {% endfor %}
{% endmacro %}
{# print the adjusted product.variant #}
{{ _self.productVariant(products) }}

In any case, I cannot get this to work as written.

Alternately, I would entertain other filters or options to achieve this in a more efficient way.

Sign In or Register to comment.