Template Engine
Last updated
Was this helpful?
Last updated
Was this helpful?
Use these sample templates to get started with templates quickly and understand what real-world use cases might look like.
Context: A template for generating structured AI prompts with dynamic instructions, constraints, and example inputs/outputs.
Context: A product listing page for an e-commerce website, showing personalized content based on user authentication and product availability.
Context: A flexible database query generator that adapts to user roles and filtering requirements for a multi-tenant application.
Use an SQL template in combination with our Direct Database Query function to dynamically generate and use SQL queries against your Xano database. You can also use our External Database Query functions the same way.
Just type ?:raw
into the query editor and point the statement argument to the output of your Template Engine function.
Context: A Twig template for generating raw Markdown code with dynamic content and metadata.
Context: A flexible email template system that supports personalized messaging, dynamic sections, and optional signatures.
Use an HTML template in combination with our function to serve HTML directly using your APIs by setting the header Content-Type: text/html; charset=utf-8
Xano offers some filters to help ensure that any dynamic / user input is not parsed in a way that might harm your database or cause other unintended consequences.
Make sure to process your inputs before they are used in any SQL queries with the appropriate filter.
These filters are
The Template Engine, powered by Twig, is used to manipulate and dynamically generate large blocks of text or code with your own data, such as records from your Xano database, or from inputs sent to your APIs.
It's great for helping generate things like AI prompts, HTML, and other more large-format data without messing around with a bulk of separate functions to do so.
At its core, think of the Template Engine as text replacement and manipulation of the future. It is designed to give you a simple syntax to quickly manipulate large text strings with dynamic data, such as...
AI Prompts
HTML
JSON
SQL queries
Replacing a name inside of a string like "Hello, [first_name] [last_name]"
Dynamically providing a price for a single product
The Template Engine, however, is useful for content templates where:
The template will be edited by non-developers
The data structure is complex with nested objects
You need to include conditional sections
Data formatting (like dates) needs to be consistent
Templates might be reused with different data sources
If you're doing dynamic replacement over a longer block of text, such as the example below, Template Engine will make this much easier for you.
Variables are wrapped in {{ curly braces }}, like this, and begin with a $ character. In the below example, we're getting the name
from an object stored in the user1
variable.
Reference items in an array by using the item index.
Conditionals are helpful if you want to dynamically determine what the end result of your template looks like outside of the actual data. For example, maybe you want VIP users to have a different greeting than regular users.
Conditionals are wrapped in {% and %} and have support for else
and else if
In the above example, for this user:
...the result would be:
In the above example, for this score:
...the result would be:
You can use loops to populate lists of data without having to write out separate lines for each item, or knowing how many items you'll need to populate.
2x Blue T-shirt at $19.99 each
1x Denim Jeans at $59.99 each
3x Cotton Socks at $4.99 each
You can also use an Else statement at the end of your For loop to determine what action to take if no items are found. In the next example, if $list
contains no items, the template will return No items found.
You can use Twig's built in filters, similar to our own, to transform or manipulate data as part of the template.
upper
Converts string to uppercase
{{ $user.name|upper }}
When $user.name is "John Smith"
"JOHN SMITH"
lower
Converts string to lowercase
{{ $user.name|lower }}
When $user.name is "John Smith"
"john smith"
trim
Removes whitespace from the beginning and end of a string
{{ $user.input|trim }}
When $user.input is " hello "
"hello"
join
Joins array elements into a string with a delimiter
{{ $user.tags|join(', ') }}
When $user.tags is ["php", "twig", "web"]
"php, twig, web"
default
Provides a fallback value if the variable is null, empty, or undefined
{{ $user.middleName|default('No middle name') }}
When $user.middleName is null
"No middle name"
number_format
Formats numbers with grouped thousands and decimal points
{{ $product.price|number_format(2, '.', ',') }}
When $product.price is 1234.56
"1,234.56"
shuffle
Randomly shuffles an array
{{ $user.items|shuffle }}
When $user.items is ["a", "b", "c"]
Random order like: ["c", "a", "b"]
date
Formats dates using PHP's date syntax
{{ $user.createdAt|date("F j, Y") }}
When $user.createdAt is "2023-12-25"
"December 25, 2023"
The escape filter is used to format text using specifications designated by the destination, such as a URL that only allows certain characters to remain valid.
When you use e
by itself without specifying a format, it typically defaults to HTML escaping. This means it will convert characters like <
, >
, &
, "
, and '
to their HTML-safe equivalents.
When you specify a format (like e('html')
, e('js')
, e('url')
, etc.), you're explicitly telling the Template Engine how to escape the content for a specific context, which can provide more precise protection. We'd recommend always specifying the format, just to be safe.
You can insert comments into your templates by wrapping them in {# and #}. They won't appear in your final template.
The template engine is powered by Twig, which you can learn more about .
You should stick with filters like or if you're manipulating short strings of text, such as:
The below list is some of the most essential filters used in Twig, but it is not all of them. You can review the entire list .