Search code examples
typescriptoperatorsfreemarker

Freemarker equivalent of typescript optional chaining "?"


Typescript has a nice operator ? (called optional chaining) that check empty attributes in chains of dots.

Example

x?.y?.w?.z

means (more or less)

if (x !== undefined && x !== null &&
    x.y !== undefined && x.y !== null &&
    x.y.w !== undefined && x.y.w !== null)
then x.y.w.z
else null

Is there something similar in FreeMarker template language?

(please notice that ? Freemarker operator has a completely different meaning).

I tried in docs https://freemarker.apache.org/docs/dgui_template_exp.html but I had no luck.


Solution

  • Closest is combining the binary ! operator (often used like maybeMissing!'sine default') with (), like (x.y.z)!'some default'. Also you can omit the default value, like (x.y.z)!, if you just want it to be an empty string, 0, or false.