Skip to main content

Format text using the Answer Utility block

Overview

The Render Templated Text action formats text using Jinja 2. It's a very handy tool when you need to change inputted data into a format that suits your use case. This can range from trimming text, to formatting currency, to transforming JSON data into something your chatters can read.

render_tempate_3.png

What is Jinja?

Jinja is a templating engine, or in more simple terms, a text formatter. A Jinja template is a set of instructions that tells the templating engine how that formatted text should look.

Use cases for Render Templated Text

  • String manipulation (e.g. parsing a full name into just the first name)

  • Casing (e.g. capitalize all letters in a word, or capitalize only the first letter)

  • String validation (e.g. checking an order number’s length)

  • Extensive conditional logic

  • Presenting JSON data in readable text format

Configure the Render Templated Text action

Refer to the Template Designer documentation for help building templates.

  1. Click the Select Action drop-down menu and select Render Templated Text.

  2. Use the Jinja syntax to build a formatting template in the Jinja Template field.

    Tip

    Use @@ in your template to indicate a line break.

  3. Add a variable containing the information to be rendered into the Template Input field.

  4. Click the Select Optional Variables drop-down and select Rendered Output.

  5. Assign a variable for Rendered Output to capture the formatted text.

  6. Click the Fallback Answer drop-down menu, then select an Answer for use as a fallback should an error occur.

Template examples

The following tables illustrate some of the Jinja templates you can use.

Printing expressions

{% ... %} for Statements

{{ ... }} for Expressions to print to the template output

{# ... #} for Comments not included in the template output

# ... ## for Line Statements*

Note

  • * “# for item in seq” and “{% for item in seq %}” are equivalent.

  • If the rendered template has a length greater than 1024 characters, instead of the full output, the result is truncated to the 1021st character, and "..." is appended to the output (i.e. the output will never be greater than 1024 characters). This is to compensate for the text message limit of 1024 characters imposed by Ada chat.

String Manipulation

Manipulate strings of text.

Template Input

Template

Result

Hello world!

{{ template_input[1] }}

H

Hello world!

{{ template_input[-1] }}

!

Hello world!

{{ template_input[-2:] }}

d!

Hello world!

{{ template_input[1:] }}

ello world!

Hello world!

{{ template_input[:-1] }}

Hello world

Hello world!

{{ template_input[1:9] }}

ello wor

Hello world!

{{ template_input.strip() }}

Hello world!

Hello world!

{{ template_input.replace(" ",", ") }}

Or

{{ template_input|replace(" ",", ") }}

Hello, world!

Hello world!

{{ template_input[-5:].capitalize() }}

Orld!

Note

Multiple filters can be chained. The output of one filter is applied to the next.

Capitalization

Change the case of string text.

Template input

Template

Result

JOHN SMITH

{{ template_input.capitalize() }}

John smith

JOHN SMITH

{{ template_input.title() }}

John Smith

Hello world!

{{ template_input.upper() }}

HELLO WORLD!

Hello world!

{{ template_input.lower() }}

hello world!

Validation

You can validate a string.

Template input

Template

Result

Hello world!

{{ "!" in template_input }}

True

Hello world!

{{ template_input|length }}

12

123456789

{{ template_input|string|length }}

9

Format currency

Create a template to format currency.

Template input

Template

Result

975

{{ "{:,.2f}".format(template_input) }}

975.00

7807.9

{{ "{:,.2f}".format(template_input) }}

7,807.90

$460568.24

{{ "{:,.2f}".format(template_input[1:]|float) }}

460,568.24

If statement

The if statement in Jinja mimics the if statement in Python and the value of the condition determines the flow of the statement.

  • {% if “Canada” in template_input %}CA

  • {% elif “United States” in template_input %}US

  • {% elif “Mexico” in template_input %}MX

  • {% else %}Country not available

  • {% endif %}

These are just a few example use cases. There are lots of ways to use the Rendered Templated Text action.

Have any questions? Contact your Ada team—or email us at help@ada.support.