StringTemplate list of attributes defined for a given template

前端 未结 2 781
误落风尘
误落风尘 2020-12-18 07:19

I am getting started with StringTemplate 4 and I am trying to create a template from a simple string stored in a database. I use something like this:

STGroup         


        
相关标签:
2条回答
  • 2020-12-18 07:50

    The documentation for CompiledST states tokens is only for debug. Not sure what that means.

    ST template = new ST("Hello <username>, how are you? Using <if(condition)>expression<endif> in condition works, and repeating <username> is not a problem.");
    Set<String> expressions = new HashSet<String>();
    TokenStream tokens = template.impl.tokens;
    for (int i = 0; i < tokens.range(); i++) {
        Token token = tokens.get(i);
        if (token.getType() == STLexer.ID) {
            expressions.add(token.getText());
        }
    }
    

    Gives you the Strings username and condition.

    0 讨论(0)
  • 2020-12-18 07:52

    You can use st.impl.formalArguments to access the Map<String, FormalArgument> where the arguments are defined. Note that for some templates this field will be null.

    0 讨论(0)
提交回复
热议问题