1. General
  2. Nested Substitution Recipe

General

Nested Substitution Recipe

Recipe for nested substitution using Handlebars skip and substitute helpers. Useful for multi-stage rendering, preserving placeholders, and controlled dynamic content generation.

User Engagement

Example User Requests :

  • How can I implement nested substitution in my Stubber agent?
  • I want my bot to interpolate runtime data into it's system prompt on the _create action, how can I do that?

Clarification Questions

  • Where in template.data is your system prompt stored?
  • What is the runtime data (stubpost.data) you want to interpolate into your system prompt?

Example Answers

  • My system prompt is stored in template.data.prompts.system
  • The runtime data I want to interpolate is the user's name, which is stored in stubpost.data.customer_name

Implementation Plan

Implementation Steps:

  1. Add stubpost.data.customer_name to the prompt in question, inside the skip helper, like so:
        You are a friendly support bot, you work for GreenFibre, a fibre and wireless telecoms company.

You are speaking to: {{{{skip}}}}{{stub.data.customer_name}}{{{{/skip}}}}.

      
  1. This will ensure that {{stubpost.data.customer_name}} is not substituted during the initial template rendering phase, allowing it to be available for substitution during the _create action.

  2. Inside a task in the _create action, use the substitute helper to perform the nested substitution on template.data.prompt.system, like so:

        {
  "start_gpt_chat": {
    "tasktype": "gpt_chat_task",
    "params": {
      "messages": [
        {
          "role": "system",
          "content": "{{#substitute}}{{template.data.prompts.system}}{{/substitute}}"
        }
      ]
    }
  }
}

      
  1. The final system prompt sent to the model will have the stubpost.data.customer_name value substituted in, allowing for dynamic and personalized interactions based on runtime data.
        You are a friendly support bot, you work for GreenFibre, a fibre and wireless telecoms company.

You are speaking to: John.

      

Where John is the value of stubpost.data.customer_name at runtime.