title: StringTemplate4 and anonymous templates
body: <p>I have a problem with the recently <a href="http://www.antlr.org/wiki/display/ST/StringTemplate+4+Documentation" rel="nofollow">released beta version of Stringtemplate 4</a>.</p>
  
  <p>In StringTemplate 3, I used to have templates like</p>
  
  <pre><code>&lt;env_vars:{ var | void* &lt;var&gt;() { return null; }}&gt;
  </code></pre>
  
  <p>i.e. anonymous templates containing a literal closing brace ("}"), and these worked well enough in ST3 without escaping the first closing brace.</p>
  
  <p>With ST4, I'm getting a NPE (the test case below prints the stack trace). I can get it to work by escaping the closing literal brace, so the template looks like this (notice the quoting backslash in front of the first closing brace):</p>
  
  <pre><code>&lt;env_vars:{ var | void* &lt;var&gt;() { return null; \}}&gt;
  </code></pre>
  
  <p>But it seems a bit ugly; I've always admired ST's non-intrusive syntax and having to match each "{" to be output with a corresponding "\}" somehow seems very asymmetrical.</p>
  
  <p>Is there something I'm missing or is this an expected change in behavior from ST3?</p>
  
  <p>Test case:</p>
  
  <pre>
  import org.stringtemplate.v4.ST;
  
  public class ST4Test
  {
    public static void main(final String[] args)
    {
      final String[] env_vars = new String[]{"one", "two", "three"};
  
      try
      {
  
        // This used to work in ST3, but fails in ST4.
        final ST failingST = new ST("&lt;env_vars:{ var | void* &lt;var&gt;() { return null; }}\n&gt;");
        failingST.add("env_vars", env_vars);
  
        System.out.printf("%s\n", failingST.render());
      }
      catch (Exception ex)
      {
        // The failing example results in a NPE
        ex.printStackTrace();
      }
  
      // This works, but requires quoting the "}" contained within the anonymous
      // template, which I find a bit disturbing, considering that I use ST for
      // generating C-code, which tends to use the occasional "}", along with the
      // fact that this used to work in ST3.
      final ST workingST = new ST("&lt;env_vars:{ var | void* &lt;var&gt;() { return null; \\}\n}&gt;");
      workingST.add("env_vars", env_vars);
      System.out.printf("%s\n", workingST.render());
    }
  }
  </pre>