X

Geertjan's Blog

  • October 2, 2007

FreeMarker: Baked into NetBeans IDE 6.0 (Part 3)

Geertjan Wielenga
Product Manager
Talking about templates again... in recent blog entries I've written about how the NetBeans module author can send values from an instantiate() method to fill a FreeMarker template's variables. The new method DataObject.createFromTemplate(targetFolder, targetName, mapWithParameters) makes this possible. So, by using that method, you will not be limited to the default variables that are available to FreeMarker templates in NetBeans IDE. These default variables are the following:
  • date
  • encoding
  • name
  • nameAndExt
  • time
  • user

The above can be used in FreeMarker templates as is, i.e., without your needing to define a map with these values. So you could put ${nameAndExt} in your FreeMarker template and then, when the file is ultimately generated, i.e., from that template, the name and extension of your file will appear in your file.

Let's now turn our gaze away from the NetBeans module author. Let's now wear the hat of the NetBeans user. The NetBeans user has the Template Manager under the Tools menu. Some of the templates in there are defined in FreeMarker. For example, this is the FreeMarker template for the Java interface:

<#assign licenseFirst = "/\*">
<#assign licensePrefix = " \* ">
<#assign licenseLast = " \*/">
<#include "../Licenses/license-${project.license}.txt">
<#if package?? && package != "">
package ${package};
</#if>
/\*\*
\*
\* @author ${user}
\*/
public interface ${name} {
}

So, the NetBeans user is able to use any of the items in the list above as variables in their templates. They can open the template in the editor and then they can change the template and save it. Then the rewritten template will be used next time they use the wizard that makes use of that template. So, just like the NetBeans module author, the list of items above is available to the NetBeans user.

"So," you might think. "The NetBeans module author can do a lot more. After all, the NetBeans module author can define new variables, such as what was done in FreeMarker: Baked into NetBeans IDE 6.0 (Part 2). In other words, while the NetBeans module author can create new variables in the FreeMarker template, the user is unable to do so, being restricted to those that are in the list above."

And the answer to the above is: "Not so!" It took a while to get to this point of the story, but I thought it might be necessary. The point is that the user, since the last few days, thanks to Jarda Tulach, can go to this place in the Template Manager:

When you open that file, you will see this, exactly this:

#
# Here, or in other properties files in this directory, you can define
# various properties that you want to make available to the template
# processor while creating various templates.
#
# uncomment the next line and specify your user name to be used in new templates
#user=Your Name <your.name at your.org>

Now... the NetBeans user can add properties here (or override existing ones, such as "user"). So, for example, let's add this line:

greeting=hello world

And now, in any of the FreeMarker templates in the Template Manager, I (as the NetBeans user) can put ${greeting}. Then, when I choose the wizard that makes use of that FreeMarker template, all instances of ${greeting} will be replaced with "hello world". In this way, the user has complete control over the FreeMarker templates in the Template Manager. I think this is pretty cool.

In other news. Check out yet another Schliemann implementation, here in Henry Story's blog. He helpfully even includes the code. Thanks a lot!

Join the discussion

