• 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
  • Labs The future of collective knowledge sharing
  • 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 assign value to variable

I'm having trouble while trying to assign value to a variable using Freemarker.

This is the error message: Encountered ".", but was expecting one of: STRING_LITERAL RAW_STRING ID Can anyone help me with this? Thank you.

user3034683's user avatar

  • Off topic, but you don't need #if + #list for such situations, just use a range with exclusive end, like <#list 0..!num as i> . (Requires FreeMarker 2.3.21.) –  ddekany Jan 15, 2015 at 22:38

You can only write top-level variables in a FreeMarker template. Also you can't assign to a variable with dynamically constructed name, except with an ?interpret hack: <@"<#assign abc${i?c} = abc${i?c}?reaplce('test', "Test")>"?interpret /> . Obviously that's horrid... BTW, what's the use case here? Why do you need to assign to dynamically constructed variable names?

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 and acknowledge that you have read and understand our privacy policy and code of conduct .

Not the answer you're looking for? Browse other questions tagged freemarker or ask your own question .

  • The Overflow Blog
  • Fighting comment spam at Facebook scale (Ep. 602)
  • What it’s like being a professional workplace bestie (Ep. 603)
  • Featured on Meta
  • Moderation strike: Results of negotiations
  • Our Design Vision for Stack Overflow and the Stack Exchange network
  • Temporary policy: Generative AI (e.g., ChatGPT) is banned
  • Discussions experiment launching on NLP Collective
  • Call for volunteer reviewers for an updated search experience: OverflowAI Search

Hot Network Questions

  • may(=possibility) vs. can(=possibility)
  • People who can't receive O negative blood?
  • TV-series with wizards and swords, with fat companion
  • Congruences for power-sum of divisors
  • Is quadrature still considered part of numerical analysis?
  • So, too, may be the fate of his seed
  • Are high-starch potatoes hard (low-starch soft)?
  • Is it safe to create a public ID by hashing a private key?
  • Is there a socially-optimal way to drive on a busy interstate?
  • Would it make sense to use the "plus" and "minus" components of \baselineskip?
  • Can believers of Jehovah work for the navy?
  • Outlining the boundary of a shape defined by intersections in TikZ
  • What adhesive solution should use for walls with powdery surface?
  • Would it be possible to make a custom zoomable world map (kinda like google maps)?
  • How to type this character from 汉字大字典 in my computer?
  • Is naturalism the null hypothesis?
  • Can a public domain character be subject to trademarks?
  • Two ideal ohmmeters in parallel
  • move the instance collection to the point of contact with the plane?
  • A fantasy about a man selling his shadow to the Devil
  • Which airline is liable for compensation in case of missed connection?
  • Compile TailwindCSS to CSS
  • What is this transparent round "plate" brick with wiring inside?
  • Enthalpy change of a solution of of 1-pentanol in 1-butanol

freemarker assign to list

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 .

FreeMarker

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

Loop variable built-ins

  • Alpha. index
  • Expressions
  • #directives

is_even_item

Is_odd_item, item_parity, item_parity_cap.

Loop variable built-ins only exists since FreeMarker 2.3.23.

These built-ins you can only use with the loop variable of the list and items directives (and of the deprecated foreach directive). Some explanation of that follows ( loopVar ?index returns the 0-based index in the listable value we iterate through):

When the list directive doesn't specify the loop variable, these built-ins are used with the loop variable of the items directive:

Loop variable built-ins only use the name of loop variable, so that they can identify the related ongoing iteration. They don't read the value of the loop variable. Hence, this is a parsing error:

This built-in is available since FreeMarker 2.3.23.

Returns the 1-based index where the iteration (which is identified by the loop variable name) currently stands.

For the 0-based index, use the index built-in .

Tells if the item where the iteration (which is identified by the loop variable name) currently stands is not the last item.

For separating items with commas and such, use <#sep> separator </#sep> instead of <#if var ?has_next> separator </#if> , as it's more readable. (Furthermore the </#sep> can be often omitted, like in <#list ... as var > ... ${ var } ... <#sep> separator </#list> )

