Search code examples
javaantlrantlr4stringtemplatestringtemplate-4

Java StringTemplate Using File From Outside Directory


I have a directory structure like this:

Templates/
├── Foo/
│   ├── Foo.st
├── Signature.st

Here's what the Foo.st looks like:

<?xml version="1.0" encoding="UTF-8"?>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
</head>
<body>
    <div id="body">
        <p> Some Text </p>
    </div>
    $Signature()$
</body>
</html>

Here's my Java code with StringTemplate:

STRawGroupDir dir = new STRawGroupDir("Templates", '$', '$');
ST st = dir.getInstanceOf("Foo/Foo");
System.out.println(st.render());

But the output is:

<?xml version="1.0" encoding="UTF-8"?>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
</head>
<body>
    <div id="body">
        <p> Some Text </p>
    </div>
</body>
</html>

How do I get the Foo template to be able to properly reference the signature template?

If I put Signature.st inside the Foo directory then the above code works just fine, but I can't do that as I'll have many templates that will reference the Signature template.


Solution

  • Try:

    ...
    $/Signature()$
    ...
    

    Template calls are resolved relative to the calling template. Starting a template call with prefix / will make the template call absolute - which is what you expect.