I am getting started with StringTemplate 4 and I am trying to create a template from a simple string stored in a database. I use something like this:
STGroup
The documentation for CompiledST states tokens
is only for debug. Not sure what that means.
ST template = new ST("Hello <username>, how are you? Using <if(condition)>expression<endif> in condition works, and repeating <username> is not a problem.");
Set<String> expressions = new HashSet<String>();
TokenStream tokens = template.impl.tokens;
for (int i = 0; i < tokens.range(); i++) {
Token token = tokens.get(i);
if (token.getType() == STLexer.ID) {
expressions.add(token.getText());
}
}
Gives you the Strings username
and condition
.
You can use st.impl.formalArguments to access the Map<String, FormalArgument>
where the arguments are defined. Note that for some templates this field will be null
.