FreeMarker

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

Built-ins for booleans

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") .

Often used / Reference

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.

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:

gpsingh's user avatar

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.

ddekany's user avatar

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 .

Hot Network Questions

freemarker assign boolean variable

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

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

  1. Introduction and use of freemarker

    freemarker assign boolean variable

  2. 😊 Freemarker assign. freemarker. 2019-01-22

    freemarker assign boolean variable

  3. freemarker assign 声明变量,boolean,date,date日期格式转换_mexican_jacky的博客-CSDN博客

    freemarker assign boolean variable

  4. Is there a way to toggle a boolean variable in C#?

    freemarker assign boolean variable

  5. Java Booleans 1: Creating two boolean variables

    freemarker assign boolean variable

  6. boolean variables python

    freemarker assign boolean variable

VIDEO

  1. 006 BOOLEAN

  2. VB NET

  3. SurvivalHorrorAction Middle Boss and Boss Boolean Variable

  4. Lab3: Boolean Algebra Continued

  5. RPA Automation Anywhere A360 Boolean Package and Boolean Variable

  6. Variable & assignments

COMMENTS

  1. assign

    =: 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> .

  2. Built-ins for booleans

    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}.

  3. freemarker

    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...

  4. Freemarker macro with boolean variable

    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

  5. FreeMarker: Conditional Statements

    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.

  6. FreeMarker Manual

    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.

  7. FreeMarker Common Operations

    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>

  8. FreeMarker Manual

    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}").

  9. Freemarker Variable Reference Guide

    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

  10. Null Values in FreeMarker

    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: