Remove HTML tags in Freemarker Template

前端 未结 2 1166
半阙折子戏
半阙折子戏 2021-01-24 03:43

I\'ve got a freemarker template that displays the same string in a context where HTML is allowed as well as a context where it is not.

Is there a built-in in Freemarker

2条回答
  •  粉色の甜心
    2021-01-24 04:13

    You could use the Freemarker built-in string replace function with the "r" flag to enable regular expressions.

    Here's a simple regexp that does the trick:

    ${page_title?replace('<[^>]+>','','r')}
    

    Note that if you use backslashes within the regular expression they must be escaped, as follows (contrived example that removes whitespace):

    ${page_title?replace('\\s+','','r')}
    

提交回复
热议问题