Class SimpleEmailTemplate

java.lang.Object
cloud.opencode.base.email.template.SimpleEmailTemplate

public final class SimpleEmailTemplate extends Object
Simple Email Template Engine 简单邮件模板引擎

Simple template engine with variable substitution.

支持变量替换的简单模板引擎。

Features | 主要功能:

  • Variable substitution: ${name} or {{name}} - 变量替换
  • Default values: ${name:default} - 默认值
  • HTML escaping support - HTML转义支持
  • Template caching - 模板缓存
  • File and classpath loading - 文件和类路径加载

Usage Examples | 使用示例:

// Direct template rendering
String result = SimpleEmailTemplate.render(
    "Hello ${name}, your order #${orderId} is confirmed.",
    Map.of("name", "John", "orderId", "12345")
);

// Load from file
String template = SimpleEmailTemplate.loadTemplate(Path.of("templates/welcome.html"));
String result = SimpleEmailTemplate.render(template, variables);

// Load from classpath
String template = SimpleEmailTemplate.loadTemplateFromClasspath("templates/welcome.html");

Security | 安全性:

  • Thread-safe: Yes - 线程安全: 是
Since:
JDK 25, opencode-base-email V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • getInstance

      public static SimpleEmailTemplate getInstance()
      Get singleton instance 获取单例实例
      Returns:
      the instance | 实例
    • render

      public String render(String template, Map<String,Object> variables)
    • renderTemplate

      public static String renderTemplate(String template, Map<String,Object> variables)
      Render template with variable substitution (static method) 使用变量替换渲染模板(静态方法)
      Parameters:
      template - the template content | 模板内容
      variables - the variables to substitute | 要替换的变量
      Returns:
      the rendered content | 渲染后的内容
    • renderTemplate

      public static String renderTemplate(String template, Map<String,Object> variables, boolean escapeHtml)
      Render template with optional HTML escaping 使用可选HTML转义渲染模板
      Parameters:
      template - the template content | 模板内容
      variables - the variables to substitute | 要替换的变量
      escapeHtml - whether to HTML-escape values | 是否HTML转义值
      Returns:
      the rendered content | 渲染后的内容
    • loadTemplate

      public static String loadTemplate(Path path)
      Load template from file 从文件加载模板
      Parameters:
      path - the file path | 文件路径
      Returns:
      the template content | 模板内容
    • loadTemplateFromClasspath

      public static String loadTemplateFromClasspath(String resourcePath)
      Load template from classpath 从类路径加载模板
      Parameters:
      resourcePath - the classpath resource path | 类路径资源路径
      Returns:
      the template content | 模板内容
    • loadTemplateFromClasspath

      public static String loadTemplateFromClasspath(String resourcePath, ClassLoader classLoader)
      Load template from classpath with custom classloader 使用自定义类加载器从类路径加载模板
      Parameters:
      resourcePath - the classpath resource path | 类路径资源路径
      classLoader - the class loader | 类加载器
      Returns:
      the template content | 模板内容
    • loadTemplateCached

      public static String loadTemplateCached(Path path)
      Load template with caching 使用缓存加载模板
      Parameters:
      path - the file path | 文件路径
      Returns:
      the template content | 模板内容
    • loadTemplateFromClasspathCached

      public static String loadTemplateFromClasspathCached(String resourcePath)
      Load template from classpath with caching 使用缓存从类路径加载模板
      Parameters:
      resourcePath - the classpath resource path | 类路径资源路径
      Returns:
      the template content | 模板内容
    • clearCache

      public static void clearCache()
      Clear template cache 清除模板缓存
    • removeFromCache

      public static void removeFromCache(String key)
      Remove specific template from cache 从缓存中移除特定模板
      Parameters:
      key - the cache key | 缓存键
    • getCacheSize

      public static int getCacheSize()
      Get cache size 获取缓存大小
      Returns:
      the cache size | 缓存大小
    • escapeHtml

      public static String escapeHtml(String text)
      Escape HTML special characters 转义HTML特殊字符
      Parameters:
      text - the text to escape | 要转义的文本
      Returns:
      the escaped text | 转义后的文本
    • createHtmlTemplate

      public static String createHtmlTemplate(String title, String body)
      Create a simple HTML email template 创建简单的HTML邮件模板
      Parameters:
      title - the email title | 邮件标题
      body - the body content | 正文内容
      Returns:
      the HTML template | HTML模板