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:

This entry was posted in New Features and tagged , , . Bookmark the permalink.

3 Responses to Enabling Implicit Context Variables Resolution in Template Files

  1. A. Adrian says:

    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:

    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:
    Server.MyApp.UDF.util.someFunction();
    Server.MyApp.UDF.xml.someFunction();
    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:
    Server.MyApp.UDF.xml
    …it should look for the function here:
    /ProjectRoot/udf/util.cfm

  3. Tom Davies says:

    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 :-(

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">