Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Which Code Templates Should NetBeans IDE PHP Provide?

DZone's Guide to

Which Code Templates Should NetBeans IDE PHP Provide?

· Web Dev Zone
Free Resource

Discover how to focus on operators for Reactive Programming and how they are essential to react to data in your application.  Brought to you in partnership with Wakanda

The NetBeans editor contains many features that can increase your productivity. Included in these are the code templates that let you paste snippets into a document at the caret position via an abbreviation or via code completion.

You can find the list of available code templates in the Options dialog, in the category Editor and then in the tab Code Templates. If you want to know the PHP code templates, simply select PHP in the Language combo box:

You can paste the snippet via the defined abbreviation or code completion. For example, in the picture above, template is selected that generates a new class stub. The assigned abbreviation is cls, which is expanded when the Tab key is pressed. So, when you press keys C then L then S and then Tab, the cls string is replaced with the snippet. You have to press the mentioned sequence of the keys in that order. When you press a different key like Backspace or you create a typo and correct it, then it will not work.

The second way how to paste the snippet is through the code completion. The code template will appear in the code completion when you write a beggining of the snippet (no the abbreviation). So in our case you can write cla and invoke code completion (CTRL + SPACE) and there is the template offered in the code completion window. The documentation window displays the template, the description of the template and also which key you have to use for expansion.

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

The template for for cycle can be 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 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 not other parameter, the cursor is moved on the placed 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 Tab key to move between parameters. The ${cursor} parameter is a dedicated for marking position, where editor place the cursor after you finished editing all parameters.

You can add your template in the Options dialog. Just press New button, enter the abbreviation and then the code template.  If you want to write $ char (in PHP templates probably yes :)) 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 --userdir switch, then it is the value of this switch. ${language} corresponds to the appropriate language, for which you edit the templates. In PHP case it is x-php5.

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

Learn how divergent branches can appear in your repository and how to better understand why they are called “branches".  Brought to you in partnership with Wakanda

Topics:

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}