Stringtemplate compare strings does not work

前端 未结 2 973
花落未央
花落未央 2021-01-19 02:13

Can someone explain why this does not work ?

StringTemplate query = new StringTemplate(\"hello \" +  
                \"$if(param==\\\"val1\\\")$\" +  
              


        
相关标签:
2条回答
  • 2021-01-19 03:06

    ST doesn't allow computation in the templates. That would make it part of the model.

    0 讨论(0)
  • 2021-01-19 03:18

    You can't compare strings inside stringtemplate, unfortunately, but you can send a result of such a comparison into template as a parameter:

    StringTemplate query = new StringTemplate("hello " +  
                    "$if(paramEquals)$" +  
                    " it works! " +  
                    "$endif$ " +  
                    "world");  
            query.setAttribute("paramEquals", param.equals("val1"));  
            System.out.println("result: "+query.toString());
    

    It might not be what you're looking for, since every time you need to add a comparison you have to pass an extra parameter, and for loops it's even worse. But this is one workaround that may work for simple cases.

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