Code Templates in NetBeans PHP

The NetBeans editor contain many features that can increase your productivity. Code templates are one such feature. Code templates let you paste a snippet into the document at the caret position via an abbreviation or code completion.

The list of available code templates is in the Options dialog, category Editor and tab Code Templates. If you want to see the PHP code templates, you have to select PHP in the Language combo box.

As I wrote above, you can paste the snippet via the defined abbreviation or code completion. For example,  the picture above shows a template that generates a new class stub. It has the abbreviation cls assigned to it and the abbreviation is expanded by the Tab key. So when you press the keys C then L then S and then Tab, the cls string is replaced with the snippet. You have to press the exact sequence of the keys. If you press a different key like Backspace or you create a typo and correct it, the code template expansion will not work.

The second way to paste the snippet is through code completion. The code template appears in the code completion when you write the beginning of the snippet (not the abbreviation). So in our case you can write cla and invoke code completion (CTRL + SPACE) and the template is offered in the code completion window. The documentation window displays the template, the description of the template, and which key you have to use for expansion.

These snippets can be defined as "live" templates.  Basically it means that the snippet contains parameters that can be changed when the code is pasted to the editor.  I often use the "class" template, which is a live template as well, but has only one parameter ${class_name}. This parameter has a blue background after pasting and you can simply change the content. When the parameter is used more times in the snippet, then all occurrences of the parameter are renamed.

The template for the for cycle is a good example.

As you can see, the template has two parameters, number_variable and array_variable. After pasting the template into the editor, the number_variable parameter is selected and when you edit it, all occurrences are changed as well. So let's say that you want to rename it to index. Just write index and press ENTER. The next parameter - array_variable is selected.

When you are finished with array_variables, press ENTER and because there is no other parameter, the cursor is moved to the place which is marked with ${cursor} parameter in the template. After this the template is not "live" anymore and it's just a text in editor. If the template is "live" you can use the Tab key to move between parameters. The ${cursor} parameter is dedicated for marking the position where the editor places the cursor after you finish editing all parameters.

You can add your template in the Options dialog. Just press the New button, enter the abbreviation and then the code template.  If you want to write the $ character (likely in PHP templates :)) you have to escape it with $$.

The code templates are stored in your ${userdir}/config/Editors/text/${language}/CodeTemplates/org-netbeans-modules-editor-settings-CustomCodeTemplates.xml file. Parameter ${userdir} corresponds to the folder where NetBeans stores data. By default it's in your home folder, but if you start NetBeans with the --userdir switch, then it is the value of this switch. ${language} corresponds to the language for which you are editing the templates. In the case of PHP  it is x-php5.

If you use a template which is not in the PHP IDE by default, write the template in the comments. We can add these templates to the default list.



How about this to start with-


Posted by N, Varun on September 18, 2008 at 02:21 PM CEST #

[to readers:] please vote for the "Import / export of code templates" feature request here:


Posted by Giovanni on September 19, 2008 at 05:27 AM CEST #

It was a very nice idea! Just wanna say thank you for the information you have shared. Just continue writing this kind of post. I will be your loyal reader. Thanks again.

Posted by links of london on October 29, 2009 at 08:15 PM CET #

This feature of netbeans is cool but not perfect yet :(.

Please vote for a anhancement for this feature:

Posted by william on May 17, 2010 at 04:18 AM CEST #

Do i need to activate this feature?

I added a own template and tried it => nowt working

Also i tried the pre-defined and they are also not working
for example cls but nothings happening

Also i can't see it in the available list ( )

Posted by ragtek on June 15, 2010 at 10:43 AM CEST #

Well, it is indeed a very good feature, please also add the following code snippet to default snippet list.


echo "<pre>";
echo "</pre>";

Posted by Fahad Farrukh on July 08, 2010 at 05:39 AM CEST #

I'm trying to add template for PHP method:

\* ${methodDescription}.
\* @param ${type1} $${variable1} ${description1}
\* @param ${type2} $${variable2} ${description2}
\* @return ${returnType} ${returnDescription}
${public} function ${f}($${variable1}, $${variable2}) {

but when I navigate through the tempalate filling data, it skips ${variable1} and ${variable2}.

Any idea?

In eclipse I have similar template:

\* ${methodDescription}.
\* @param ${type1} $$${variable1} ${description1}
\* @param ${type2} $$${variable2} ${description2}
\* @return ${returnType} ${returnDescription}
${public} function ${f}($$${variable1}, $$${variable2}) {

and what I type in variable1 reflects all occurences of variable1. Is there something similar in NetBeans?

Posted by umpirsky on April 20, 2011 at 03:18 AM CEST #


Basically you find a bug. I have entered it in our bugzilla. . I will try to fix it.

Posted by Petr Pisl on April 20, 2011 at 08:03 AM CEST #

@Petr Pisl Thanks!

Posted by umpirsky on April 20, 2011 at 08:12 AM CEST #

How to code template variables transform uppercase|lowercase?

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?

Posted by guest on June 28, 2012 at 07:02 PM CEST #

Post a Comment:
  • HTML Syntax: NOT allowed

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


« July 2016