You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
how to make template to detect empty richtext fields #138
Copied from python-docx-template issue 600, opened by @abubelinha on 2025-06-16.
Hello.
I am trying to do something like this for each entry (e) in my template:
{% for e in entries %} {{ e.country }}:{{ e.locality }}{%r if e.habitat %}; {{r e.habitat }}{%r endif %}{% if e.substrate %}; {{ e.substrate }}{% endif %}.
{% endfor %}
Basically, I want habitat & substrate fields to be printed preceded by a separator ';' (but ONLY if those fields contain a text; otherwise, the separator and field shouldn't be printed).
This is a simplified version of my code (I generate my context from a database and I apply RichText using a function depending on some other factors).
But the Honolulu entry illustrates my problem: is there a way my template can detect that RT("", color="blue") is actually an empty text entry, so the {%r if e.habitat %} should not be executed?
Currently, I am getting this text output (I am not showing the bold/italics/coloured output here, since that's not the problem: please notice the double ; separator after Honolulu):
USA: Boston; forest; wood.
USA: New Orleans; marsh.
USA: Honolulu; ; water.
USA: St. Louis; ship; water.
USA: New York!; concrete.
@abubelinha an empty RichText is now falsy, so {%r if e.habitat %} skips RT("", color="blue") the same way it skips "", and the Honolulu line loses the extra ;. Fixed in #205, it'll be in the next release, or pip install git+https://github.com/cvas22/docform to try it now.
Activity
cvas22 commentedon Nov 25, 2026
@abubelinha an empty RichText is now falsy, so
{%r if e.habitat %}skipsRT("", color="blue")the same way it skips"", and the Honolulu line loses the extra;. Fixed in #205, it'll be in the next release, orpip install git+https://github.com/cvas22/docformto try it now.