Speed up PHP development in Eclipse PDT with Templates and Snippets

Busy schedules, long to do lists, and deadlines make it tough for developers to get familiar with some of the advanced features their tools provide. This is a shame, because some features, like Eclipse Templates, can really reduce coding time and errors.

You already know that Eclipse PDT provides autocompletion and hints for PHP's built in functions. In addition, commenting your code with PHPDoc adds autocompletion and hinting for your application's classes, methods, and variables. If you haven't already discovered, Eclipse PDT also provides autocompletion of PHP's control structures, class definitions, and methods through code templates.

Eclipse PDT Templates

The PHP Developer Tools plug in provides several templates for all of PHP's control structures (if/else, for, foreach, while, switch, etc.) and more. Fire up Eclipse and take a look at Preferences > PHP > Templates. Here are all of the predefined templates available.

The name column indicates what you need to type to insert a template. Context, description, and the template code are all visible here. Some of the names are straightforward but others may take time to memorize. Printing the template list and keeping it nearby might be helpful.

To insert a template, type the name followed by Control + Space bar, select the template, and hit enter/return. Below, I typed 'swi' and then Control + Space bar to add a switch statement.

You must be working in a file that's part of a PHP Project to be able to use these PDT templates!

Once inserted, templates may contain variable fields. The switch template contains two variables (shown with box outlines below), one for the switch statement's variable/expression and the second for the case comparison. Just tab between the fields.

Think of all of the time you'll save and the typos you'll avoid with these templates. If the value or format of any of the templates don't fit with you or your team's coding standards, add new or edit existing templates, and share them with the team by exporting and importing them. Just to be safe, be sure to keep a copy of your exported template XML file as future PDT upgrades may overwrite customizations.

Eclipse Snippets

Most IDE's include a feature to save blocks of code for re-use and Eclipse is no exception. Snippets are suited for code that is not part of an existing template.

Add the snippet to your workspace by selecting Window > Show View > Other... and then select General > Snippets. You can add categories to group snippets however you'd like (ex. PHP, CakePHP, Zend Framework, CSS, Bash, Smarty, etc.). Once you have your categories, add snippets to them.

  1. Open a file and enter a new block or select an existing block of code.
  2. Copy the code block.
  3. Click in the Snippets view, under a specific target category, if you've created categories.
  4. Select "Paste as Snippet..." in the Snippets view menu.
  5. In the "Customize Palette" dialog, enter a name and description.
  6. Use the "Move Up" and "Move Down" buttons to order the snippet in lists as desired.
  7. Click OK

You can also right-click anywhere in the Snippets view and select "Customize" to add a snippet. I haven't worked with variables yet, I assume they work like Template variables do. You can set when snippet categories are displayed so that, for example, PHP-related snippets are displayed only when you're editing PHP files.

Snippets can be exported and imported just like templates. Once I've created and used them for a while, I may share a few snippet categories. Let me know if you've seen any good collections.

Happy coding!

filed under:

Comments

If I'm not mistaken these

If I'm not mistaken these tips should also work well for Zend Studio for Eclipse.

I'm using Eclipse PDT 2.1 to

I'm using Eclipse PDT 2.1 to work in PHP, either for small-and / or
large - I've been doing for quite some time now, actually (since before
1.0 stable, if I remember correctly) ... I was wondering if any of you know "tips" to be more efficient.
Which met to explain more in detail: I know about things like Aptana as
plugins (best editor of JS / CSS), subversion (SVN for access), RSE,
FileSync, integrated debugger Xdebug ... What I mean by
"advice" is a few things he discovered one day and since the use all the
time - and allow you to be more efficient in your PHP projects. Some examples of "tips" that come to mind, and they already know and use: * Ctrl + Space to open the list of suggestions for the functions / variable names
* Ctrl + Shift + R (sail> open resource) to open a popup window that
displays only the files containing the names of what he writes, that
is, opening fast file or this could be the perfect example: I know
this is often not known for his co-workers and find it as useful as me, here is example i am trying so for mensagens
so I suppose it could be a lot of other things like this do not know me ^
^ * Ctrl + M to switch to full screen view for editor (rather than double-clicking on the tab bar) while * Shift + F2 in a function name to open its page if the PHP manual in a browser Attention Mac users use Command instead of control.
I guess you get the point, but I'm very open to suggestions (either
related to the eclipse, in general, most PHP / Esp specific) that can
help you be more efficient :-) Anyway, thanks in advance for your help!

How to use PHP Comment Templates

There are three type of templates
Can you please let me know how to use phpcomment templates

Thanks
Shaoib.

Code Templates

Anyaone have ZendFramework code templates for eclipse?

Wow, thanks

I've been using Eclipse with PDT for almost two years now and I did use the template feature, but I never noticed this snippet thingy. Way cool!

Zend Studio For Eclipse

If I'm not mistaken these tips should also work well for Zend Studio for Eclipse.

All Eclipse plugins can provide Templates...

...but you'll need to check to confirm that the one you're using does :)

Snippet export - import

Am I missing something? How do you import - export snippets? I couldn't find this in the help file, preference, or under the file import/export menu.

eclipse 3.4.0 PDT 2.0.0 WST 3.0.0 on Win XP

Thank you for the fine article.

-Rb

Hmmm, I have import/export

Hmmm, I have import/export buttons in Preferences > PHP > Templates and in the Snippets customize dialog. I assumed that they were part of the eclipse or PDT by default. I am running DTP, but other than that, I'm not sure why you're not seeing the import/export buttons.

copyright © 2011, 2 tablespoons | Privacy Policy