Comments ( 16 )
  • guest Tuesday, October 2, 2007

    ggf


  • Roridge Thursday, November 22, 2007

    I have just seen this post, and thank god for you Geertjan!

    My question is, where does this come into play?

    <#include "../Licenses/license-${project.license}.txt">

    I can't find it anywhere?

    thanks,

    Ror


  • Geertjan Thursday, November 22, 2007

    Hi Roridge, I keep meaning to investigate that specific question, but haven't got round to it yet. I hope to blog about it soon. (Feel free to ping me if I keep on forgetting.) I guess it has something to do with licenses, than one can set a license somewhere and then use the license across all templates. That's basically my understanding but, again, I will try to track down the answer soon. Thanks for the question and the encouragement.


  • Roridge Friday, November 23, 2007

    Thanks G

    I have tried to put files in places to get them to be put in the top as headers, but so far all I get is errors.

    Keep up the good work.

    Ror


  • Roridge Friday, November 23, 2007

    As a further update, this works great, although I can't work out how to auto generate the Year (rather than just the date).

    Also the User Preferences do not work in the UML code generation template, is this a bug or a feature request?

    Ror


  • Geertjan Friday, November 23, 2007

    If that doesn't work, I would say it is a bug and I would file an issue in Issuezilla, describing the steps to take. Other questions you ask I don't know how to answer right now, but will do in the coming days.


  • Geertjan Monday, November 26, 2007

    Hi Roridge, for info on the license statement in the FreeMarker templates, see this blog entry:

    http://blogs.sun.com/geertjan/date/20071126


  • Malcolm Tuesday, August 5, 2008

    Is there a property for the project name? The recommended GPL entry for a multi-file program includes the program name, but I have not been able to find documentation on how to get that property.


  • Okafor Ebube Tuesday, March 10, 2009

    Really helpful info. Thanks a lot for this.


  • Det Monday, June 28, 2010

    What a pity that the question of Malcolm (August 5, 2008) never got answered ...


  • Geertjan Wielenga Monday, June 28, 2010

    ${project.name}


  • Det Tuesday, June 29, 2010

    Thanks, Geertjan, but ...

    @author: Det - Copyright(C) 2010

    \*

    \* This file is part of Expression project.name is undefined on line 7, column 41 in Templates/Licenses/license-det-lgpl30.txt..

    \*

    \* Expression project.name is undefined on line 9, column 20 in Templates/Licenses/license-det-lgpl30.txt. is free software: you can redistribute it and/or modify

    \* it under the terms of the GNU Lesser General Public License as

    \* published by the Free Software Foundation, either version 3 of

    \* the License, or (at your option) any later version.

    \*

    What is wrong with my NB ?

    KR

    Det


  • Geertjan Tuesday, June 29, 2010

    Impossible for me to know without seeing any of your code. But, by the way, this is documented, and maybe try project.displayName instead:

    http://wiki.netbeans.org/FaqTemplateVariables

    Otherwise, create an issue in Issuezilla. And if you want an issue to be fixed, the best thing to do is to include steps to reproduce your problem and/or include a sample application with steps how to use it to reproduce your problem. Thanks.


  • Det Tuesday, June 29, 2010

    Thanks Geertjan for the quick response.

    I created a new plain Java Application project, and you're right: project.name and project.displayName both work as expected.

    Thanks for the Faq-Link.

    So it is an issue with my current project, which is indeed a Scala project.

    Will send the issue to the plugin team.


  • Marty Kopka Wednesday, June 30, 2010

    Hi,

    was just wondering if you are the right person to ask the following and if not do you know who is:

    - When/If ever, will coloured syntax support be introduced for the template user be included.

    You say:

    There's quite a bit more possible with FreeMarker, and obviouly we need syntax coloring and so on, that I'm still investigating, such as something to do with the product license that I need to figure out. But I can already see the benefits of FreeMarker support in NetBeans IDE 6.0. And it's all really pretty cool and clever.

    Sep 30 2007, 02:22:50 AM PDT Permalink

    @http://blogs.sun.com/geertjan/entry/freemarker_baked_into_netbeans_ide1

    So I figure you've looked at it at least ... any ideas on when itll be in NB?

    cheers,

    Marty


  • Mpampirina Sunday, February 6, 2011

    The "user=John Doe" in User.properties works in java templates, using the ${user} parameter.

    It also works in C++ templates, using the %<%USER%>% parameter .

    But when I define a new parameter, lets say:

    coursename=Programming 101

    while it works with ${coursename} in java templates, I would expect that %<%COURSENAME%>% would work in C++ templates, but it doesn't.

    Could you please give me a hand (or an idea) to make it work? Thanks in advance


Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.