0

I'm attempting to use StringTemplate.v4 for simplistic templates, meaning only simple gaps names like %body%--I'm not using any other features, such as if-logic, sub-templates, or expressions.

(To be honest, it's API is poorly documented, and at this point I'm considering abandoning it completely. It would be nice if there were JavaDoc source code links, so at least I could dig around and figure things out myself. Really frustrated.)

I'm trying to determine the gaps that exist in an anonymous template, to verify it has the required gaps before attempting to use it.

   import  java.util.Arrays;
   import  java.util.Map;
   import  java.util.Set;
   import  org.stringtemplate.v4.ST;
   import  org.stringtemplate.v4.compiler.FormalArgument;

public class GapsInAnST  {
   public static final void main(String[] ignored)  {
      ST tmpl = new ST("Hello %name%. You are %age% years old.", '%', '%');

      Map<String,Object> gapMap = tmpl.getAttributes();

      System.out.println("gapMap=" + gapMap);
      if(gapMap != null)  {
         System.out.println("getAttributes()=" + Arrays.toString(gapMap.keySet().toArray()));
      }

      System.out.println("tmpl.impl.hasFormalArgs=" + tmpl.impl.hasFormalArgs);
      Map<String,FormalArgument> formalArgMap = tmpl.impl.formalArguments;
      if(formalArgMap != null)  {
        System.out.println("getAttributes()=" + Arrays.toString(formalArgMap.keySet().toArray()));
      }

      tmpl.add("name", "Seymour");
      tmpl.add("age", "43");

      System.out.println(tmpl.render());
   }
}

Output:

gapMap=null
tmpl.impl.hasFormalArgs=false
Hello Seymour. You are 43 years old.

I found out why getAttributes() returns null in this google groups thread, and about formalArguments in this question: StringTemplate list of attributes defined for a given template).

So how do I get all gaps actually existing in an anonymous template before filling any gaps? I realize I could do it with regex, but I am hoping there is a built in way of doing this.

Thanks.

Community
  • 1
  • 1
aliteralmind
  • 19,847
  • 17
  • 77
  • 108

1 Answers1

-1

I've decided to give up on StringTemplate4. In fact, I just rolled my own, as pretty much all the "lightweight" Java templating solutions all have advanced features (like looping, expressions, logic, template "groups"), and I don't want any of it. I just want gaps (Hi %name%).

It's called Template Featherweight (GitHub link).

Here are two examples:

First, a basic use that renders the completely filled template into a string:

   import  com.github.aliteralmind.templatefeather.FeatherTemplate;
public class HelloFeather  {
   public static final void main(String[] ignored)  {
      String origText = &quot;Hello %name%. I like you, %name%, %pct_num%__PCT__ guaranteed.&quot;;

      String rendered = (new FeatherTemplate(origText,
         null)).                        //debug on=System.out, off=null
            fill(&quot;name&quot;, &quot;Ralph&quot;).
            fill(&quot;pct_num&quot;, 45).
         getFilled();

      System.out.println(rendered);
   }
}

Output:

Hello Ralph. I like you, Ralph, 45% guaranteed.

The second example demonstrates "auto-rendering", which renders the template as its filled, into something other than a string, such as a file or stream--or anything you can wrap into an Appendable:

   import  com.github.aliteralmind.templatefeather.FeatherTemplate;
public class FeatherAutoRenderDemo  {
   public static final void main(String[] ignored)  {
      String origText = &quot;Hello %name%. I like you, %name%, %pct_num%__PCT__ guaranteed.&quot;;

      FeatherTemplate tmpl = new FeatherTemplate(origText,
         null);  //debug on=System.out, off=null

      tmpl.setAutoRenderer(System.out);

      System.out.println(&quot;<--Auto renderer set.&quot;);
      tmpl.fill(&quot;name&quot;, &quot;Ralph&quot;);

      System.out.println(&quot;<--Filled first gap&quot;);
      tmpl.fill(&quot;pct_num&quot;, 45);

      System.out.println(&quot;<--Filled second-and-final gap&quot;);
   }
}

Output:

Hello <--Auto renderer set.
Ralph. I like you, Ralph, <--Filled first gap
45% guaranteed.<--Filled second-and-final gap
aliteralmind
  • 19,847
  • 17
  • 77
  • 108