Reputation: 959
I am trying to get all the template parameters - the parameter names inside {{}}. As an example, for this template:
The {{pet}} chased after the {{toy}}
I'd like to get "pet" and "toy"
I am only able to use the samskivert/jmustache library so I cannot use a different mustache library.
Is there a way to do this with jmustache so that I don't have to parse the string with a regex?
Upvotes: 1
Views: 864
Reputation: 12024
Mustache.Visitor
Used to visit the tags in a template without executing it
Example:
List<String> vars = new ArrayList<>();
Template tmpl = ... // compile your template
tmpl.visit(new Mustache.Visitor() {
// I assume you don't care about the raw text or include directives
public void visitText(String text) {}
// You do care about variables, per your example
public void visitVariable(String name) {vars.add("Variable: " + name); }
// Also makes sense to visit nested templates.
public boolean visitInclude(String name) { vars.add("Include: " + name); return true; }
// I assume you also care about sections and inverted sections
public boolean visitSection(String name) { vars.add("Section: " + name); return true; }
public boolean visitInvertedSection(String name) { vars.add("Inverted Section: " + name); return true; }
});
Visitor
is available from jmustache
version 1.15:
<groupId>com.samskivert</groupId>
<artifactId>jmustache</artifactId>
<version>1.15</version>
Upvotes: 1
Reputation: 959
There's actually a mechanism for this using Visitor
pattern https://github.com/samskivert/jmustache/issues/108#issuecomment-510071602 but it's not released yet https://github.com/samskivert/jmustache/issues/109#issue-466358322
Upvotes: 0