Class OpenTemplate
java.lang.Object
cloud.opencode.base.string.OpenTemplate
Template Utility - Provides simple string template rendering.
模板工具 - 提供简单的字符串模板渲染。
Features | 主要功能:
- Variable placeholder rendering - 变量占位符渲染
- Named and indexed parameter support - 命名和索引参数支持
- Template compilation and caching - 模板编译和缓存
- Built-in filters (upper, lower, truncate, etc.) - 内置过滤器
Usage Examples | 使用示例:
// Simple placeholder rendering
String result = OpenTemplate.render("Hello ${name}!", Map.of("name", "World"));
// "Hello World!"
// Indexed format
String formatted = OpenTemplate.format("Hello {0}, welcome to {1}!", "Alice", "Java");
// Compiled template
Template tpl = OpenTemplate.compile("${greeting} ${name}");
String output = tpl.render(Map.of("greeting", "Hi", "name", "Bob"));
Security | 安全性:
- Thread-safe: Yes (ConcurrentHashMap for filters and cache) - 线程安全: 是(过滤器和缓存使用 ConcurrentHashMap)
- Null-safe: Yes - 空值安全: 是
- Cache bounded: Yes (max 1024 templates) - 缓存有界: 是(最多1024个模板)
- Since:
- JDK 25, opencode-base-string V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionstatic voidstatic Templatestatic Stringstatic Stringstatic TemplateFilterstatic StringTemplatestatic PlaceholderTemplateplaceholder(String template, String prefix, String suffix) static voidRegister a named template.static voidregisterFilter(String name, TemplateFilter filter) static Stringstatic Stringstatic StringrenderInline(String template, Map<String, Object> context) static StringrenderNamed(String templateName, Map<String, Object> context)
-
Method Details
-
render
-
render
-
format
-
format
-
of
-
placeholder
-
compile
-
renderInline
-
registerFilter
-
getFilter
-
register
Register a named template. Cache is bounded to 1024 entries to prevent memory exhaustion from unbounded registration. 注册命名模板。缓存限制为 1024 条,防止无限注册导致内存耗尽。- Parameters:
name- the template name | 模板名template- the template string | 模板字符串- Throws:
IllegalStateException- if the cache is full and the name is new | 如果缓存已满且名称是新的
-
renderNamed
-
clearCache
public static void clearCache()
-