1. Handlebars Helpers
  2. Substitute

Handlebars Helpers

Substitute

Do variable substitution on the enclosed text before returning it.

Overview

This helper does an extra round of variable substitution on the enclosed text before processing it, this allows you to treat some parts of your data as string templates and have them evaluated before being used in the ultimate text output.

TIP

Use this helper wherever you are referencing a variable that itself contains variable substitution.

TIP

Easy way to know when you see {{[data path]}} output in your final text, it means you need to wrap that reference in the {{#substitute}}...{{/substitute}} helper.
Wrap the lowest level reference first and work your way up if you still see expected result text in the final output.

Syntax

        "{{#substitute}}{{[context that contains a string template]}}{{/substitute}}"

      

Examples

Prompt referencing other prompt "parts"

This helper can be used to reference other parts of the prompt that themselves contain variable substitution, by wrapping the variable in the {{#substitute}} helper.

Example Data

editor
        {
    "template":{
        "data": {
            "prompts": {
                "system": "You are a helpful assistant.  #Introduction {{#substitute}}{{template.data.prompts.guidelines}}{{/substitute}}",
                "guidelines": "Your name is {{template.data.settings.assistant_name}}.  1. Be concise. 2. Be accurate. 3. Be helpful.",

            },
            "settings": {
                "assistant_name": "Kelly"
            }
        }
    }
}

      

Substitution

editor
        {
    "template":{
        "data": {
            "prompts": {
                "system": "You are a helpful assistant.  #Introduction Your name is Kelly.  1. Be concise. 2. Be accurate. 3. Be helpful.",
                "guidelines": "Your name is Kelly.  1. Be concise. 2. Be accurate. 3. Be helpful.",

            },
            "settings": {
                "assistant_name": "Kelly"
            }
        }
    }
}