|
| 1 | +# Text Template |
| 2 | + |
| 3 | +The library exposes a class called `TextTemplate`. |
| 4 | + |
| 5 | +This utility helps render text templates providing a template `string` and a resolver (`ParameterResolver`). |
| 6 | + |
| 7 | +## Examples |
| 8 | + |
| 9 | +```mdx-code-block |
| 10 | +<div className="examples_grid"> |
| 11 | +``` |
| 12 | + |
| 13 | +**Input** |
| 14 | + |
| 15 | +**Definition** |
| 16 | + |
| 17 | +**Output** |
| 18 | + |
| 19 | + |
| 20 | +```json |
| 21 | +{ "name": "John" } |
| 22 | +``` |
| 23 | +```transformers |
| 24 | +"Hello {name}" |
| 25 | +``` |
| 26 | +```json |
| 27 | +"Hello John" |
| 28 | +``` |
| 29 | + |
| 30 | + |
| 31 | +```json |
| 32 | +{ "name": "John" } |
| 33 | +``` |
| 34 | +```transformers |
| 35 | +"Hello {first_name,Anonymous}" |
| 36 | +``` |
| 37 | +```json |
| 38 | +"Hello Anonymous" |
| 39 | +``` |
| 40 | + |
| 41 | + |
| 42 | +```json |
| 43 | +{ |
| 44 | + "name": "John", |
| 45 | + "children": 2 |
| 46 | +} |
| 47 | +``` |
| 48 | +```transformers |
| 49 | +"{name} has {children} children" |
| 50 | +``` |
| 51 | +```json |
| 52 | +"John has 2 children" |
| 53 | +``` |
| 54 | + |
| 55 | + |
| 56 | +```json |
| 57 | +{ |
| 58 | + "first": "John", |
| 59 | + "last": "Doe" |
| 60 | +} |
| 61 | +``` |
| 62 | +```transformers |
| 63 | +"Your initials are {$$substring(0,1):first}{$$substring(0,1):last}" |
| 64 | +``` |
| 65 | +```json |
| 66 | +"Your initials are JD" |
| 67 | +``` |
| 68 | + |
| 69 | +```mdx-code-block |
| 70 | +</div> |
| 71 | +``` |
| 72 | + |
| 73 | +<br/> |
| 74 | + |
| 75 | +:::info |
| 76 | +You can use text templates inside json transformers using the function [`$$template`](functions/template) |
| 77 | +::: |
| 78 | + |
| 79 | +## API |
| 80 | + |
| 81 | +### Java |
| 82 | + |
| 83 | +```java |
| 84 | +class TextTemplate { |
| 85 | + |
| 86 | + /** |
| 87 | + * Creates a new memory template for a string |
| 88 | + * |
| 89 | + * @param template The template text |
| 90 | + * @param defaultResolver (optional) defines how the template should resolve parameter default values |
| 91 | + */ |
| 92 | + public TextTemplate(String template, ParameterDefaultResolveOptions defaultResolver); |
| 93 | + |
| 94 | + /** |
| 95 | + * Exposes a read only list to inspect the list of parameters |
| 96 | + * |
| 97 | + * @return a list of parameters in the template |
| 98 | + */ |
| 99 | + public List<TemplateParameter> getParameters(); |
| 100 | + |
| 101 | + /** |
| 102 | + * Renders the template after inserting the parameters |
| 103 | + * |
| 104 | + * @param resolver A resolver to extract parameter values |
| 105 | + * @param adapter (optional) The adapter to use for rendering |
| 106 | + * @param urlEncodeParameters (optional) if true, the parameters will be URL encoded |
| 107 | + * @return a string with its parameters replaced |
| 108 | + */ |
| 109 | + public String render(ParameterResolver resolver, JsonAdapter<?,?,?> adapter, Boolean urlEncodeParameters); |
| 110 | + |
| 111 | + /** |
| 112 | + * gets or creates a template from the cache |
| 113 | + * |
| 114 | + * @param template the command to parse |
| 115 | + * @param defaultResolver (optional) defines how the template should resolve parameter default values |
| 116 | + * @return a new text template |
| 117 | + */ |
| 118 | + public static TextTemplate get(String template, ParameterDefaultResolveOptions defaultResolver); |
| 119 | + |
| 120 | + /** |
| 121 | + * Creates a Map suitable to be used for a parameter resolver out of string parameters. |
| 122 | + * Parameters needs to be provided in pairs of key and value. |
| 123 | + */ |
| 124 | + public static Map<String, String> mapOf(String... parameters); |
| 125 | + |
| 126 | + public static String render(String template, ParameterResolver resolver, ParameterDefaultResolveOptions defaultResolver, Boolean urlEncodeParameters); |
| 127 | + public static String render(String template, Map<String, ?> resolver, ParameterDefaultResolveOptions defaultResolver); |
| 128 | +} |
| 129 | +``` |
| 130 | + |
| 131 | +:::info |
| 132 | +Some methods will accept `Map<String, ?>` instead of `ParameterResolver`.<br/> |
| 133 | +You can use the static method `mapOf` to create such a map. |
| 134 | +::: |
| 135 | + |
| 136 | +### TypeScript |
| 137 | + |
| 138 | +```typescript |
| 139 | +class TextTemplate { |
| 140 | + /** |
| 141 | + * Creates a new memory template for a string |
| 142 | + * |
| 143 | + * @param template The template text |
| 144 | + * @param defaultResolver defines how the template should resolve parameter default values |
| 145 | + */ |
| 146 | + constructor(template: string, defaultResolver?: ParameterDefaultResolveOptions); |
| 147 | + /** |
| 148 | + * Exposes a list copy to inspect the list of parameters |
| 149 | + * |
| 150 | + * @return a list of parameters in the template |
| 151 | + */ |
| 152 | + getParameters(): TemplateParameter[]; |
| 153 | + /** |
| 154 | + * gets or creates a template from the cache |
| 155 | + * |
| 156 | + * @param template the command to parse |
| 157 | + * @param defaultResolver defines how the template should resolve parameter default values |
| 158 | + * @return a new text template |
| 159 | + */ |
| 160 | + static get(template: string, defaultResolver?: ParameterDefaultResolveOptions): TextTemplate; |
| 161 | + /** |
| 162 | + * Renders the template after inserting the parameters |
| 163 | + * |
| 164 | + * @param resolver A resolver to extract parameter values |
| 165 | + * @param urlEncodeParameters if true, the parameters will be URL encoded |
| 166 | + * @return a string with its parameters replaced |
| 167 | + */ |
| 168 | + render(resolver: Record<string, string> | ParameterResolver, urlEncodeParameters?: boolean | null): Promise<string>; |
| 169 | + static render(template: string, resolver: Record<string, string> | ParameterResolver, defaultResolver?: ParameterDefaultResolveOptions, urlEncodeParameters?: boolean): Promise<string>; |
| 170 | +} |
| 171 | +``` |
0 commit comments