[stringtemplate-interest] Angel brackets and HTML/XML

Leo R. Lundgren leo at finalresort.org
Sun Mar 7 07:17:10 PST 2010


Hello,

I just started trying out ST and it seems really nice. But I  
encountered an issue when using <..> as delimiters in a HTML template.  
I've been searching and reading but can't find a clear explanation as  
to why this is happening, and if there is a workaround.


In short; my template looks like this:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd 
">

<html lang="sv-SE">
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
		<title>Test</title>
	</head>
	<body>
		<h2>Hello <name></h2>
		<button id="uploadButton">Upload file</button>
	</body>
</html>


And my Java code like this:

	protected void doGet(HttpServletRequest request, HttpServletResponse  
response) throws ServletException, IOException {
	    String templatePath = getServletContext().getRealPath("/WEB-INF/ 
view");
	    StringTemplateGroup group =  new StringTemplateGroup("view",  
templatePath, AngleBracketTemplateLexer.class);
	    group.setRefreshInterval(0);

	    StringTemplate view = group.getInstanceOf("index");
		view.setAttribute("name", "World");

		PrintWriter out = response.getWriter();
		out.write(view.toString());
	}


When requesting the page, i get this exception:

problem parsing template 'index'
line 12:6: expecting '!', found 'l'
	at  
org 
.antlr 
.stringtemplate 
.language 
.AngleBracketTemplateLexer.nextToken(AngleBracketTemplateLexer.java:149)
	at antlr.TokenBuffer.fill(TokenBuffer.java:69)
	at antlr.TokenBuffer.LA(TokenBuffer.java:80)
	at antlr.LLkParser.LA(LLkParser.java:52)
	at  
org 
.antlr 
.stringtemplate.language.TemplateParser.template(TemplateParser.java: 
103)
	at  
org 
.antlr 
.stringtemplate 
.StringTemplate.breakTemplateIntoChunks(StringTemplate.java:850)
	at  
org 
.antlr.stringtemplate.StringTemplate.setTemplate(StringTemplate.java: 
441)
	at  
org 
.antlr 
.stringtemplate 
.StringTemplateGroup.defineTemplate(StringTemplateGroup.java:679)
	at  
org 
.antlr 
.stringtemplate 
.StringTemplateGroup.loadTemplate(StringTemplateGroup.java:553)
	at  
org 
.antlr 
.stringtemplate 
.StringTemplateGroup.loadTemplate(StringTemplateGroup.java:629)
	at  
org 
.antlr 
.stringtemplate 
.StringTemplateGroup 
.loadTemplateFromBeneathRootDirOrCLASSPATH(StringTemplateGroup.java:597)
	at  
org 
.antlr 
.stringtemplate 
.StringTemplateGroup.lookupTemplate(StringTemplateGroup.java:480)
	at  
org 
.antlr 
.stringtemplate 
.StringTemplateGroup.getInstanceOf(StringTemplateGroup.java:392)
	at  
org 
.antlr 
.stringtemplate 
.StringTemplateGroup.getInstanceOf(StringTemplateGroup.java:404)
	...


Apparently the problem is the first line in the template, where  
there's a ! in the first tag.
But even if I remove the ! (not valid, I know, just for testing) I get  
the parsing error (only the "problem parsing template 'index'" message  
and stacktrace).
If I remove everything above the <h2> line, I get output.
In any case, if I use $$ notation instead of <..>, everything works.

So, is possible to use <..> when making HTML or XML templates, without  
getting errors like this, or is it not recommended?

The reason I'm even considering <..> is because the Five Minute  
Introduction says they are the standard delimiters since version 2.3.
However, on the "Setting the expression delimiters" page in the wiki,  
it says the opposite.
It seems to me that the latter is correct, can someone verify?

In any case, I think <..> looks slightly cleaner than $$ all over the  
place, but it certianly does clash with the tags in general, so maybe  
it's better to skip them.

Thanks,

-|



More information about the stringtemplate-interest mailing list