cancel
Showing results for 
Search instead for 
Did you mean: 
AaronK
Employee
Employee

Introduction

In the world of AI-driven data integration, the ability to dynamically generate prompts is crucial for creating adaptable and responsive workflows. The Prompt Generator Snap in SnapLogic leverages Mustache templating to allow users to craft dynamic text outputs based on input data. This whitepaper aims to educate users on the fundamentals of Mustache templating and how to effectively utilize it within the Prompt Generator Snap.

What is Mustache Templating?

Mustache is a logic-less templating engine for creating dynamic content. The term "logic-less" means that Mustache templates do not contain any programming logic, such as loops or conditionals. Instead, Mustache relies on the data passed to it, which is typically in JSON format, to populate the template. This simplicity makes Mustache both powerful and easy to use for generating dynamic content.

Key Features of Mustache

  • Logic-less: Simplifies template creation by avoiding embedded logic.
  • Data-Driven: Templates are populated based on external data.
  • Language-Agnostic: Can be used with various programming languages and platforms.

Mustache Syntax Basics

Variables

Variables in Mustache are denoted by double curly braces {{ }}. When the template is processed, these placeholders are replaced with corresponding values from the input data.

Example:

Hello, {{name}}!

If the input data is { "name": "John" }, the output will be:

Hello, John!

Sections

Sections are used for iterating over lists or conditionally displaying content. They are denoted by {{#section}} for opening and {{/section}} for closing.

Example:

{{#items}}
  {{name}} costs {{price}}.
{{/items}}

If the input data is { "items": [ { "name": "Apple", "price": "$1" }, { "name": "Banana", "price": "$0.5" } ] }, the output will be:

Apple costs $1.
Banana costs $0.5.

Inverted Sections

Inverted sections are used to display content when a variable is empty or false. They are denoted by {{^section}}.

Example:

{{#hasItems}}
  We have items in stock.
{{/hasItems}}
{{^hasItems}}
  Sorry, no items available.
{{/hasItems}}

If the input data is { "hasItems": false }, the output will be:

Sorry, no items available.

Using Mustache with the Prompt Generator Snap

Overview

The Prompt Generator Snap allows users to create dynamic prompts by combining Mustache templates with input data. This Snap is particularly useful for generating messages, notifications, or any text-based content that needs to be tailored based on varying input data.

Step-by-Step Guide

  1. Create a New Pipeline: Start by creating a new pipeline in SnapLogic Designer.

  2. Add the Prompt Generator Snap: Drag and drop the Prompt Generator Snap into your pipeline.

  3. Configure the Template:

    • Open the Snap settings.

    • In the "Template" field, enter your Mustache template. For instance:

      Hello, {{firstName}} {{lastName}}. Your order {{orderNumber}} has been shipped.
      
      
  4. Provide Input Data:

    • Connect the input data source to the Prompt Generator Snap. This could be a JSON Generator Snap or any other Snap that provides data in JSON format.

    • Ensure your input data matches the variables in your template. Example input data:

      {
        "firstName": "Jane",
        "lastName": "Doe",
        "orderNumber": "12345"
      }
      
      
  5. Validate and Execute:

    • Validate the pipeline to check for any errors.

    • Execute the pipeline to see the generated output. The expected output for the above example would be:

      Hello, Jane Doe. Your order 12345 has been shipped.
      
      

Advanced Usage

Nested Objects

Mustache supports nested objects, allowing you to access deeper levels of data.

Example:

{{person.name}} lives in {{person.address.city}}.

If the input data is:

{
  "person": {
    "name": "Alice",
    "address": {
      "city": "Wonderland"
    }
  }
}

The output will be:

Alice lives in Wonderland.

Lists and Looping

You can loop through lists within your data using sections.

Example:

{{#people}}
  {{name}} is from {{city}}.
{{/people}}

If the input data is:

{
  "people": [
    { "name": "Bob", "city": "Paris" },
    { "name": "Charlie", "city": "London" }
  ]
}

The output will be:

Bob is from Paris.
Charlie is from London.

Troubleshooting Tips

  • Missing Data: Ensure that all variables used in your template are present in the input data.
  • Incorrect Output: Validate the JSON structure of your input data to match the template's expected format.
  • Syntax Errors: Double-check Mustache syntax, especially the use of curly braces and section tags.

Conclusion

Mustache templating within the Prompt Generator Snap provides a powerful tool for creating dynamic, data-driven text outputs. By understanding the basics of Mustache syntax and how to integrate it with SnapLogic, users can enhance their data integration workflows with customized and adaptable content generation.

For more information and advanced examples, please refer to the SnapLogic documentation and Mustache templating resources.