Enabling Implicit Context Variables Resolution in Template Files


Because almost any template file has external references to context variables (and we’ll eventually find their declarations in code rendering that file), we need to introduce declarations for such implicit variables. IntelliJ IDEA helps us to do this in familiar comment-based way: @vtlvariable comment declares name, type and optional scope file for implicit variables; variable type is inferred from method and #foreach usages. Let’s have a look at the latter variant.

Press Alt+Enter on an unresolved collection reference and select a current file declaration:

Appropriate types of Velocity iterables are shown:

It might be undesirable to spread IDE-specific @vtlvariable comments across an entire project, especially in a mixed-IDE team. So, IntelliJ IDEA allows to place all these comments in a single file and use external variable declaration:

An implicitly included file is used for storing the declaration comment and file attribute that determines the scope file for declared variable:

Comments below can no longer be edited.

4 Responses to Enabling Implicit Context Variables Resolution in Template Files

  1. A. Adrian says:

    August 8, 2009

    While variable resolution is a must for usable Velocity support, polluting the template files with such comments is not the best idea IMHO.
    Different frameworks(that use velocity) will use different methods to fill the context with variables (and for many of them the variable “tree” can be huge).
    I think a much better and general approach would be to let the user set in the Velocity Facet a sort of “template variable provider” class.
    For this “template variable provider”, one approach could be similar to a “JDBC driver” (and would not depend on IntelliJ), that each framework (that needs Velocity) could implement (in a much simpler way than making an IntelliJ plug-in right now).

  2. Dave Merrill says:

    July 28, 2010

    Is there any way to do something like this outside of Velocity? I work on a large ColdFusion app with a lot of static functions loaded into server space, and called like this:
    There are thousands of references like this in our code that a) are all flagged as unresolved, and b) don’t provide argument hints.
    The path segments at the ‘util’ and ‘xml’ level above correspond to physical cfm or cfc files, and I’d really like some way to tell IDEA on a project- or module-wide basis that to resolve this:
    …it should look for the function here:

  3. Tom Davies says:

    November 24, 2011

    Any way of doing this for macro parameters, i.e.

    #* @vtlvariable name=”page” type=”xxx” *#
    #macro (foo $page)
    $page.attribute <— get’s a grey squiggly 🙁

    • Alasdair Macrae says:

      June 12, 2015


      Did you ever find out if this was possible with macro params?


Subscribe for updates