String Template does not allow '[' and ']' as delimeters?

40 views
Skip to first unread message

geoff...@empowerops.com

unread,
Jun 21, 2016, 5:22:55 PM6/21/16
to stringtemplate-discussion, geoff...@empowerops.com
Hey guys,

I was attempting to use String Template to do a little work on a mostly textual view component loaded from FXML. FXML is, as the name implies, an XML derivative, so using angle brackets would cause the parser a great deal of trouble, so I figured I would switch to square brackets, aka editors brackets, because back in my paper writing days I would use notes in square brackets to indicate where i needed to paste some content. Thus I would get the FXML document like this

<ContextualText body="There was a problem with the [component.simpleName], It encountered an error: [component.errorDescription]">

but, much to my shigrin, it seems that StringTemplate has a special case for square brackets as delimeters: the above code throws an exception when you pass `body` into the ST constructor with delimters `'[', ']'`.

Below are a couple of tests I've written to illustrate the problem:

//kotlin
@Test fun `when using non standard delimiters should act properly`(){
val template = ST("hello {target}", '{', '}').apply{
add("target", "String Template!")
}

val result = template.render();

assertThat(result).isEqualTo("hello String Template!")
}

@Test fun `when using square brackets specifically should throw at construction`(){
assertThatThrownBy { ST("hello [target]", '[', ']') }
.isInstanceOf(STException::class.java)
}

the latter also causes StringTemplate's antlr parser to log a lexical error to standard err:
1:13: ']' came as a complete surprise to me

This puts me in kind've a bind, since in FXML I cant use angle brackets, I wish to avoid using curley brackets since its already interpreted by a JEXL-like expression interpreter, and regular parenthesis '(' and ')' are common enough in english that their usage would be problematic. I can go with the sortve conventional dollar as a delimeter, but I've always liked the syntax of a distinct open and close delimeters.

I'm using StringTemplate 4.0.8

Any help much appreciated!

Reply all
Reply to author
Forward
0 new messages