Will Hains
Will Hains

Reputation: 1076

How to escape template syntax in templates

I am making a page in my Play app that talks about how to make Play app pages. (Very meta :P)

I couldn't find anything in the Play Framework documentation that explains how to escape Play's template syntax in a template. Does anyone know how to do this?

Upvotes: 4

Views: 9106

Answers (3)

guy mograbi
guy mograbi

Reputation: 28638

Lets say you want to display ${name} as HTML text and not let play render a value for variable name then the answer to the question can be found when letting play compile ${"$"} in the template.

Play produces an error stating:

 illegal string body character after dollar sign; solution: either escape a literal dollar sign "\$5" or bracket the value expression "${5}"

This actually means that if you want to display ${name} you should write the following

${"\${name}"}

Try this, it works!

Upvotes: 8

Will Hains
Will Hains

Reputation: 1076

Came up with an interesting solution for this:

${"#{a @Controller.action()}${user.name}#{/a}"}

Play automatically escapes the contents of the string to HTML entities. This ultimately requires less extra code, and as a bonus it means I can copy & paste template code as-is into the page.

I hope this is helpful to someone. :)

Upvotes: 1

grahamrb
grahamrb

Reputation: 2269

Perhaps not the most elegant solution but if you replace the $ in any of your tags with the ascii escaped equivalent then it works, eg, if I want to print ${user.name} out on the screen then the text I would enter would be:

${user.name}

Obviously the same goes for any other special characters that the templating language uses as a trigger eg #, @ and %

Upvotes: 5

Related Questions