[stringtemplate-interest] Bug/Problem passing parameters into iteration

Terence Parr parrt at cs.usfca.edu
Wed Nov 3 13:51:59 PDT 2010


Hi Mike. quick question/thought.  Are you expecting indent expr to be evaluated for every iteration?  ST tries to avoid that and does indent once before iteration starts.

Ter
On Nov 2, 2010, at 4:55 PM, Mike Goodwin wrote:

> Hello List,
> 
> On the StringTemplate home page it says "Bug reports can be found and
> filed via StringTemplate's JIRA", unfortunately it seems that account
> registration is not possible, so I am not sure about the accuracy of
> that statement. (For me it says contact the list of administrators,
> but then the list of administrators it suggests is empty).
> 
> I have encountered what I believe to be a bug (source+output appended
> below). For me the object Model has its toString() method called an
> formed as part of the call to the template element. I cannot quite see
> why this would be the case. Removing all references to indent make
> this issue go away (but remove the indentation). I cannot see another
> way that I would be able to pass arguments in to the iteration. (I can
> work around it by using a field, and calculating it in the model, but
> this is not ideal).
> 
> I am using StringTemplate 3.2.1.
> 
> regards,
> 
> Mike
> 
> 
> 
> 
> ----------------------- PokeStringTemplate.java ----------------------
> 
> package poke.stringtemplate;
> 
> import java.io.File;
> import java.io.FileWriter;
> import java.io.IOException;
> import java.io.InputStream;
> import java.io.InputStreamReader;
> import java.net.URL;
> import java.util.ArrayList;
> import java.util.List;
> 
> import org.antlr.stringtemplate.StringTemplate;
> import org.antlr.stringtemplate.StringTemplateGroup;
> import org.antlr.stringtemplate.language.DefaultTemplateLexer;
> 
> public class PokeStringTemplate {
> 	
> 	static public class Model{
> 		final String name;
> 		final List<Model> children = new ArrayList();;
> 		Model(Model parent, String name){
> 			if(parent!=null) parent.children.add(this);
> 			this.name = name;
> 		}
> 		public String getName(){ return name; }
> 		public List<Model> getChildren(){ return children; }
> 		@Override public String toString() {
> 			return "Do not want to see this!";
> 		}
> 	}
> 
> 	
> 	static public void main(String[] args) throws IOException {
> 		
> 		Model a = new Model(null,"a");
> 		Model a_b = new Model(a,"b");
> 		Model a_c = new Model(a,"c");
> 		Model a_b_d = new Model(a_b,"d");
> 		Model a_b_e = new Model(a_b,"e");
> 		
> 		StringTemplate t = getTemplate("tree.stg", "tree");
> 		t.setAttribute("root", a);
> 		File out = new File("tree.html");
> 		stringToFile(t+"", out);
> 		
> 	}
> 
> 	static private void stringToFile(String s, File f) throws IOException{
> 		FileWriter w = new FileWriter(f);
> 		w.append(s);
> 		w.close();
> 	}
> 	
>    static private StringTemplateGroup getTemplateGroup(String name)
> throws IOException{
>    	URL url = PokeStringTemplate.class.getResource(name);
>    	InputStream is = url.openStream();
>    	return new StringTemplateGroup(new InputStreamReader(is),
> DefaultTemplateLexer.class);
>    	
>    }
> 
>    static private StringTemplate getTemplate(String file, String
> name) throws IOException{
>    	return getTemplateGroup(file).getInstanceOf(name);
>    }
> }
> 
> 
> ------------------ tree.stg ---------------------------------
> 
> group xmldoc;
> 
> element(indent)::=<<
> $indent$$it.name$<br>
> $indent$$it.children:element(indent=indent+"&nbsp;&nbsp;")$
>>> 
> 
> elementTop()::=<<
> $it.name$<br>
> $it.children:element(indent="&nbsp;&nbsp;")$
>>> 
> 
> tree(root)::=<<
> <html>
> 	<body>
> 		<code>
> $root:elementTop()$
> 		</code>
> 	</body>
> </html>
>>> 
> 
> 
> ------------------ output (copied from rendered html)
> ---------------------------------
> a
>  b
>  Do not want to see this!  d
> Do not want to see this!  Do not want to see this!  e
> Do not want to see this!    c
> _______________________________________________
> stringtemplate-interest mailing list
> stringtemplate-interest at antlr.org
> http://www.antlr.org/mailman/listinfo/stringtemplate-interest



More information about the stringtemplate-interest mailing list