简体   繁体   中英

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.

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.

I figured out a really easy way to do this. I have now the following directory structure:

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

Here's my Main.st

$ templates : { template |
$(template)()$
}$

$Signature()$

And here's my java code:

STRawGroupDir dir = new STRawGroupDir("Templates", '$', '$');
ST st = dir.getInstanceOf("Main");
st.add("templates", Arrays.asList("/Foo/Foo.st"));
st.render();

Now I can pass in any number of templates and this works perfectly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM