Tuesday October 02, 2007
FreeMarker: Baked into NetBeans IDE 6.0 (Part 3)
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!
Oct 02 2007, 01:34:22 PM PDT Permalink


