Is there an easier way to write text email templates in Python?

ぃ、小莉子 提交于 2019-12-11 15:18:00

问题


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.

来源:https://stackoverflow.com/questions/55572465/is-there-an-easier-way-to-write-text-email-templates-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!