
- Report a Bug
- Apache FreeMarker Manual
- Template Language Reference
- Built-in Reference

Built-ins for booleans
- Alpha. index
- Expressions
- #directives
c (for boolean value)
Cn (for boolean value), string (when used with a boolean value).
The c built-in also works on numbers , and on strings !
To provide a background, see Template Author's Guide/Miscellaneous/Formatting for humans, or for computers
The c built-in supports booleans since FreeMarker 2.3.20.
This built-in converts a boolean to a "computer language" literal, as opposed to format it for human reading. This formats is independent of the boolean_format configuration setting, as that setting is meant to specify the format for human readers. Instead, it depends on the c_format setting . However, currently all c_format that's built into FreeMarker result will give true or false .
When outputting boolean literals for JavaScript, JSON, Java, and many other languages, always use this built-in, instead of relying on boolean_format .
If you only generate output that's computer language and isn't read by end-users, you may prefer to set the boolean_format configuration setting to c (since FreeMarker 2.3.29), in which case ${ aBoolean } will have the same output as ${ aBoolean ?c} .
If the value the c built-in is applied on is null /missing, it will stop the template processing with error, just like most other built-ins. If instead you want to output a null literal, see the cn built-in .
This does the same as the c built-in , but when applied on a null /missing value, it will output a null value according the c_format setting . See more details about formatting a null here .
Converts a boolean to a string. You can use it in two ways:
As foo?string("yes", "no") : Formats the boolean value to the first parameter (here: "yes" ) if the boolean is true, and to the second parameter (here: "no" ) if it's false. Unless you only meant to format a boolean with simple literals, use ?then( whenTrue , whenFalse ) instead, as that has less type limitations, and it evaluate its parameters lazily! The return value of ?string is always a string (unlike for ?then ), because if the parameters aren't strings, they will be converted to strings. Also note that both parameters are evaluated (unlike for ?then ), despite that only one of them will be used; this might has negative impact if the parameters aren't just literals.
foo?string : Deprecated starting from FreeMarker 2.3.20: use ?c instead, or set the boolean_format setting to something like "yes,no" and then the conversion can happen automatically . If you still need to know about this, this will convert the boolean to string using the default strings for representing true and false values. By default, true is rendered as "true" and false is rendered as "false" . This is mostly only useful if you generate source code with FreeMarker (but use ?c for that starting from 2.3.20) . To change these default strings, you can use the boolean_format setting .
Note that in the very rare case when a value is multi-typed and is both a boolean and a string, then the string value of the variable will be returned, and so the boolean_format setting will have no effect.
This built-in exists since FreeMarker 2.3.23.
Used like booleanExp ?then( whenTrue , whenFalse ) , fills the same role as the ternary operator in C-like languages (i.e., booleanExp ? whenTrue : whenFalse ). If booleanExp evaluates to boolean true then it evaluates and returns its first argument, or else if booleanExp evaluates to boolean false then it evaluates and return its second argument. Off course, all three expression can be arbitrary complex. The argument expressions can have any type, even different types.
An important special property of this built-in is that only one of the argument expressions will be evaluated. This is unlike with normal method calls, where all argument expressions are evaluated, regardless if the method will need them. This also means that the argument that's not needed can even refer to missing variables without causing error. (It still can't be syntactically invalid of course.)
If you need to choose based on a non-boolean value, you should use the switch built-in instead of nesting multiple then -s into each other, like priority?switch(1, "low", 2, "medium", 3, "high") , or even true?switch(priority <= 1, "low", priority == 2, "medium", priority >= 3, "high") .
- What is FreeMarker?
- Version history
- Privacy policy
Often used / Reference
- Try template online
- Expressions cheatsheet
- .special_vars
- Configuration settings
- Github project page
- Report a bug
- Report security vulnerability
- Get help on StackOverflow
- Announcements on Twitter
- Discuss on mailing lists
- Stack Overflow
Last generated: 2023-01-15 14:59:35 GMT , for Freemarker 2.3.32
© 1999 –2023 The Apache Software Foundation . Apache FreeMarker, FreeMarker, Apache Incubator, Apache, the Apache FreeMarker logo are trademarks of The Apache Software Foundation. All other marks mentioned may be trademarks or registered trademarks of their respective owners.
- Stack Overflow Public questions & answers
- Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers
- Talent Build your employer brand
- Advertising Reach developers & technologists worldwide
- About the company
Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.

