Skip to main content

By clicking Submit, you agree to the developerWorks terms of use.

The first time you sign into developerWorks, a profile is created for you. Select information in your profile (name, country/region, and company) is displayed to the public and will accompany any content you post. You may update your IBM account at any time.

All information submitted is secure.

  • Close [x]

The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerworks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

By clicking Submit, you agree to the developerWorks terms of use.

All information submitted is secure.

  • Close [x]

Getting started with Eclipse code templates

Move beyond snippets to increase your productivity

Jeremy Wischusen (cortex@visualflowdesigns.com), Flash, Flex, PHP Web Developer, Visual Flow Designs
Jeremy Wischusen has been designing Web sites since 1997 and programming since 2003. He has worked with Microsoft Gold Certified Partners to deliver software training for such companies as Wyeth Pharmaceuticals and The Vanguard Group. He has also worked on large-scale sites, such as myYearbook.com and for such clients as Dockers, Quicksilver, Major League Baseball, Sports Authority, CBS, and Liz Claiborne through GSI Commerce. He works with front- and back-end systems, designing many projects from start to finish using such technologies as Ajax, jQuery, CSS, XHTML, PHP, Flash, and Flex.

Summary:  This tutorial covers the basic use of Eclipse code templates. It takes a broad approach to expose the key concepts and skills that apply to templates regardless of the particular plug-in they're associated with. You will learn about the benefits of templates, how to create and edit them, and how to use them within the Eclipse IDE to increase your productivity.

Date:  07 Oct 2008
Level:  Intermediate PDF:  A4 and Letter (379 KB | 19 pages)Get Adobe® Reader®

Activity:  91457 views
Comments:  

Working with existing templates

Create your file

To use a template, you must create a document of the type that your templates are associated with. So, because this tutorial deals with HTML code templates, create an HTML file. Close the Preferences pane by clicking OK. If you have not done so, create a project so you can create a file to work with. The name of the project doesn't really matter, but you can call it templates, if you like.

You will create a file to work with, but you want to do so in a specific way so that you can see the template context:

  1. Click File > Other. The New window appears.

    Figure 2. The New window
    The New window

  2. In the Wizards tree, expand Web.
  3. Click HTML and click Next. The New HTML Page window appears.

    Figure 3. The New HTML Page window
    The New HTML Page window

  4. Type a name for the file. You can use any name you like, as you're only going to be working with one file.
  5. Click Next.

You should now be looking at a data grid with several templates in it.


Figure 4. Select an HTML template
Select an HTML template

This is an example of the context sensitivity of templates. Here, Eclipse knows you're creating a new document of type HTML. Because the HTML plug-in has a context called New HTML, the IDE presents those templates relevant to creating a new document. This would be the same for any other plug-in that defines templates for a new document. To use one of these templates, simply click it in the data grid and click Finish. (Other plug-ins may have additional steps, but as far as selecting the template goes, it is generally the same process.)

You should now be looking at a document that contains the code in the template you selected, which means that you're ready to see how templates work in the editor.

Use the editor

To work with your new page in the template:

  1. Click in the editor so that the cursor is in between the HTML <body> tags.
  2. Press Ctrl+Space (or Cmd+Space on the Mac) to trigger the auto-completion box.
  3. Start typing the word comment. By the time you get to the first m, the word comment should be highlighted in the auto-completion box.

    Figure 5. The text in the auto-completion box
    The text in the auto-completion box

  4. Press Enter to insert the text.

You should now have an HTML comment in your page, and the cursor should be blinking between the starting and ending <comment> tags. So, what just happened? Because you're now within the editor window and within an HTML tag, you're in the HTML Tag context. Hence, the IDE presents those templates related to that context. Remember that the comment template looked like this: <!-- ${cursor} --> and that ${cursor} is a predefined variable. This variable causes the cursor to be located between the comment tags when the template is inserted.

As far as using templates goes, that's basically it. Based on the context of where you are and what you're doing, the IDE presents you with appropriate templates. All you have to do is select the template you want.

3 of 9 | Previous | Next

Comments



static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=Open source
ArticleID=342826
TutorialTitle=Getting started with Eclipse code templates
publish-date=10072008
author1-email=cortex@visualflowdesigns.com
author1-email-cc=