X

An Oracle blog about NetBeans for PHP

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.

Join the discussion

Comments ( 10 )
  • N, Varun Thursday, September 18, 2008
  • Giovanni Friday, September 19, 2008

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

    http://www.netbeans.org/issues/show_bug.cgi?id=99494

    ;)


  • links of london Thursday, October 29, 2009

    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.


  • william Monday, May 17, 2010

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

    Please vote for a anhancement for this feature:

    http://netbeans.org/bugzilla/show_bug.cgi?id=184604


  • ragtek Tuesday, June 15, 2010

    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 ( http://blogs.sun.com/netbeansphp/resource/article_images/codeTemplates3.png )


  • Fahad Farrukh Thursday, July 8, 2010

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

    ABBREVIATION

    pre

    SNIPPET

    echo "<pre>";

    print_r(_array);

    echo "</pre>";


  • umpirsky Wednesday, April 20, 2011

    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}) {

    ${selection}${cursor};

    }

    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}) {

    ${cursor};

    }

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


  • Petr Pisl Wednesday, April 20, 2011

    @umpirsky

    Basically you find a bug. I have entered it in our bugzilla. http://netbeans.org/bugzilla/show_bug.cgi?id=197906 . I will try to fix it.


  • umpirsky Wednesday, April 20, 2011

    @Petr Pisl Thanks!


  • guest Thursday, June 28, 2012

    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?


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