Templates

This article provides an overview of how prompt instructions and policies can be tailored to a user, through the use of templates. For instance, instructions can be customized for the user’s language, the device they are using, their plan type, and so on.

Introduction to templates

Markprompt uses Handlebars , which is a simple templating language that allows you to embed expressions in your content. It is used to dynamically generate content by merging templates with a context object. Handlebars provides a clean and concise syntax for injecting variables and managing conditional content.

Using variables

The simplest use of templates is via variables, which act as placeholders for content that can be provided on a per-user basis.

Basic variable insertion

Variables are placeholders that are wrapped in double curly braces ({{}}). These variables are replaced with corresponding values from the context provided during template rendering.

Example
1Hello, {{name}}!

If the context passed is:

1{
2  "name": "Steve Jobs"
3}

this template will be rendered as:

1Hello, Steve Jobs!

Accessing object properties

To organize information, nested properties within an object are also supported. You can use dot notation to access these properties.

Example
1- The user is on the {{user.plan}} plan. - Their main region is {{user.region}}.

If the context passed is:

1{
2  "user": {
3    "plan": "Pro",
4    "region": "Canada"
5  }
6}

this template will be rendered as:

1- The user is on the Pro plan.
2- Their main region is Canada.

Conditional statements

Conditional statements allow you to introduce logical branching within your instructions, such as providing different chunks of instructions depending on the user.

Basic conditional rendering

Handlebars allows you to conditionally render content using the {{#if}} block. The content within the {{#if}} block is rendered if the condition evaluates to true.

Example
1{{#if markprompt.isMobile}}
2  The user is on mobile, do not include images in the response.
3{{/if}}

Using alternative content

You can use the {{else}} block to define alternative content that is rendered when the condition in {{#if}} evaluates to false.

1{{#if markprompt.isMobile}}
2  - The user is on mobile. - Do not include images in the response. - Write
3  short answers (max 50 words).
4{{else}}
5  - The user is on desktop. - Include images in the response whenever relevant.
6  - Write comprehensive, richly formatted answers (but no longer than 300
7  words).
8{{/if}}

Helpers

Markprompt provides helper functions enabling you to interact with your project data.

Specifically, Markprompt currently supports a source helper, which allows you to include the content of an file inside of your prompts and policies. It used as follows:

1Here is our pricing:
2
3{{source name='Help Center' path='/guides/pricing'}}

The source helper expects two parameters:

  • name: the name of the source
  • path: the path of the file under that source

You can grab this information when you open the file in the Data tab.

Playground testing

In order to test your templates, the playground allows you to pass a mock context object. This can be provided by hitting the arrow next to the Send button:

The context can be set here, as a JSON-formatted object, e.g.:

1{
2  "user": {
3    "name": "Steve Jobs",
4    "role": "owner",
5    "plan": "Pro",
6    "region": "Canada"
7  },
8  "business": {
9    "id": "apple-inc"
10  }
11}

Reserved variables

In addition to the variables you pass explicitly via context, Markprompt also passes system variables and thread metadata, nested under the markprompt and threadMetadata objects respectively.

System variables

System variables are provided automatically, so that you do not need to worry about passing them explicitly. These variables are accessible via the markprompt object. They are currently:

Key Type Description
markprompt.isMobile true | false

True if the user is accessing your assistant via a mobile device.

To use it, simply refer to the properties under the markprompt object:

1{{#if markprompt.isMobile}}
2  The user is on a mobile device.
3{{/if}}

Thread metadata

Likewise, when you pass thread metadata alongside a chat request, the payload is accessible via the threadMetadata object. To use thread metadata, simply refer to the properties under the threadMetadata object:

1{{#if threadMetadata.country}}
2  The user is located in
3  {{threadMetadata.country}}.
4{{/if}}