iank
iank

Reputation: 810

Using the <#ftl> directive in a freemarker template

I'm trying to have Freemarker parse my template files as UTF-8, but it seems to be always parsing them as Cp1252.

2013-07-30 00:00:00.984 DEBUG freemarker.cache:? Could not find template in cache, creating new one; id=["myFile.ftl"["es_MX",Cp1252,parsed] ]

I searched and found that adding the FTL directive allows me to specify the encoding.

I added it to the template file (saved in UTF-8 with BOM in Notepad++) like so:

<#ftl encoding="UTF-8">
Estimado Usuario,
Testing this content!
<#/ftl>

But get this in the log.

MessageTemplateException [message=Unable to process template due to Encountered "<#ftl " at line 1, column 4 in myFile.ftl.
Was expecting one of:
    <EOF> 
    <ATTEMPT> ...
    <IF> ...
    <LIST> ...
    <FOREACH> ...
    <SWITCH> ...
    <ASSIGN> ...
    <GLOBALASSIGN> ...
    <LOCALASSIGN> ...
    <_INCLUDE> ...
    <IMPORT> ...
    <FUNCTION> ...
    <MACRO> ...
    <TRANSFORM> ...
    <VISIT> ...
    <STOP> ...
    <RETURN> ...
    <CALL> ...
    <SETTING> ...
    <COMPRESS> ...
    <COMMENT> ...
    <TERSE_COMMENT> ...
    <NOPARSE> ...
    <BREAK> ...
    <SIMPLE_RETURN> ...
    <HALT> ...
    <FLUSH> ...
    <TRIM> ...
    <LTRIM> ...
    <RTRIM> ...
    <NOTRIM> ...
    <SIMPLE_NESTED> ...
    <NESTED> ...
    <SIMPLE_RECURSE> ...
    <RECURSE> ...
    <FALLBACK> ...
    <ESCAPE> ...
    <NOESCAPE> ...
    <UNIFIED_CALL> ...
    <WHITESPACE> ...
    <PRINTABLE_CHARS> ...
    <FALSE_ALERT> ...
    "${" ...
    "#{" ...
 with inputs of {loginUrl=testURL} in myFile.ftl with locale es_MX, code=SUCCESS, arguments=[]]

Maybe I'm not doing this right?

Upvotes: 2

Views: 4403

Answers (2)

klemonnier
klemonnier

Reputation: 1

I had the same problem today and finally found a way to use the <#ftl> directive, so in case the same thing happens to anyone :

My problem was that I didn't put the <#ftl> directive on top of my template. As soon as I put it on the first line of my template, everything worked like a charm.

Upvotes: 0

ddekany
ddekany

Reputation: 31152

Don't use BOM. Also don't use </#ftl>, there's only <#ftl>. But most importantly, if most of your templates are in UTF-8, just set the default encoding of FreeMarker to UTF-8 (in Spring, something like <bean class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">...<property name="freemarkerSettings">...<props>...<prop key="default_encoding">UTF-8</prop>), so your don't need to use #ftl because of this.

Upvotes: 2

Related Questions