October 12, 2011

Visualforce Code Templates/Snippets for Force.com IDE !

I recently saw a lot of action around Mac’s favorite editor TextMate. Force.com community has done a great job to create a full fledged IDE and various code templates for ease in working.

I like TextMate, but I am more inclined towards Force.com IDE, because of its platform neutral nature. Most of my force.com pals are either on Windows machines and few of the geeks are using Ubuntu too. So I am always more motivated towards doing something using Force.com IDE.

Force.com IDE is already doing a decent job on top of Eclipse. It gives a nice Visualforce editor, that gives syntax highlighting and code indentation based on standard Eclipse HTML Editor. I recently tried to create a couple of Visualforce code templates/snippets to quickly assist in creating the pages, and was successful in that attempt. Rest of the post describes how.

How to setup Visualforce code templates for Eclipse ?

Here are the steps

  1. Goto Eclipse > Preferences.
  2. Search for “Template” in the left pane preference filter box, as shown belowEclipse Preferences Pane with Template Search Text 
  3. Hit the “New” button on extreme right side to start creating a new Template, as shown :Create New template for visualforce tag
  4. In the dialog next, enter tag name in the text field labeled “Name” and code snippet in text area labeled “Pattern”. Also select context as “HTML Tag”, as shown below : Defining template name and pattern for visualforce
  5. Now, in any visualforce page in Force.com IDE, press CTRL + SPACE and start typing the tag name, like apex:p… In the content assist box, you will see the newly created template as shown below. Hit enter to get the template inserted in current code location.  Visualforce template based content assist box in Eclipse

Using the same steps as above, I have created a few templates for visualforce like

Field Label Template

Visualforce Field Label template

CRUD FLS Permission

Visualforce CRUD FLS check template

Creating a useful template library for Visualforce !

The good part about Eclipse templates is those can be exported and imported back via XML. I started creating a few code snippets/templates and the export for them is available in gist here.

One can easily import this GIST on the same template page using the “Import” button near the New one. Similarly exporting yours is easy too, just select templates to export and hit “Export” button.

Please join me to add more and better code templates for making Visualforce development easier in Eclipse.