If you need the inverse of this built-in, use var ?is_last instead of ! var ?has_next , because it's more readable.

Returns the 0-based index where the iteration (which is identified by the loop variable name) currently stands.

For the 1-based index, use the counter built-in .

Tells if the item where the iteration (which is identified by the loop variable name) currently stands has an even 1-based index.

To make tables with alternating row colors and such, use var ?item_parity or var ?item_cycle(...) instead.

Tells if the item where the iteration (which is identified by the loop variable name) currently stands is the first item.

Tells if the item where the iteration (which is identified by the loop variable name) currently stands is the last item.

If you need the inverse of this built-in, use var ?has_next instead of ! var ?is_last , because it's more readable.

Tells if the item where the iteration (which is identified by the loop variable name) currently stands has an odd 1-based index.

This is a more generic version of the item_parity built-in , where you can specify what value to use instead of "odd" and "even" . It also allows more than 2 values that it will cycle through.

Some details:

The number of arguments must be at least 1, and has no upper limit.

The type of the arguments can be anything, they doesn't have to be strings.

Use the item_parity built-in instead if the values you need are "odd" and "even" .

Returns "odd" or "even" string value, depending on the parity of the 1-based index where the iteration (which is identified by the loop variable name) currently stands. This is commonly used for alternating color for table rows:

Use the item_parity_cap built-in for capitalized "Odd" and "Even" . Use the item_cycle built-in to specify custom values, or more then two values.

Returns "Odd" or "Even" string value (note the capitalization), depending on the parity of the 1-based index where the iteration (which is identified by the loop variable name) currently stands.

Use the item_parity built-in for lower case "odd" and "even" .

  • 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-05-11 21:11:51 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.

IMAGES

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

    freemarker assign to list

  2. [Solved] Freemarker assign list length to local variable

    freemarker assign to list

  3. Introduction and use of freemarker

    freemarker assign to list

  4. Spring Boot FreeMarker Tutorial with Example

    freemarker assign to list

  5. Spring Boot + FreeMarker Example

    freemarker assign to list

  6. freemarker 表达式为空报错 遍历list 判空_freemarker list判空_wangjun5159的博客-CSDN博客

    freemarker assign to list

VIDEO

  1. SYNCHRO Control: Basic Controls

  2. MA2 Appearance and label Macros

  3. How To Assign Unique Permissions To SharePoint List Items

  4. Assign Preprint Server Manager and Moderator to Process the Preprint and Assign Publication Rights

  5. Assign serial numbers after filter in Excel

  6. Assign Expenses to a Customer or Project 7310 Xero 2022 -2023

COMMENTS

  1. assign

    Note that because the FreeMarker template language assumes that sequences (lists, arrays, etc.) and hashes (maps, beans, etc.) are immutable, you can not write something like <#assign myObj.someProperty = 'will NOT work'> or <#assign myList[0] = 'will NOT work'>.

  2. hashmap

    Modified 10 years, 2 months ago. Viewed 2k times. 0. i have a freemarker problem. I have one hash map called nodes and i iterate trough it like this: <#list hashmap.collection as nodes> .....some displaying <#assign nodeName> $ {nodes.name} </#assign> <#list hashmap2.nodeName.collection as nodes2> .......some more displaying.

  3. Freemarker assign value to variable

    Freemarker assign value to variable. I'm having trouble while trying to assign value to a variable using Freemarker. <#if size??> <#assign num=size?number> <#if (num>0)> <#list 0..num-1 as i> <#if .vars ['abc'+i?c] = "test"> <#assign .vars ['abc'+i?c] = .vars ['abc'+i?c]?replace ("test","Test")> </#if> </#list> </#if>.

  4. Loop variable built-ins

    0 1 2 When the list directive doesn't specify the loop variable, these built-ins are used with the loop variable of the items directive: Template <#list ['a', 'b', 'c']> <ul> <#items as x> <li>$ {x?index}</li> </#items> </ul> </#list>