Using Netbeans PHP Code Templates

Netbeans is a great IDE and with every version lots more features are introduced that make it even better. One thing that I like to use is the code templates, which have been available from version 6.5. Code templates allows you to type a simple command and get a section of code. What commands you can use depend on what version of Netbeans you are using and which programming language you focused on. As a PHP developer I usually download the PHP version, which comes with a set of PHP code templates. To try one out go into a PHP file in Netbeans and enter if followed by a tab. Netbeans will automatically change this into the following:

if (condition) {
  ;
}

You might notice that Netbeans puts your cursor over the condition part of this statement, you can now type in the condition part of the statement without having to move the cursor into the right place. Hitting enter after you have typed something here will take you to the next line, just before the semi colon.

Note that you need to type if followed directly by the tab key, you can't enter any other key presses between these or it will not work. To look at the different code templates available go to Tools > Options, click on the Editor icon and then click on the Code Templates tab. You should see a dialog that looks like this:

Netbeans Code Templates

The code templates are split into different languages, the diagram above shows the PHP language selected and start of the PHP list of code templates. To see what a template creates just click on the line, it will show you in the white box below.

It is also possible to create your own templates that can be used just like any other template. I use the following block of code quite a bit:

echo '<pre>' . print_r($someArray, true) . '</pre>';

Click on the New button to the right hand side of the table and you will see a dialog box, this box is for you to type in your abbreviation. In this box I typed in the following:

print_r

This dialog then takes you back to the code templates screen and inserts a line with your abbreviation into the table and highlights it. In the text area in the lower half of this screen enter the following:

echo '<pre>' . print_r(${cursor}, true) . '</pre>';

Finally on this screen, if you want to change the keypress that completes your code template then you can do it at the bottom of this dialog. You can only change it to space, shift+space or enter. Once you are happy click ok to save the templates you entered and get back into the editor.

Now when you type print_r and hit the tab key (if you didn't change it to something else) Netbeans will print out the following:

echo '<pre>' . print_r(, true) . '</pre>';

And put the cursor in place of the ${cursor} string we defined earlier. The ${cursor} string is a special parameter used by Netbeans to place the cursor at that point. However, anything you enclose in ${} will be seen as a place marker and will allow you to enter a bit of code and hit enter to continue to the next place marker.

You can create code templates for single lines of code like the above, or for much more complicated sets of code like class testing templates.

Comments

I need to convert same variables to uppercase|lowercase|captalize.

/**
 * @package     ${1 default="Hello"}
 * @subpackage  ${com}_${1 capitalize=false}
 * @copyright   Copyright (C) 2012 ${AtomTech}, Inc. All rights reserved.
 * @license     GNU General Public License version 2 or later; see LICENSE.txt
 */

// No direct access
defined('_JEXEC') or die;

How can I do this?
 

Permalink

Add new comment

The content of this field is kept private and will not be shown publicly.
CAPTCHA
7 + 3 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.