stringtemplate-4

embed java code inside a template

可紊 提交于 2019-12-06 23:42:34
Is it possible to embed executable java code inside a ST4 template? eg if I want to pass a string to my template, which sometimes prints the string as-is, and sometimes prints it in caps, so I want a java code to do the necessary conversion. Without this feature, I see 3 ways to achieve the same functionality: (1) pre-compute the alternative values in java, and pass them all to the template at one shot (but this may result in too many arguments): // in the below code, if the template can compute DESCRIPTION_CAPS from DESCRIPTION, using embedded java code, that reduces one argument test

Custom format functions for StringTemplate4

不问归期 提交于 2019-12-06 07:28:42
I would like to know how to create a custom format function for string template. Let say I have the following code: render(attributes) :: << <html> $atributes: { <div> $customformat(atribute.name)$</div> } </html> >> customformat(name) ::= << $name; format="upper"$ >> Currently the behaviour of the function customformat is: Input: "hello world" -> Output: "HELLO WORLD" And I would like to modify the customformat function so the output is something like the following: Input: "hello world" -> Output: "HELLO_WORLD" Andy Stabler As far as I'm aware this isn't possible, since StringTemplate is all

NoIndentWriter not respecting whitespace in template

青春壹個敷衍的年華 提交于 2019-12-05 16:09:31
I am trying to use the NoIndentWriter class to get around a problem I am facing related to the inserting of whitespace in multiline Strings . However, it appears that NoIndentWriter does a lot more than just not auto-indenting. In particular it trims all leading (but not trailing whitespace) even if explicitly present in a template. E.g. with the following template file: $ cat src/templates/a.xml.stg ladder(value)::=<< 0 1 2 3 ^value^ >> ... and the following code: public static void main(String args[]) throws Exception { STGroup stGroup = new STGroupFile("templates/a.xml.stg", '^', '^'); ST

Unable to catch STException in StringTemplate 4

喜欢而已 提交于 2019-12-04 17:18:42
I am unable to catch the STException thrown by the STGroupFile. This is a problem. I need to abort if the template is bad. To reproduce this problem, I have this incorrect template file called tmp.stg: temp1(param1)::=<< %if(param1)% %param1:{%temp2(p)%}; separator"\n"% %endif% >> And this groovy code to process it: #!/usr/bin/env groovy @Grab(group="org.antlr", module="ST4", version="4.0.8") import org.stringtemplate.v4.STGroupFile; import org.stringtemplate.v4.NumberRenderer; public class Gex { public static void main(String [] args) { System.out.println("Processing...") File fn = new File(

What is the best way to ensure HTML entities are escaped in StringTemplate

試著忘記壹切 提交于 2019-12-03 22:25:18
问题 Assuming the following string template, is being given a list of Java Bean objects: <ul>$people:{p|<li>$p.name$ $p.email</li>}$</ul> ie the list of people might contain Person objects which you may or may not have the ability to enhance/extend: class Person { .... public getName() { ... } public getEmail() { ... } } The getName() and getEmail() methods don't return sanitised (escaped html entities). How do you get around this? 回答1: You may use a custom renderer, for example: public static

how do I iterate though a java list in stringtemplate?

两盒软妹~` 提交于 2019-12-03 10:54:07
I want to iterate through a hibernate query results inside stringtemplate. I've been looking for examples but I can't find anything. can you please help? thanks The syntax looks like <items :{ item | <item> }> Putting it together in Java: List<String> teams = Arrays.asList("Cats", "Birds", "Turtles"); ST s = new ST( "<teams :{team | <team> }>"); s.add("teams", teams); System.out.println(s.render()); In this example, I iterate over the List and print each team that is in the teams list. The result that would be printed is: Cats Birds Turtles We can explore the syntax that makes this happen.

Filter out empty strings in ST4

蓝咒 提交于 2019-12-02 12:19:38
问题 Consider the following example: <["foo", "", "bar"]; separator=","> This gives the result: foo,,bar But I need: foo,bar Is there any way to filter out empty string values before formating with separator in ST4? (In real code the values come from another template, which has <if> condition and returns empty result for undesired data from model, and I don't whant to move the condition out of that template to keep templates incapsulated/isolated.) 回答1: I've found the workaround with two auxilary

Filter out empty strings in ST4

╄→гoц情女王★ 提交于 2019-12-02 04:37:46
Consider the following example: <["foo", "", "bar"]; separator=","> This gives the result: foo,,bar But I need: foo,bar Is there any way to filter out empty string values before formating with separator in ST4? (In real code the values come from another template, which has <if> condition and returns empty result for undesired data from model, and I don't whant to move the condition out of that template to keep templates incapsulated/isolated.) I've found the workaround with two auxilary things. But it is so creepy... DropEmpty ::= ["": [], default: key] Separated(l, s, w=false) ::= "<if

StringTemplate remove < > as delimiters

[亡魂溺海] 提交于 2019-12-01 17:37:17
I am trying to use StringTemplate in a java web app to generate html emails. When trying to enter line breaks, I use \<br\> This does not seem to be working. \<b\> is not working as well. Is there a way to turn off < and > as delimiters so I can test if this is the issue? All over the StringTemplate documentation it says to use $...$ as your delimiters but doesn't say how to not use <...> When creating a group you can set the delimiters by using STGroup group = new STGroupDir("emails", '$','$'); Where '$' and '$' are your delimiters. Although Lumpy definitely answered the question correctly,

StringTemplate remove < > as delimiters

余生颓废 提交于 2019-12-01 16:24:10
问题 I am trying to use StringTemplate in a java web app to generate html emails. When trying to enter line breaks, I use \<br\> This does not seem to be working. \<b\> is not working as well. Is there a way to turn off < and > as delimiters so I can test if this is the issue? All over the StringTemplate documentation it says to use $...$ as your delimiters but doesn't say how to not use <...> 回答1: When creating a group you can set the delimiters by using STGroup group = new STGroupDir("emails", '