<a id="template-context"></a>
<a id="id1"></a>
# Template Context

The following elements are available in the template context. Every filter a template can use, with an example of each, is listed in [Filters](template_filters.html.md#template-filters).

These values are available whatever the Template's scope. Each scope then adds the objects that scope reports on — the Draft, the Project, or the rows of a listing:

- [Draft Home Template Context](template_context_draft_home.html.md)
- [Project Home Template Context](template_context_project_home.html.md)
- [Project Ticket Template Context](template_context_project_tickets.html.md)
- [Draft Object List Template Context](template_context_draft_object_list.html.md)

- **Name:** checksum

  **Description:** The fingerprint value for the document. Every document template has a unique fingerprint which is calculated from its definition. Including the fingerprint in a document can provide assurance that a validated version of the document template has been used to generate a particular document.

  **Example:**

  ```jinja
  Checksum : {{ checksum }}
  ```

- **Name:** convert_html

  **Description:** A filter function which attempts to turn HTML content into equivalent Word or Excel formats for display.

  *Only available in Word and Excel Document Templates*

  **Example:**

  ```jinja
  {{ field.PreText|convert_html }}
  ```

- **Name:** fail(message)

  **Description:** Stops generation. No document is produced, and the generation fails showing *message*, so a template can refuse to run when what it reports on is not in a state it should report on. A test run from the Document Template editor shows the message the same way.

  The message is shown as plain text on one line. Anything that looks like an HTML tag is removed, as is any other `<` or `>`, and line breaks become spaces. So write *under 18* rather than *< 18*, and *Visit Screening* rather than *<Screening>*. With no message, or an empty one, the generation fails showing "Generation stopped by the template."

  **Example:**

  ```jinja
  {% if conflicts %}
    {{ fail("Scripter and Tester found to be the same for: " ~ conflicts|join(", ")) }}
  {% endif %}
  ```

- **Name:** GENERATED_AT

  **Description:** A python date/time object in UTC timezone representing the start date/time of the document generation task.

  See [Python Date Formats](https://docs.python.org/3/library/time.html#time.strftime).

  **Example:**

  ```jinja
  Created at: {{ GENERATED_AT | timezone(USER_TIMEZONE_NAME) | format_datetime('%Y-%m-%d') }}
  ```

- **Name:** IS_TEST

  **Description:** A boolean representing whether the document template is being run in test mode from the document template editor.

  **Example:**

  ```jinja
  {% if IS_TEST %}RUNNING IN TEST MODE{% endif %}
  ```

- **Name:** len

  **Description:** The python len() function. Returns the length of a list or string

  **Example:**

  ```jinja
  {% if len(fields) > 10 %}...{% endif %}
  ```

- **Name:** project_role_for_user(user)

  **Description:** project_role_for_user(user) takes a [User](attribute_reference.html.md#attribute-user) object and returns the [User Project Role](attribute_reference.html.md#attribute-user-project-role) that the user has in this Project.

  **Example:**

  ```jinja
  Hello {{ user.username }} your role in the project is {{ project_role_for_user(user).name }}
  ```

- **Name:** user_by_id(id)

  **Description:** user_by_id(id) takes the value of a [User](../properties.html.md#user-properties) Custom Property and returns the [User](attribute_reference.html.md#attribute-user) it names, so you can show the person's name and email address rather than the stored number.

  It returns nothing when the property has no value, when the person no longer exists, or when they do not work in this Organization, so test the result before using it.

  **Example:**

  ```jinja
  {% set owner = user_by_id(form.properties['form owner'].value) %}
  {% if owner %}Owner: {{ owner.username }} ({{ owner.email }}){% endif %}
  ```

- **Name:** compare_to_standard(object)

  **Description:** compare_to_standard(object) compares an object with its Library Standard, as the Compare dialog does, and returns the differences, Standard Rules and explanations. See [Comparing Objects](comparing_objects.html.md#comparing-objects).

  It returns nothing when the object has no Library Standard you can view, so test the result before using it.

  **Example:**

  ```jinja
  {% set result = compare_to_standard(form) %}
  {% if result %}{{ result.summary.differences.total }} differences{% endif %}
  ```

- **Name:** compare(first, second)

  **Description:** compare(first, second) compares two objects of the same kind directly and returns every difference. See [Comparing Objects](comparing_objects.html.md#comparing-objects).

  **Example:**

  ```jinja
  {% set result = compare(first_form, second_form) %}
  {{ result.has_changes }}
  ```

- **Name:** now()

  **Description:** now() function returns a python DateTime object representing the current server time with UTC timezone.

  See [Python Date Formats](https://docs.python.org/3/library/time.html#time.strftime).

  **Example:**

  ```jinja
  Time is now: {{ now() | format_datetime(('%d %b %Y %H:%M:%S') }}
  ```

- **Name:** settings

  **Description:** A python dictionary of the values the user chose in the generation dialog, keyed by the Setting's *Name*. Every Setting defined on the Template is present, holding its Default when the user did not change it.

  See [Template Settings](template_settings.html.md#template-settings).

  **Example:**

  ```jinja
  {{ settings.get("form_order") }}
  ```

- **Name:** template_name

  **Description:** The name of the Document Template being executed.

  **Example:**

  ```jinja
  Template: {{ template_name }}
  ```

- **Name:** timezone

  **Description:** Converts a datetime value into the timezone provided.

  See [List of Time Zones](https://en.wikipedia.org/wiki/List_of_tz_database_time_zones).

  **Example:**

  ```jinja
  User Time : {{ now()|timezone(USER_TIMEZONE_NAME)|format_datetime('%Y-%m-%d') }}
  Time in NY : {{ now()|timezone("America/New_York")|format_datetime('%Y-%m-%d') }}
  ```

- **Name:** user

  **Description:** The [User](attribute_reference.html.md#attribute-user) object for the user running the report.

  **Example:**

  ```jinja
  Run by {{ user.username }}
  ```

- **Name:** USER_TIMEZONE_NAME

  **Description:** The name of the timezone of the user running the document template. e.g. Europe/London

  See [List of Time Zones](https://en.wikipedia.org/wiki/List_of_tz_database_time_zones).

  **Example:**

  ```jinja
  Timezone : {{ USER_TIMEZONE_NAME }}
  ```

- **Name:** wiki_page(name)

  **Description:** A function which returns a [Wiki](../../project_wiki.html.md#project-wiki) page from the Drafts Project Wiki, formatted using the convert_html filter above. This is useful for injecting content maintained in the Wiki into documents. Place it on its own line, outside any `<para>`, when the page contains a table, a code block, a note or a horizontal line. Code blocks keep their line breaks and are colored when the Wiki page names their language. Table cells that span columns or rows are merged in the Word table. A note or warning (`!!! note`) becomes a box with a thick left border, so it stays apart from the rest of the page. Images pasted into the Wiki page are included, scaled to fit the width of the page. Images from other web addresses and the `[TOC]` contents list are not included, and links become plain text.

  *Only available in Word Document Templates*

  **Example:**

  ```jinja
  {{ wiki_page("Design Notes") }}
  ```
