Question

I'm trying to get the simple example to work, here is the sample code .stg file

group list-demo;

htmListExample(xmen) ::= <<
Example 5:
<html>
    <body>
    <h1>Original X-Men</h1>
        <ul>
            $xmen:listItem()$
        </ul>
        </body>
    </html>

    >>

    listItem() ::= <<
    <li>$it$</li>
>>

My Java code:

STGroup group = new STGroupFile("/myTemplate2.stg",'$','$');
ST template = group.getInstanceOf("htmListExample");
List<String> xmen = Arrays.asList("Jean Gray", "Cyclops");
template.add("xmen", xmen);
System.out.println(template.render().toString());

And the output:

 context [/htmListExample] 6:18 passed 1 arg(s) to template /listItem with 0 declared arg(s)
 context [/htmListExample] 6:13 passed 1 arg(s) to template /listItem with 0 declared arg(s)
 context [/htmListExample] 6:13 passed 1 arg(s) to template /listItem with 0 declared arg(s)
 context [/htmListExample /listItem] 2:5 attribute it isn't defined
 context [/htmListExample /listItem] 2:5 attribute it isn't defined
Example 5:
<html>
    <body>
        <h1>Original X-Men</h1>
        <ul>
            <li></li>
            <li></li>
        </ul>
    </body>
</html>

Can anyone shed some light as to why the listItem() is not recognized? I'm using the ST-4.0.7.jar.

Thanks

Was it helpful?

Solution

In StringTemplate 4, the map operator : maps a collection to a template that takes one argument. You need to declare the it argument for the listItem template:

listItem(it) ::= <<
<li>$it$</li>
>>

The warnings which you saw in the output say this:

  1. ST4 expected a template with 1 parameter, but you passed it listItem which takes 0 parameters.
  2. You didn't declare the it parameter, but you referenced it within listItem.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top