[stringtemplate-interest] ST4 - $ delimiters

Terence Parr parrt at cs.usfca.edu
Sat Jan 23 11:55:57 PST 2010


whoops. a faulty set of unit tests left this hole.  Please try downloading  trunk again. enclosing some unit tests to show how to do $..$

thanks for trying it out!

Ter
   @Test public void testAttr() throws Exception {
        String template = "hi $name$!";
        ST st = new ST(template, '$', '$');
        st.add("name", "Ter");
        String expected = "hi Ter!";
        String result = st.render();
        assertEquals(expected, result);
    }

    @Test public void testParallelMap() throws Exception {
        STGroup group = new STGroup('$', '$');
        group.defineTemplate("test", "hi $names,phones:{n,p | $n$:$p$;}$");
        ST st = group.getInstanceOf("test");
        st.add("names", "Ter");
        st.add("names", "Tom");
        st.add("names", "Sumana");
        st.add("phones", "x5001");
        st.add("phones", "x5002");
        st.add("phones", "x5003");
        String expected =
            "hi Ter:x5001;Tom:x5002;Sumana:x5003;";
        String result = st.render();
        assertEquals(expected, result);
    }

    @Test public void testRefToAnotherTemplateInSameGroup() throws Exception {
        String dir = getRandomDir();
        String a = "a() ::= << $b()$ >>\n";
        String b = "b() ::= <<bar>>\n";
        writeFile(dir, "a.st", a);
        writeFile(dir, "b.st", b);
        STGroup group = new STGroupDir(dir, '$', '$');
        ST st = group.getInstanceOf("a");
        String expected = " bar ";
        String result = st.render();
        assertEquals(expected, result);
    }

    @Test public void testDefaultArgument() throws Exception {
        String templates =
                "method(name) ::= <<"+newline+
                "$stat(...)$" +newline+
                ">>"+newline+
                "stat(name,value=\"99\") ::= \"x=$value$; // $name$\""+newline
                ;
        writeFile(tmpdir, "group.stg", templates);
        STGroup group = new STGroupFile(tmpdir+"/group.stg", '$', '$');
        ST b = group.getInstanceOf("method");
        b.add("name", "foo");
        String expecting = "x=99; // foo";
        String result = b.render();
        assertEquals(expecting, result);
    }

On Jan 23, 2010, at 7:00 AM, Adrian Cuthbertson wrote:

> Hi,
> 
> I'm just experimenting with ST4 which I'd like to use for generating some html.
> What is the correct way of changing the delimiter to a $? I tried the
> following...
> 
> public class G {
> 	public static STGroup getG() {
> 		STGroup g = new STGroup();
> 		g.delimiterStartChar = '$';
> 		g.delimiterStopChar = '$';
> 		return g;
> 	}
> 	public static void main(String[] args) {
> 		STGroup g = G.getG();
> 		g.defineTemplate("t", "Hello $name$!");
> 		ST t = g.getInstanceOf("t");
> 		t.add("name","Fred");
> 		t.add("name","Blogs");
> 		System.out.println(t.render());
> 	}
> }
> 
> But I get "Hello $name$!" as output.
> 
> Thanks for any assistance!
> 
> Regards, Adrian.
> _______________________________________________
> 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