How to output ${expression} in Freemarker without it being interpreted?

后端 未结 8 1121
隐瞒了意图╮
隐瞒了意图╮ 2020-12-05 06:39

I\'m trying to use Freemarker in conjunction with jQuery Templates.

Both frameworks use dollar sign/curly brackets to identify expressions for substitution (or as th

相关标签:
8条回答
  • 2020-12-05 07:14

    This should print ${person.name}:

    ${r"${person.name}"}
    

    From the freemarker docs

    A special kind of string literals is the raw string literals. In raw string literals, backslash and ${ have no special meaning, they are considered as plain characters. To indicate that a string literal is a raw string literal, you have to put an r directly before the opening quotation mark or apostrophe-quote

    0 讨论(0)
  • 2020-12-05 07:20

    I had to spent some time to figure out the following scenarios to escape ${expression} -

    • In Freemarker assignment:

    <#assign var = r"${expression}">

    • In html attribute:

    <a href="/user/${r"${expression}"}"> Some link </a>

    • In Freemarker concatenation:

    <#assign x = "something&"+r"${expression}"/>

    0 讨论(0)
  • 2020-12-05 07:22

    If ${ is your only problem, then you could use the alternate syntax in the jQuery Templates plugin like this: {{= person.name}}

    Maybe a little cleaner than escaping it.

    0 讨论(0)
  • 2020-12-05 07:25

    Did you try $$?

    I found from the Freemarker manual that ${r"${person.name}"} will print out ${person.name} without attempting to render it.

    Perhaps you should also take a look at Freemarker escaping freemarker

    0 讨论(0)
  • 2020-12-05 07:28

    I can confirm that the

    ${r"${item.id}"}
    

    is the correct way as an example.

    So I kinda full example will look like

    <span><a href="/user/user-remove/${r"${item.id}"}"> Remove </a></span>
    

    and the output will be :

    <span><a href="/user/user-remove/${item.id}"> Remove </a></span>
    
    0 讨论(0)
  • 2020-12-05 07:29

    In the case when you want to use non-raw strings so that you can escape double quotes, apostrophes, etc, you can do the following:

    Imagine that you want to use the string ${Hello}-"My friend's friend" inside of a string. You cannot do that with raw strings. What I have used that works is:

    ${"\x0024{Hello}-\"My friend's friend\""}
    

    I have not escaped the apostrophe since I used double quotes.

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