Working with existing templates
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:
- Click File > Other. The New window appears.
Figure 2. The New window
- In the Wizards tree, expand Web.
- Click HTML and click Next. The New HTML Page window appears.
Figure 3. The New HTML Page window
- Type a name for the file. You can use any name you like, as you're only going to be working with one file.
- Click Next.
You should now be looking at a data grid with several templates in it.
Figure 4. 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.
To work with your new page in the template:
- Click in the editor so that the cursor is in between the HTML
<body>tags. - Press Ctrl+Space (or Cmd+Space on the Mac) to trigger the auto-completion box.
- Start typing the word
comment. By the time you get to the firstm, the word comment should be highlighted in the auto-completion box.
Figure 5. The text in the auto-completion box
- 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.



