TemplateString
A simple template string implementation. Template strings are a more structured way to do string interpolation, where you can define a template with placeholders for variables, and then fill in those variables later.
Example usage:
var template: templateString = "Hello, {{name}}! Today is {{day}}.";
var variables: map(string, string);
variables["name"] = "Alice";
variables["day"] = "Monday";
var result = template(variables);
writeln(result); // Output: Hello, Alice! Today is Monday.
- record templateString
The template string type. Template strings can either be implicitly created from string literals, or explicitly created with the constructor. The template string type has a single field, template, which is the template string itself. Variables in the template string are denoted by
{{variableName}}by default, but this can be customized.var template = new templateString("Hello, <<name>>! Today is <<day>>.", ("<<", ">>")); var variables: map(string, string); variables["name"] = "Alice"; variables["day"] = "Monday"; var result = template(variables); writeln(result); // Output: Hello, Alice! Today is Monday.
- var template : string
The template string itself.
- proc this(variables) : string throws
Fill in the template string with the provided variables, returning the result.
- Arguments:
variables – The variables to fill the template with. This can either be a
Map.mapof strings to strings or an associative array with string keys and string values.- Throws:
TemplateStringError – If the template string is malformed (e.g. has an unclosed variable) or if a variable in the template string is not found in the variables map.
- proc fill(variables: map(string, string)) : string throws
Fill in the template string with the provided variables, returning the result.
- Arguments:
variables – A
Map.mapof strings to strings, where the keys are the variable names and the values are the replacements.- Throws:
TemplateStringError – If the template string is malformed (e.g. has an unclosed variable) or if a variable in the template string is not found in the variables map.
- proc fill(variables: [] string) : string throws where variables.isAssociative() && variables.domain.idxType == string
Fill in the template string with the provided variables, returning the result.
- Arguments:
variables – An associative array with string keys and string values, where the keys are the variable names and the values are the replacements.
- Throws:
TemplateStringError – If the template string is malformed (e.g. has an unclosed variable) or if a variable in the template string is not found in the variables map.
- class TemplateStringError : Error
Error type for TemplateString errors