-1

How to set ST4 condition from Java?

I have following ST4 template

templateExample(param) ::= <<
$if (condition)$
<ul><li>$param$ is true</li></ul>
$else$
<ul><li>$param$ is false</li></ul>
$endif$
>>

While the main class is

public static void main(String[] args) {
        final STGroup stGroup = new STGroupFile("exampleTemplate.stg", '$', '$');
        stGroup.registerRenderer(String.class, new StringRenderer());

        final ST templateExample = stGroup.getInstanceOf("templateExample");
        templateExample.add("param", "Hello World");
        templateExample.add("condition", true);

        System.out.println(templateExample.render());
    }

However when i run the code, following error raised :

Exception in thread "main" java.lang.IllegalArgumentException: no such attribute: condition
    at org.stringtemplate.v4.ST.add(ST.java:226)
    at com.cupidocreative.main.StringTemplateSandboxMain.main(StringTemplateSandboxMain.java:16)

How to set the "condition" from my java class? Thanks in advance

Timothy
  • 855
  • 1
  • 13
  • 29

1 Answers1

1

here is my solution

templateExample(condition, param) ::= <<
$if (condition)$
<ul><li>$param$ is true</li></ul>
$else$
<ul><li>$param$ is false</li></ul>
$endif$
>>
Timothy
  • 855
  • 1
  • 13
  • 29