Assigning a setting variable into a local variable in ftl

1.6k Views Asked by At

I am new to Ftl and am working with dates in ftl. I know that I can get the current year using

    ${.now?string("yyyy")}

However I want to assign the year into a variable. On using

<#assign year = ${.now?string("yyyy")}>

I get a syntax error. Can someone help?

2

There are 2 best solutions below

3
On

I get this syntax error, and unless you are using an old version, you should too:

Syntax error in template "adhoc.ftl" in line 1, column 17: You can't use "${" here as you are already in FreeMarker-expression-mode. Thus, instead of ${myExpression}, just write myExpression. (${...} is only needed where otherwise static text is expected, i.e, outside FreeMarker tags and ${...}-s.)

0
On

The correct assignment is

<#assign year = .now?string("yyyy") />