How to extract all variables in a Mustache Template in Java

1,700 views
Skip to first unread message

panikumar malepati

unread,
May 31, 2017, 3:05:51 PM5/31/17
to mustache.java
Hi all,

   We have a requirement to extract all the variables from the Mustache Template. We need to identify the variables then query the values from other sub system. I'd appreciate if someone could provide a solution to this in Java. 

We are using the following library:


package: com.github.spullara.mustache.java
version: 0.9.2

Sam Pullara

unread,
May 31, 2017, 6:51:47 PM5/31/17
to mustac...@googlegroups.com
Can you give an example template and what you would do with each variable? It sounds like you want to avoid putting a view layer between the template and the raw data which usually leads to problems. Getting a structured tree of variables found in the template is possible using your own object handler that records it as it is executed. 

Sam
--
You received this message because you are subscribed to the Google Groups "mustache.java" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mustachejava...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

panikumar malepati

unread,
May 31, 2017, 8:29:14 PM5/31/17
to mustache.java
Hi Sam, 

     There is no view layer attached in between the template and the raw data. templates are stored in our system from a source. we need to identify the variables in the template to collect the values from another system. basically, is there a way to extract all variables embedded in a text/html templates. 

Sample plain text template: "Hello your loan is submissted {{loanId}}"

 Hope the above helps.

panikumar malepati

unread,
Jun 2, 2017, 10:02:22 AM6/2/17
to mustache.java
HI all,

    Could you please help if there is a way extract variables {{}} embedded in a plain text/html templates? I only see the way to extract them using string matcher pattern. 

-Thanks,

Sam Pullara

unread,
Jun 2, 2017, 2:13:19 PM6/2/17
to mustac...@googlegroups.com
A custom ObjectHandler will get a callback for each variable. Create a factory, set your object handler and then execute. In the callback collect the names of the variables. Use that list of names to create a map with the keys and values. Then use mustache normally with that map. I still don't recommend architecting things this way but it is doable. 

Sam
--

Sam

unread,
Jun 5, 2017, 11:26:13 AM6/5/17
to mustache.java
Based on walking the parse tree you can also do this:


That will let you see the values that are present in the template. This is probably a lot easier than using an ObjectHandler in this case. 

Sam
To unsubscribe from this group and stop receiving emails from it, send an email to mustachejava+unsubscribe@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages