Class SimpleEmailTemplate
java.lang.Object
cloud.opencode.base.email.template.SimpleEmailTemplate
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 Summary
Modifier and TypeMethodDescriptionstatic voidClear template cache 清除模板缓存static StringcreateHtmlTemplate(String title, String body) Create a simple HTML email template 创建简单的HTML邮件模板static StringescapeHtml(String text) Escape HTML special characters 转义HTML特殊字符static intGet cache size 获取缓存大小static SimpleEmailTemplateGet singleton instance 获取单例实例static StringloadTemplate(Path path) Load template from file 从文件加载模板static StringloadTemplateCached(Path path) Load template with caching 使用缓存加载模板static StringloadTemplateFromClasspath(String resourcePath) Load template from classpath 从类路径加载模板static StringloadTemplateFromClasspath(String resourcePath, ClassLoader classLoader) Load template from classpath with custom classloader 使用自定义类加载器从类路径加载模板static StringloadTemplateFromClasspathCached(String resourcePath) Load template from classpath with caching 使用缓存从类路径加载模板static voidremoveFromCache(String key) Remove specific template from cache 从缓存中移除特定模板static StringrenderTemplate(String template, Map<String, Object> variables) Render template with variable substitution (static method) 使用变量替换渲染模板(静态方法)static StringrenderTemplate(String template, Map<String, Object> variables, boolean escapeHtml) Render template with optional HTML escaping 使用可选HTML转义渲染模板
-
Method Details
-
getInstance
Get singleton instance 获取单例实例- Returns:
- the instance | 实例
-
render
-
renderTemplate
-
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
-
loadTemplateFromClasspath
-
loadTemplateFromClasspath
Load template from classpath with custom classloader 使用自定义类加载器从类路径加载模板- Parameters:
resourcePath- the classpath resource path | 类路径资源路径classLoader- the class loader | 类加载器- Returns:
- the template content | 模板内容
-
loadTemplateCached
-
loadTemplateFromClasspathCached
-
clearCache
public static void clearCache()Clear template cache 清除模板缓存 -
removeFromCache
Remove specific template from cache 从缓存中移除特定模板- Parameters:
key- the cache key | 缓存键
-
getCacheSize
public static int getCacheSize()Get cache size 获取缓存大小- Returns:
- the cache size | 缓存大小
-
escapeHtml
-
createHtmlTemplate
-