Freemarker macro with boolean variable
I am using Freemarker 2.3.28 in Spring and Freemarker application. I have a trouble getting booleans working with freemarker macros.
I getting a JSON from server-side which has one attribute as boolean (verified with JS typeof function). I am putting this data in a template using:
var3 in this expression is coming as boolean, which I want to use for some checkbox expression to make it checked or unchecked.
Now when I use this as macro-expression as follows, it is not picking the var3 as boolean:
I get the error when I use ?string:
If I use ?then it gives error:
- boolean-expression
I suppose FreeMarker is evaluted before the {{...}} parts, so the FreeMarker macro literally gets "{{var_id}}" , which is a string literal. So the basic approach of the problem seems to be incorrect.
Also, even if Handlebars is evaluated first, you end up with things like var3="true" , which is again a string literal in FreeMarker, because of the quotation marks around it.
Your Answer
Sign up or log in, post as a guest.
Required, but never shown
By clicking “Post Your Answer”, you agree to our terms of service , privacy policy and cookie policy
Not the answer you're looking for? Browse other questions tagged macros freemarker boolean-expression or ask your own question .
- The Overflow Blog
- How Intuit democratizes AI development across teams through reusability sponsored post
- The nature of simulating nature: A Q&A with IBM Quantum researcher Dr. Jamie...
- Featured on Meta
- We've added a "Necessary cookies only" option to the cookie consent popup
- Launching the CI/CD and R Collectives and community editing features for...
- Staging Ground Beta 1 Recap, and Reviewers needed for Beta 2
- The [amazon] tag is being burninated
- Temporary policy: ChatGPT is banned
Hot Network Questions
- Replacing broken pins/legs on a DIP IC package
- Is it suspicious or odd to stand by the gate of a GA airport watching the planes?
- Surly Straggler vs. other types of steel frames
- Should I put my dog down to help the homeless?
- Is there a single-word adjective for "having exceptionally strong moral principles"?
- What am I doing wrong here in the PlotLegends specification?
- What Is the Difference Between 'Man' And 'Son of Man' in Num 23:19?
- What did Ctrl+NumLock do?
- Can I tell police to wait and call a lawyer when served with a search warrant?
- Why does Mister Mxyzptlk need to have a weakness in the comics?
- Biodiversity through radiation
- Do new devs get fired if they can't solve a certain bug?
- What sort of strategies would a medieval military use against a fantasy giant?
- Do roots of these polynomials approach the negative of the Euler-Mascheroni constant?
- How to handle a hobby that makes income in US
- How to match a specific column position till the end of line?
- ncdu: What's going on with this second size column?
- How can we prove that the supernatural or paranormal doesn't exist?
- Tips for golfing in SVG
- Is there a solution to add special characters from software and how to do it
- Does ZnSO4 + H2 at high pressure reverses to Zn + H2SO4?
- Is a PhD visitor considered as a visiting scholar?
- Describing the homotopy explicitly.
- Why do academics stay as adjuncts for years rather than move around?
Your privacy
By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy .
Page Contents
Description
- name : name of the variable. It is not expression. However, it can be written as a string literal, which is useful if the variable name contains reserved characters, for example <#assign "foo-bar" = 1> . Note that this string literal does not expand interpolations (as "${foo}" ).
- value : the value to store. Expression.
- namespacehash : a hash that was created for a namespace (by import ). Expression.
With this you can create a new variable, or replace an existing variable. Note that only top-level variables can be created/replaced (i.e. you can't create/replace some_hash.subvar , but some_hash ).
For more information about variables, read this: Template Author's Guide/Miscellaneous/Defining variables in the template
Example: variable seasons will store a sequence:
Example: Increments the numerical value stored in variable test :
As a convenience feature, you can do more assignments with one assign tag. For example this will do the same as the two previous examples:
If you know what namespaces are: assign directive creates variables in namespaces. Normally it creates the variable in the current namespace (i.e. in the namespace associated with the template where the tag is). However, if you use in namespacehash then you can create/replace a variable of another namespace than the current namespace. For example, here you create/replace variable bgColor of the namespace used for /mylib.ftl :
An extreme usage of assign is when it captures the output generated between its start-tag and end-tag. That is, things that are printed between the tags will not be shown on the page, but will be stored in the variable. For example:
will print:
Please note that you should not to use this to insert variables into strings:
You should simply write:

IMAGES
VIDEO
COMMENTS
=: Assignment operator. It can also be one of the assignment shorthand operators (since FreeMarker 2.3.23): ++, -- , +=, -= , *=, /= or %=. Like <#assign x++> is similar to <#assign x = x + 1>, and <#assign x += 2> is the same as <#assign x = x + 2> .
If you only generate output that's computer language and isn't read by end-users, you may prefer to set the boolean_format configuration setting to c (since FreeMarker 2.3.29), in which case $ { aBoolean } will have the same output as $ { aBoolean ?c}.
Starting from FreeMarker 2.3.20, if you want to print true/false (because you are generating JavaScript or such), write $ {booleanVar?c} ( ?c for "computer format", also used for numbers). $ {booleanVar?string} is dangerous for that, since somebody can set the boolean_format setting to yes,no or something...
FreeMarker template error: For "?then (...)" left-hand operand: Expected a boolean, but this has evaluated to a string (wrapper: f.t.SimpleScalar): ==> var3 macros freemarker boolean-expression Share Improve this question Follow asked Apr 20, 2020 at 10:31 gpsingh 11 3 Add a comment 1 Answer Sorted by: 0
When setting up a conditional statement you must begin by assigning a variable for any fields you want to reference using the #assignFreemarker Tag. At this point, the values for those variables are checked, a condition is included to account for edge cases, and the conditional statement is closed out.
You can use it in two ways: As foo?string ("yes", "no"): This will return the first parameter (here: "yes") if the boolean is true, otherwise the second parameter (here: "no" ). Note that the return value is always a string; if the parameters were numbers, they would be converted to strings first.
It's very important to keep in mind that FreeMarker considers >= and > as closing characters for an FTL tag. The solution is to wrap their usage in parentheses or use gte or gt instead. Putting it together, for the following template: <#if status??> <p> $ {status.reason} </p> <#else> <p> Missing status! </p> </#if>
name: name of the variable.It is not expression. However, it can be written as a string literal, which is useful if the variable name contains reserved characters, for example <#assign "foo-bar" = 1>.Note that this string literal does not expand interpolations (as "${foo}").
By default, FreeMarker templates have access to several variables defined in init.ftl that you can use in your themes to access several theme objects, settings, and resources. Several of these variables are listed below for reference: Common Variables URLs Page Logo Navigation My Sites Includes Date
FreeMarker has built-in functions to detect for variables. The most common method of detecting for empty or null values uses the has_content function and the trim function. The has_content function, in the case of Accelerator, returns with a boolean value determining whether or not the provided variable exists. Using the function looks like this: