Freemarker escaping freemarker - freemarker

I'm using freemarker to generate a freemarker template. But I need some way to escape freemarker tags.
How would I escape a <#list> tag or a ${expression} ?

You could also use: ${"$"}{expression} if you find the {} nesting confusing.

I'm using the alternative syntax feature. I start the template with [#ftl] and use this syntax.
For the expressions I use the string literal feature: ${r"${expression}"}

You can configure FreeMarker to use [=exp] instead of ${exp} (since 2.3.28), and [#...]/[#...] instead of <#...>|<#...> by setting both the interpolation_syntax and the tag_syntax configuration setting to square_bracket (in the Java API: Configuration cfg; ... cfg.setInterpolationSyntax(Configuration.SQUARE_BRACKET_INTERPOLATION_SYNTAX) and cfg.setTagSyntax(Configuration.SQUARE_BRACKET_TAG_SYNTAX)). Then the syntax doesn't clash with the default syntax.
There's one tricky case; if the template starts with <#ftl>, then it will switch the tag syntax back to angle_bracket. To counter that, just add a [#ftl] line before it.
See also: https://freemarker.apache.org/docs/dgui_misc_alternativesyntax.html

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.

Related

How to use escaped colon in thymeleaf th:text?

This is what happens when I try to use a colon in th:text:
and a backslash doesn't seem to fix it:
How can I use the colon symbol in th:text?
If you want to place a literal into th:text, you have to use single quotes: th:text="'7:00AM'". See documentation here.
(By contrast, something like this th:text="7_00AM" is valid - because it is a literal token. Such strings can only use a subset of characters, but do not need enclosing 's.)

Escape single quote in Xtend template expression

I have a very simple question, but could not figure it out by Google search, please help.
I want to produce this string '\u0000' (note the simple quote marks surrounding it!) using the following simple Xtend method containing a template expression:
def String makeDefaultChar()
{
''''\u0000''''
}
However, this is not accepted as proper syntax (probably because of the four ''''. Is there an escape character for this use case or what is the right syntax?
Thank you in advance!
P.S.
Of course I could use plain Java string like this "'\\u0000'" to achieve the same, but I want to use an Xtend template expression.
My Xtend version is: 2.9.1.v201512180746
There is no "escaping" in template expressions, so you have to use the workaround you mentioned:
'''«"'\\u0000'"»'''
or
'''«"'"»\u0000«"'"»'''
Related discussion: https://groups.google.com/forum/#!topic/xtend-lang/bVZ0nKmQGAI
Single quotes are allowed within Xtend templates as long as they do not occur at the beginning or the end of the template. So a simple workaround is to add an empty expression before/after the single quote:
'''«»'\u0000'«»'''

How to escape the back-tick (`) character in tiddlywiki?

I would like to use the back-tick in regular text (not in a code snippet) in TW5. Is this possible?
It is also possible to use the hex code (`) or the HTML code (&#96) for back-tick
I wanted to display a back-tick in a code block, so used:
<code>`</code>
Using the HTML code looks like:
<code>`</code>
This has the advantage that you're not disabling any parsing rules.
In TiddlyWiki5 you can disable certain parsing rules using the \rules pragma
A pragma is a special component of WikiText that provides control over the way the remaining text is parsed.
http://tiddlywiki.com/#Pragma
So if you add
\rules except codeinline
at the very(!) beginning of your tiddler text, any following backtick symbol in the text is not interpreted as special character.
This comes however at the cost that you cannot use this symbol as wikitext-directive anymore to achieve inline-code for programming snippets. Instead you would need to add the html code tag manually.

Freemarker string escaping doesn't work with default operator

We use Freemarker 2.3.20 and came across a strange behavior, when using the default operator together with string escaping like this:
${picture.#author[0]!""?js_string}
in this case, quotes in the authors value are not escaped if !"" is present.
We need to check first for the value and can't use the default op:
<#if picture.#author[0]??>${picture.#author[0]?js_string}</#if>
this is quite ugly and blown up code.
Is this a bug or a feature?
It's because of the operator precedences. ${picture.#author[0]!""?js_string} means ${picture.#author[0]!(""?js_string)}. What you want is ${(picture.#author[0]!"")?js_string}.

Another way instead of escaping regex patterns?

Usually when my regex patterns look like this:
http://www.microsoft.com/
Then i have to escape it like this:
string.match(/http:\/\/www\.microsoft\.com\//)
Is there another way instead of escaping it like that?
I want to be able to just use it like this http://www.microsoft.com, cause I don't want to escape all the special characters in all my patterns.
Regexp.new(Regexp.quote('http://www.microsoft.com/'))
Regexp.quote simply escapes any characters that have special regexp meaning; it takes and returns a string. Note that . is also special. After quoting, you can append to the regexp as needed before passing to the constructor. A simple example:
Regexp.new(Regexp.quote('http://www.microsoft.com/') + '(.*)')
This adds a capturing group for the rest of the path.
You can also use arbitrary delimiters in Ruby for regular expressions by using %r and defining a character before the regular expression, for example:
%r!http://www.microsoft.com/!
Regexp.quote or Regexp.escape can be used to automatically escape things for you:
https://ruby-doc.org/core/Regexp.html#method-c-escape
The result can be passed to Regexp.new to create a Regexp object, and then you can call the object's .match method and pass it the string to match against (the opposite order from string.match(/regex/)).
You can simply use single quotes for escaping.
string.match('http://www.microsoft.com/')
you can also use %q{} if you need single quotes in the text itself. If you need to have variables extrapolated inside the string, then use %Q{}. That's equivalent to double quotes ".
If the string contains regex expressions (eg: .*?()[]^$) that you want extrapolated, use // or %r{}
For convenience I just define
def regexcape(s)
Regexp.new(Regexp.escape(s))
end

Resources