Search code examples
stringtemplate

How to do standard layouts with StringTemplate?


With StringTemplate, what is the proper way to have a standard layout template such as:

<head>
..
</head>

<html>

$body()$

</html>

Where I can set the body template from my application, so that every template I use uses this fundamental layout?

Thanks.


Solution

  • I found it hiding in the documentation: http://www.antlr.org/wiki/display/ST/StringTemplate+2.2+Documentation

    "Include template whose name is computed via expr. The argument-list is a list of attribute assignments where each assignment is of the form attribute=expr. Example $(whichFormat)()$ looks up whichFormat's value and uses that as template name. Can also apply an indirect template to an attribute."

    So my main layout template now looks like this:

    <head>
        <title>Sportello</title>
    </head>
    
    <html lang="en-US">
    <body>
        $partials/header()$
        <section>$(body_template)()$</section>
        $partials/footer()$
    </body>
    </html> 
    

    ...to which I pass the subtemplate's name as an attribute.