-->

Is there an easier way to write text email templat

2019-08-20 08:26发布

问题:

I must be totally missing something here. What follows is as far as I could take it (jinja with trim_blocks, not Django templates):

{%- if order.contact_name -%}
    Name: {{ order.contact_name + '\n' -}}
{%- endif -%}
Phone: {{ order.phone + '\n' -}}
{%- if order.email -%}
    Email: {{ order.email + '\n' -}}
{%- endif -%}
{%- if order.comment -%}
    Comment: {{ order.comment + '\n' -}}
{%- endif -%}

{%- if order.delivery_type or order.town or order.branch or order.address -%}
    {{- '\n' -}}

    {%- if order.delivery_type -%}
        Delivery type: {{ order.delivery_type.display_name_short + '\n' -}}
    {%- endif -%}
    {%- if order.town -%}
        Town: {{ order.town + '\n' -}}
    {%- endif -%}
    {%- if order.branch -%}
        Branch: {{ order.branch + '\n' -}}
    {%- endif -%}
    {%- if order.address -%}
        Address: {{ order.address + '\n' -}}
    {%- endif -%}
{%- endif -%}

{%- for pvxo in order.productvariantxorders.all() -%}
    {{- '\n' -}}
    Category: {{ (pvxo.productvariant.product.subcategory.category.name
        if pvxo.productvariant
        else pvxo.product.subcategory.category.name) + '\n' -}}
    Product: {{ (pvxo.productvariant.product.name
        if pvxo.productvariant
        else pvxo.product.name) + '\n' -}}
    {%- if pvxo.productvariant -%}
        Size: {{ pvxo.productvariant.size.name + '\n' -}}
    {%- endif -%}
    Price: {{ pvxo.amount }} x {{ (pvxo.productvariant.product.price
        if pvxo.productvariant
        else pvxo.product.price) | string + '\n' -}}
    {{- (pvxo.productvariant.product.get_absolute_url()
        if pvxo.productvariant
        else pvxo.product.get_absolute_url()) + '\n' -}}
{%- endfor -%}

There's a bit more -'s than is really needed, but not by a wide margin. And that is to not think much about whether to add -. Extra ones don't hurt.

I'm not hell-bent on jinja, and hope you will point me in the right direction.