Getting Intellij to pick up Freemarker macros

I'm trying to get intellij to properly pick up freemarker macros defined in includes.

If I create a file base.ftl that has:

[#macro test name]
Hello ${name}
[/#macro]

And another file index.ftl:
[#include "/base.ftl" /]

[@test name="John" /]


Intellij doesn't complain about finding base.ftl, but it complains it can't resolve the macro test.

Any way to fix this?

Thanks.

12 comments
Comment actions Permalink

Do you use absolute or relative paths in the [#include] directive?   With absolute paths, you should set the [#-- @ftlroot "template root path" --] directive.

If this doesn't solve your problem, please provide more details about the project paths configuration.

0
Comment actions Permalink

Please also try File | Invalidate caches

0
Comment actions Permalink

I have same proble, it displays red for

[#include "/general/commons.ftl" parse=true ]


irina wrote:

you should set the [#-- @ftlroot "template root path" --] directive.


I dont understand,why is this needed?
is this a limitation of an IntelliJ, ?
I have not seen anywhere, that it required modification of a source file to satisfy the needs of IDE, dont you think that this is a little overboard?

0
Comment actions Permalink

This is needed because template root paths depend on your runtime FreeMarker configuration, and the IDE can't possibly guess that.
It can be done in your source file if it's convenient, but it's not necessary. You can do it in a separate FTL file under your project content, provided that the file starts with a special "implicitly included" marker:

[#ftl]
[#-- @implicitly included --]
...
[#-- @ftlroot "template root path" --]
...


(or its angle-bracket variation). This file can be excluded from version control so as not to pollute your shared codebase with IDE-specific stuff.
0
Comment actions Permalink

but  usualy this kind of settings are found under project properties/settings somewhere right? like deployment descriptor is, and many others ...
but that probably requres more effort right?
i mean IDE settings does not really belong in source code :)
I am building my project with ant and feel really guilty having that file in root of src, just to make IDEA happy about  my paths.

0
Comment actions Permalink

There are no UI settings for FreeMarker. This ftl-root setting was placed to the source code because it was quite close to ftlvariable comment syntax which allows the IDE to understand your FreeMarker model, and since it's different for every source file, it's hard to configure it in UI.

There's nothing to be guilty about, just don't include this file into Ant build.

0
Comment actions Permalink

Peter,

1. Where would be the best place to add a feature request to further document this feature?  I found this doc ( https://blog.jetbrains.com/idea/2009/08/enabling-template-files-and-velocimacros-resolution/ ), but it's Velocity-specific despite the title.

2. @ftlroot wants a relative root, but it's not clear relative to me where it starts looking, relative to what?

3. I want to make use of @ftlvariable, but I find any docs.

4. How do I define a run-time root for Freemarker? For example, let's say I have a repo that contains most of my app's Freemarker templates.  However, those templates rely on some another file that is machine-generated at build time.  I'd like to tell IntelliJ about my build template folder, so it can interpret that.

0
Comment actions Permalink

1. The best place for feature requests is issue tracker: https://youtrack.jetbrains.com/issues#newissue

2. ftlroot comments are relative to the directory of the file where this comment is placed.

3. The easiest way to declare an @ftlvariable is to find a FreeMarker reference that can't be resolved, press Alt+Enter there and choose a quick fix to define the variable in comment.

4. Sorry, I don't understand what's "build template folder". You can try to put a file similar to your auto-generated file to one of module's content/source/resource roots, or ensure that the directory with the auto-generated file is marked as one of those in Project Structure.

0
Comment actions Permalink

Could you point to documentation for the @ftlroot variable?  I found documentation for <#ftl> here http://freemarker.org/docs/ref_directive_ftl.html but not ftlroot.

0
Comment actions Permalink

Unfortunately there seems to be no documentation for ftlroot. I've filed https://youtrack.jetbrains.com/issue/IDEA-176216.

ftlroot is an IDE-specific comment that describes where included files are located, so that the IDE can find them and use definitions from them.

0
Comment actions Permalink

I have a directory which is a template root in the current module/jar project, as well as a template root directory in a separate module/jar project.  This works fine on the web server for macro resolution.  However, IntelliJ can't find the macro files in the other project, even if the ftlroot is set and is the same directory in that other module.  Is there a workaround for this?  In both projects, macros are in the /src/main/resources/macros folder.  Both are Maven projects. 

0
Comment actions Permalink

Could you please file a request to https://youtrack.jetbrains.com/issues#newissue, and provide more details there: e.g. some screenshots showing the layout of the projects/files, and the ftlroot errors?

0

Please sign in to leave a comment.