StringTemplate初步使用

时间:2023-03-09 07:18:09
StringTemplate初步使用

例子1:

    private static void test_1(String[] args) throws Exception {
ST hello = new ST("Hello, <name>");
hello.add("name", "World");
System.out.println(hello.render());
}

例子1输出:

Hello, World

例子2:

    private static void test_2(String[] args) throws Exception {

        String g =
"a(x) ::= <<foo>>\n"+
"b() ::= <<bar>>\n";
STGroup group = new STGroupString(g);
ST st = group.getInstanceOf("a");
String result = st.render(); System.out.println(result);
}

例子2输出:

foo

例子3:

    private static void test_3(String[] args) throws Exception {

        STGroup group = new STGroupFile("./src/com/test/stringtemplate/a.stg");
ST st = group.getInstanceOf("a");
String result = st.render(); System.out.println(result);
}

例子3输出:

foo