Search code examples
stringtemplatestringtemplate-4

How can StringTemplate import directory be used?


I wrote a sample java file that is located in /src folder.

public class Main {
  public static void main(String[] args){
    STGroupFile StgFile=new STGroupFile("template.stg");
    StgFile.delimiterStartChar = '$';
    StgFile.delimiterStopChar = '$';
    ST webtemp=StgFile.getInstanceOf("test");
    webtemp.add("x","whyyy");
    System.out.println(webtemp.render());
  }
}

Also I have the file template.stg that is located in /src folder also

import "headers/header.stg" 
test(x) ::= << $included(x)$ >>

and the file header.stg that is located in /src/headers folder.

included(x) ::= << headers[$x$] >>

The result obviously is

headers[whyyy]  

but if I try to import whole headers directory making the template.stg like that:

  import "headers" 
  test(x) ::= << $included(x)$ >>
  • the included subtemplate can't be used...

The version of StringTemplate I use is the latest 4.0.5 and according to the link http://www.antlr.org/wiki/display/ST4/Differences+between+v3+and+v4 importing a directory is allowed.. I have even used / before and/or after / - relative/absolute paths and it doesn't work :( Any Help please? Thanks :)


Solution

  • Have you tried $header/included(x)$?