Class SimpleTemplateEngine

java.lang.Object
com.tnsai.prompt.SimpleTemplateEngine
All Implemented Interfaces:
com.tnsai.prompt.TemplateEngine

public class SimpleTemplateEngine extends Object implements com.tnsai.prompt.TemplateEngine
Simple template engine with {{variable}} syntax.

Supports basic variable substitution without external dependencies. For more advanced features like conditionals and loops, use MustacheTemplateEngine.

Syntax

  • {{variable}} - Simple variable substitution
  • {{variable:default}} - Variable with default value
  • {{#if variable}}...{{/if}} - Conditional block (truthy check)
  • {{#unless variable}}...{{/unless}} - Inverse conditional

Usage


 TemplateEngine engine = new SimpleTemplateEngine();

 String template = "Hello {{name}}, your score is {{score:0}}";
 String result = engine.render(template, Map.of("name", "Alice", "score", 95));
 // Result: "Hello Alice, your score is 95"

 // With default value
 String result2 = engine.render(template, Map.of("name", "Bob"));
 // Result: "Hello Bob, your score is 0"
 
  • Constructor Details

    • SimpleTemplateEngine

      public SimpleTemplateEngine()
  • Method Details

    • render

      public String render(String template, Map<String,Object> variables)
      Specified by:
      render in interface com.tnsai.prompt.TemplateEngine
    • extractVariables

      public Set<String> extractVariables(String template)
      Specified by:
      extractVariables in interface com.tnsai.prompt.TemplateEngine
    • getName

      public String getName()
      Specified by:
      getName in interface com.tnsai.prompt.TemplateEngine