How to manage templates in NetBeans II

My last entry "How to manage templates in NetBeans I" introduces to the file templates in NetBeans. This is probably more interesting part, which describes how to change the content of a new file during creating from a template. 

When you press the Finish button in the new file wizard, then usually  the selected template is process with an assigned template engine and the resulted file can contain some information like time of creation, user name and etc, which are added by the template engine. As I mentioned, one of the template attributes is a template engine. This is not mandatory but in the most cases it is FreeMarker.

Due to issue #140256 only templates, which where created via Duplicate button in the Template Manger, have assigned the template engine as has the original template. The next text is applicable only for templates created via Duplicate button.

As I wrote above, processing of template with a template engine can alter the content of new created file. I will explain it on an example. Expect that we want to have a template to generate file with PHP class, which include our predefined license, who and when the file is created and also in which encoding the file is created. The result file Example.php should look like at the picture.

What the template needs to alter:

  • Line #2: name of the charset
  • Line #4 - #7: include my license
  • Line #10, #15, #18: name of the class, which is the same as name of the file
  • Line #12: name with file extension and when it was created
  • Line #13: who the file created

There are few predefined variables for  FreeMarker in NetBeans, which will be used.

Variable Name Description
name contains the name of the file that is being created
user contains the user name
nameAndExt contains the name and extension of the file that is being created
date contains text representing the current day like 23. 3. 2007
time contains text the current time like 17:18:30
encoding the file encoding of the template instance


Because we want to reuse the license between more templates in the future, we need to create a new license template file. Open a project in NetBeans or create new one.  Go to the New File Wizard and in Other category select Empty File. As the file  name enter mylicense.txt. The license content you can download here and it can look like this:

${licenseFirst}, ${licensePrefix} and ${licenseLast} are variables, which will be defined in the template, where the license is used. The definition is done via <#assign > directive. We use the declarative way, because then the same license file can be reused in html, css and other files. Usually a license is placed to source file as a comment and the meaning of the variables is:

  • licenseFirst - text which is used as a delimiter for starting comment.
  • licensePrefix - text which is used as a line comment.
  • licenseLast - text which is used as a delimiter for ending comment. 

If a variable is unnecessary for a language then it can be defined as empty.

Now it's time to add mylicense.txt file to the Template Manager. The fastest way is through context menu over the file, where you can  select Save as Template... action. There is already License folder in dialog, which is displayed, so why don't put it there. Select the License folder and press OK button. After this you can delete the license file from your project.

Open the Template Manager. As you can see the License folder contains new myLincense.txt template.  Expand PHP folder and select PHP Class template. Press Duplicate button. New PHPClass_1 is created. It's necessary to use Duplicate function, because the Template Manager copies all attributes of original template including assigned scripting engine. Rename the new template to MyPHPClass.

Open the new template in editor via Open in Editor button. Replace the content with this or change the content to looks like:

The template is opened in php editor and this is why it contains error messages. It's not valid php code, but it's ok for now.

The directie <#include "../Licenses/mylicense.txt"> includes the license file, which we created before. The <#assing > directives defines variables for the license file.

I think we are done. Just test the new license. Go to New File wizard, select php category and choose MyPHPClass template. Create new file from this template and I hope that the new file contains everything as you expect.


Comments:

So, what about changing the function template ?

I like this:

/\*\*
\*
\* @return unknown_type
\*/
function test()
{

}

to become this :

/\*\*
\* Apr 19, 2010 - Shiro
\* Description
\*
\* @return unknown_type
\*/
function test()
{

}

Posted by Theo van der Sluijs on October 22, 2010 at 09:07 AM CEST #

Is there a way to include the proper namespace in the New Class template?

Posted by Adam Monsen on February 27, 2012 at 10:42 PM CET #

Post a Comment:
  • HTML Syntax: NOT allowed
About

This blogs is written by NetBeans developers who contribute to the PHP support mainly.

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today