9

I am trying to create a simple XML-Template which so far only consists of:

<?xml version="1.0"?>

I read the file like this:

    STGroup group = new STGroupDir("templates");
    ST st = group.getInstanceOf("report");
    st.add("analysis", ana);
    String result = st.render();
    System.out.println(result);

And the result is several error messages:

report.st 1:1: invalid character '<'
report.st 1:1: invalid character '?'
report.st 1:19: invalid character '?'
report.st 1:20: invalid character '>'
report.st 1:2: no viable alternative at input 'xml'

I have seen other people reading HTML tempaltes that also use tags. So what am I doing wrong?

er4z0r
  • 4,711
  • 8
  • 42
  • 62
  • It's empty : you just had declared document type. – Artem Oboturov May 02 '12 at 14:09
  • Try to use inspector [http://www.antlr.org/wiki/display/ST4/StringTemplate+Inspector+GUI](http://www.antlr.org/wiki/display/ST4/StringTemplate+Inspector+GUI) – Artem Oboturov May 02 '12 at 14:16
  • 1
    It's not expecting xml file as input. See syntax at [http://www.antlr.org/wiki/display/ST4/StringTemplate+cheat+sheet](http://www.antlr.org/wiki/display/ST4/StringTemplate+cheat+sheet). – Artem Oboturov May 02 '12 at 14:18
  • Sorry, I still don't get it. I tried to escape the < and > but it still would not help. – er4z0r May 02 '12 at 14:53

3 Answers3

6

Okay it seems I overlooked that you need to specify templates in a different snytax. Allthough this was not obvious from the examples I used:

My working template looks different now:

report (analysis) ::= <<
<?xml version="1.0"?>
>>

In addition I also changed the delimeters:

STGroup group = new STGroupDir("templates",'$','$');
er4z0r
  • 4,711
  • 8
  • 42
  • 62
  • That's what I told you when said it wasn't expecting xml at all. – Artem Oboturov May 03 '12 at 07:47
  • 1
    If you want something with a template which is actually a template, look at freemarker. – Artem Oboturov May 03 '12 at 07:59
  • 2
    Yeah. Allthough Terrence Parr keeps saying that StringTemplate is good model/view separation, I cannot imagine a designer that can be bohtered to write templates for this. – er4z0r May 05 '12 at 16:36
  • If you are writing Java code to process the template, use this to load a raw template file by itself: ST st = new ST("your xml here", '$', '$'); then your template will not need to have the template declaration like report(analysis) ::= << ... >> – jbuhacoff May 02 '19 at 19:34
  • 1
    ST by default uses <> as delimiters. You don't have to change anything with your template as long as you pass "custom" delimiters. Note that I am using the latest (4.3) version as we speak. – krizajb Aug 17 '21 at 09:09
3

I've found that you can escape the angle bracket as well:

report(analysis) ::= <<
\<?xml version="1.0"?>
>>

Note the \ right before the <?xml -- additionally it's smart enough not to require another escape at the ?> closing bracket.

And I believe what Terrence Parr is suggesting about model/view separation is that the view never really has an opportunity to manipulate the underlying data structure (or model) passed to the template. This is accomplished by "applying" a template to the data or collection, rather than looping in the template over the data. Perhaps that is subtle, and perhaps practically (in the case of designers) a bit too pure.

lucidquiet
  • 6,124
  • 7
  • 51
  • 88
  • This approached worked like a charm for me (I'm using StringTemplate 4.0.8). I'm voting up since sometimes it can be easier to just add the backslash to the template than replace for than find an unused character. – cleberz Apr 02 '14 at 20:16
2

Alternatively, you can use STRawGroupDir if you don't want to use group file syntax. This class is similar to STGroupDir, but specifically for loading files like XML and HTML.

http://www.stringtemplate.org/api/org/stringtemplate/v4/STRawGroupDir.html

metatheorem
  • 568
  • 1
  • 3
  • 14