Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 they're called in freemarker, "interpolations") , e.g. ${person.name} .

So when I define a jQuery Template with expressions in that syntax, Freemarker tries to interpret them (and fails).

I've tried various combinations of escaping the ${ sequence to pass it through Freemarker to no avail - \${, \$\{, $\{, etc.

Inserting a freemarker comment in between the dollar and the curly (e.g. $<#-- -->{expression}) DOES work - but I'm looking for a more concise and elegant solution.

Is there a simpler way to get a Freemarker template to output the character sequence ${?

like image 930
Glenn Barnett Avatar asked Mar 05 '11 23:03

Glenn Barnett


People also ask

How do you escape special characters in FreeMarker?

Both quotation mark (") and apostrophe-quoate (') are escaped. Starting from FreeMarker 2.3. 1, it also escapes > as \> (to avoid </script>). Furthermore, all characters under UCS code point 0x20, that has no dedicated escape sequence in JavaScript language, will be replaced with hexadecimal escape (\xXX).

What syntax is used to access data using FreeMarker under a condition?

Special variables are variables defined by the FreeMarker engine itself. To access them, you use the . variable_name syntax.

How do you use the ternary operator in FreeMarker?

When applied to a boolean, the string built-in will act as a ternary operator. It's no very readable as this is not the intended usage of it. It's for formatting boolean values, like Registered: ${registered? string('yes', 'no')} .

How do I comment in FreeMarker template?

Comments: <#-- and --> Comments are similar to HTML comments, but they are delimited by <#-- and -->. Comments will be ignored by FreeMarker, and will not be written to the output.


2 Answers

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

like image 159
javanna Avatar answered Sep 19 '22 15:09

javanna


For longer sections without FreeMarker markup, use <#noparse>...</#noparse>.

Starting with FreeMarker 2.3.28, configure FreeMarker to use square bracket syntax ([=exp]) instead of brace syntax (${exp}) by setting the interpolation_syntax configuration option to square_bracket.

Note that unlike the tag syntax, the interpolation syntax cannot be specified inside the template. Changing the interpolation syntax requires calling the Java API:

Configuration cfg; // ... cfg.setInterpolationSyntax(SQUARE_BRACKET_INTERPOLATION_SYNTAX); 

Then FreeMarker will consider ${exp} to be static text.

Do not confuse interpolation syntax with tag syntax, which also can have square_bracket value, but is independent of the interpolation syntax.

When using FreeMarker-based file PreProcessor (FMPP), either configure the setting via config.fmpp or on the command-line, such as:

fmpp --verbose --interpolation-syntax squareBracket ... 

This will call the appropriate Java API prior to processing the file.

See also:

  • https://freemarker.apache.org/docs/dgui_misc_alternativesyntax.html
  • http://fmpp.sourceforge.net/settings.html#templateSyntax
like image 23
ddekany Avatar answered Sep 20 '22 15:09

ddekany