Word Template Variables

Introducing the Word template variables

Jinja2 Statements, Expressions, & Filters

When you request a Word document, Ghostwriter opens your selected template file and processes any Jinja2 expressions within the document to create a new document. The new document is saved in memory and sent to you for download.

Jinja2 uses statements, expressions, and filters. These equate to lines of code and variables:

  • Statement{% ... %}

    • Statements are lines of code, like {% if some_variable %}

  • Expression{{ ... }}

    • In general, an expression works like a variable in most cases, like {{ client.name }}

  • Filter... |filter ...

    • You can pipe a value into a filter to modify it, like {{ client.name|title }}

Templates can contain basic expressions that Ghostwriter simply replaces and more complicated statements (e.g., for loops, if/else). In addition to the custom expressions and filters documented on this page, Jinja2 offers built-in statements, expressions, and filters you can use with Ghostwriter templates.

The official Jinja2 documentation contains all of the information you need to get started using its more advanced features:

There are also various considerations covered in the official Jinja2 documentation, such as whitespace control:

All of Ghostwriter's expressions and statements should be wrapped in curly braces with one space to either side ({{ client.name }}or {% if ... %} ) – unless otherwise noted.

If you do not include the spaces, that expression will not be recognized as valid and will be ignored by Jinja2.

Potentially Useful Jinja2 Expressions

These expressions are built into Jinja2 and might be useful in your Word documents:

There are many other expressions and filters available. If you want to do something, there is probably a way to accomplish it with a built-in expression cleanly. You can perform math, logic, string mutations, and more.

Check the Jinja2 documentation: https://jinja.palletsprojects.com/en/3.1.x/templates/#expressions

Ghostwriter Expressions

To see what all is available for your report, generate the JSON report. Everything in the resulting JSON will be available to a report template. The following table describes the top-level keys:

Dates are localized based on your locale configuration in the server settings. The default is en-us, so the default date format is M d, Y (e.g., June 22, 2021).

The project key has separate values for the day, month, and year the project started and ended. Use these to assemble your own date or date range formats if you need to represent a date differently or only want part of the date.

If you do not have a client short_name value set, Ghostwriter will replace references to client.short_name with the client's full name.

Findings Attributes – HTML & Rich Text Attributes

You write your findings in Ghostwriter's WYSIWYG editor, where you can style text as you would directly in Word. The WYSIWYG editor uses HTML, so Ghostwriter stores your content as HTML.

Let's say you put the following Jinja2 code in a template:

{% for finding in findings %}
{{ finding.description }}
{% endfor %}

That would drop in raw HTML using whatever style you had assigned to {{ finding.description }} in the template. It's unlikely you would want that.

Jinja2's striptags filter can help, but that removes all HTML without preserving new lines. Ghostwriter's custom strip_html filter will strip the tags and preserve newlines, but the output will still be all plaintext. You must re-apply character and paragraph styles, font changes, and other options. Your evidence files will also appear as text placeholders.

To get what you see in the WYSIWYG editor in your Word document, add _rt (for rich text) to the attribute's name, use the p tag (see Ghostwriter Tags below). The above example becomes:

{% for finding in findings %}
{{p finding.description_rt }}
{% endfor %}

This will drop in your WYSIWYG HTML converted to Open XML for Word. Your image and text evidence will be present (with style and border options applied), and all of your text will be styled.

Each finding also has a unique severity_rt attribute. You don't style this text in the WYSIWYG editor. Ghostwriter creates a rich text version of your severity category that is colored using your configured color code.

The severity_rt attribute only styles the color of the text run so that you can apply a paragraph style to it directly in your Word template. Use it with the r tag (for a run) like so:

That template renders as:

Ghostwriter Tags

Several tags used for Word documents are not built into Jinja2. These tags are added after you open an expression or statement (before the space).

Example: {{p findings_subdoc }}

The table tags may appear complicated at first. You can create a table with a row for each point of contact using the provided statements, expressions, and tags like this:

Per python-docx-template, do not use {%p, {%tr, {%tc or {%r twice in the same paragraph, row, column or run.

Bad:

{%p if display_paragraph %}Here is my paragraph {%p endif %}

Good:

{%p if display_paragraph %}
Here is my paragraph
{%p endif %}

Ghostwriter Statements

There are several statements for Word documents that are not built into Jinja2:

Ghostwriter Filters

Ghostwriter offers some custom filters you can use to modify report values quickly:

The filter collection is under development and will continue to grow.

Subdocuments

Subdocuments are like other variables, except they are pre-rendered Word documents. When a subdocument is inserted into the template, it is like copy/pasting content from one document into another. A subdocument can be a small paragraph or a much larger section.

Ghostwriter uses subdocuments to translate your WYSIWYG content (e.g., findings) to Office Open XML.

Subdocuments are referenced as {{p VARIABLE }}. That variable is automatically replaced with the contents of the subdocument.

Debugging a Template

Ghostwriter uses the jinja2.ext.debug extension to make it easier for you to debug a template. Place a {% debug %} tag somewhere in your template.

The next time you generate a report with that template, Ghostwriter will replace the tag with the template's available context (the report and project data) and filters.

Last updated