Class TemplateEngine

java.lang.Object
cloud.opencode.base.string.template.TemplateEngine

public final class TemplateEngine extends Object
Template Engine - Simple and flexible template rendering engine 模板引擎 - 简单灵活的模板渲染引擎

A lightweight template engine that supports variable substitution, custom functions, and configurable delimiters. Provides a fluent API for template configuration and rendering.

轻量级模板引擎,支持变量替换、自定义函数和可配置分隔符。提供流畅的API用于模板配置和渲染。

Features | 主要功能:

  • Variable substitution - 变量替换
  • Custom function registration - 自定义函数注册
  • Configurable variable delimiters - 可配置变量分隔符
  • Fluent builder API - 流畅构建器API
  • Multiple context types support - 支持多种上下文类型

Usage Examples | 使用示例:

// Create engine with default settings
TemplateEngine engine = TemplateEngine.create();

// Render with Map context
Map<String, Object> context = Map.of("name", "World");
String result = engine.render("Hello ${name}!", context);
// Output: "Hello World!"

// Custom delimiters
engine = TemplateEngine.create()
    .variablePrefix("{{")
    .variableSuffix("}}");
result = engine.render("Hello {{name}}!", context);

// Register custom function
engine.registerFunction("upper", args -> args[0].toString().toUpperCase());
result = engine.render("${upper(name)}", context);

Performance | 性能特性:

  • Time complexity: O(n) for rendering - 渲染时间复杂度: O(n)
  • Space complexity: O(m) for context - 上下文空间复杂度: O(m)
  • Function lookup: O(1) using HashMap - 函数查找: O(1) 使用HashMap

Security | 安全性:

  • Thread-safe: No - 线程安全: 否
  • Null-safe: Yes - 空值安全: 是
  • Immutable: No (builder pattern) - 不可变: 否(构建器模式)
Since:
JDK 25, opencode-base-string V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • create

      public static TemplateEngine create()
      Create a new template engine instance 创建新的模板引擎实例

      Examples | 示例:

      TemplateEngine engine = TemplateEngine.create();
      
      Returns:
      new engine instance | 新的引擎实例
    • render

      public String render(String template, Map<String,Object> context)
      Render template with Map context 使用Map上下文渲染模板

      Examples | 示例:

      engine.render("Hello ${name}!", Map.of("name", "World"))  = "Hello World!"
      engine.render("${x} + ${y}", Map.of("x", 1, "y", 2))      = "1 + 2"
      
      Parameters:
      template - template string | 模板字符串
      context - variable context | 变量上下文
      Returns:
      rendered result | 渲染结果
    • render

      public String render(String template, TemplateContext context)
      Render template with TemplateContext 使用TemplateContext渲染模板

      Examples | 示例:

      TemplateContext ctx = TemplateContext.create().put("name", "World");
      engine.render("Hello ${name}!", ctx) = "Hello World!"
      
      Parameters:
      template - template string | 模板字符串
      context - template context | 模板上下文
      Returns:
      rendered result | 渲染结果
    • registerFunction

      public TemplateEngine registerFunction(String name, Function<Object[],Object> function)
      Register a custom function 注册自定义函数

      Examples | 示例:

      engine.registerFunction("upper", args -> args[0].toString().toUpperCase());
      engine.render("${upper(name)}", Map.of("name", "hello")) = "HELLO"
      
      Parameters:
      name - function name | 函数名
      function - function implementation | 函数实现
      Returns:
      this engine instance for chaining | 用于链式调用的引擎实例
    • variablePrefix

      public TemplateEngine variablePrefix(String prefix)
      Set variable prefix 设置变量前缀

      Examples | 示例:

      engine.variablePrefix("{{");
      engine.render("{{name}}", context) // Uses {{ instead of ${
      
      Parameters:
      prefix - variable prefix | 变量前缀
      Returns:
      this engine instance for chaining | 用于链式调用的引擎实例
    • variableSuffix

      public TemplateEngine variableSuffix(String suffix)
      Set variable suffix 设置变量后缀

      Examples | 示例:

      engine.variableSuffix("}}");
      engine.render("{{name}}", context) // Uses }} instead of }
      
      Parameters:
      suffix - variable suffix | 变量后缀
      Returns:
      this engine instance for chaining | 用于链式调用的引擎实例