[antlr-interest] Getting foreach working with StringTemplate for C#

Micheal J open.zone at virgin.net
Fri Feb 29 16:18:26 PST 2008


> Hi,
> 
> I'm still struggling with StringTemplate for C#, having 
> looked through the lists, searched the web, and looked at the 
> source code.

Did you try the wiki?
http://www.antlr.org/wiki/display/ST/StringTemplate+Wiki+Home

> I can't get the following working (much simplified version of 
> what I'm trying to achieve):
> 
>   string tpl = "$foreach f in foo$ $f$ $end$";
>   //string tpl = "$foo$";
> 			
>   StringTemplate st = new StringTemplate(tpl);
>   st.SetAttribute("foo", new string[] {"a", "b", "c"});
>   Console.Out.WriteLine(st.ToString());
> 
> If I switch the comment onto the first line, I get the array 
> collapsed into a string with no separators.
> 
> In my "real" scenario, which works with simple values, I just 
> can't merge in arrays, lists, or anything enumerable.  I hope 
> I'm doing something wrong -- that someone can explain to me 
> -- because it seems such a basic task, I have trouble 
> believing that it isn't implemented...!
> 
> In the "real" scenario, I've also tried with group files and 
> varying "dollar" and "angle bracket syntaxes", with no luck.
> 
> Can anyone help?
 

Sure. The template text you are supplying is NOT valid ST syntax. See
[http://www.antlr.org/wiki/display/ST/Five+minute+Introduction] for a basic
ST introduction (the basic syntax cheat sheet is useful for beginners)

Rather than:

	$foreach f in foo$ $f$ $end$


Try (either of) these instead:

	$foo:{f| $f$ }$ 

	OR

	<foo:{f| <f> }> 


Micheal



More information about the antlr-interest mailing list