#413 Directive of #Include cannot render file paths that have variable

directives (1)

I wonder how can I include something like
<#include "mydirectory/{{aVariable}}.ftl"
aVariable is a known value and when I assign it in previous lines like this:
<#assign x= "{{aVariable}}">
it displays correctly. However in the #include, {{aVariable}} again renders as {{aVariable}}, not the real value. I tried to pass ${x}, but still, x renders back to {{aVariable}}.

Is there anyway to get around this? Thanks.


  • {{aVariable}} is not FTL syntax. If it's still substituted then it's not done by FreeMarker but something else, and that something certainly replaces it in the output of the template, and thus #include is not affected.

    The usual way of inserting values into the output is like ${aVariable}, which also works with #include like <#include "mydirectory/${aVariable}"> or <#include "mydirectory/" + aVariable>.

    Last edit: Dániel Dékány 2014-04-05
    • status: open --> pending-invalid
    • Group: -->
  • organ

    Thanks for your response.
    In my case, it is freemarker and handlebar together and if I assign a variable such as
    <assign x="{{aVariable}}"> it is able to print x correctly, only if I put x into Includes, it renders back to {{aVariable}}, i.e. <#include "mydirectory/" + aVariable>. or <#include "mydirectory/" + ${aVariable}> I tried both but they didnt work.

  • I suppose FTL is used to generate a Handlebar template. That is, FreeMarker runs before Handlerbar, and it's Handlerbar that replaces the {{...}}-s in the output of the FTL, not FreeMarker.

  • organ

    Thanks for your explanation. I guess in this case, I will have to set the value in handlebar and pass it to FTL.

  • I don't know the framework that you are using, but it's highly unlikely that that's possible. Most certainly FreeMarker runs strictly before Handlebar, as FreeMarker runs on the server and Handlebar runs on the client. Probably you should use a FreeMarker data-model variable for the #include, and should use Handlebar only for variables that are actually shown for the user.

    • status: pending-invalid --> closed-invalid
  • organ

    Thanks Daniel, what you suggested is the right direction. May I ask a question not quite relevant to the original: my java class that feeds the json data passes string with html tags such as test (with bold tags)I wonder what should I do on the freemarker side to render 'test' as black? I tried escape/no escape on freemarker and they didn't work. The string comes from java always renders literally instead of rendering as html tags.
    I tried StringEscapeUtils.escapeHtml in java but ftl will display with 'less than' 'greater than' symbols.

    Any suggestions?

    Last edit: organ 2014-04-07
  • Amit Kumar
    Amit Kumar

    Hi Organ,
    were you able to resolve this issue as i am facing same issue.