1. Home
  2. Computing & Technology
  3. Delphi Programming

Creating Custom Code Templates in Delphi 2006

By Zarko Gajic, About.com Guide

One of the new features in Delphi 2006 are live code templates. Code templates provide a means of automating the task of typing frequently used code structures.

Beside providing a set of predefined code templates, Delphi 2006 allows you to add your own custom templates. To see the full list, select "View - Templates" from the main IDE menu. A docked window will appear.
Existing templates are stored in an XML file format in the BDS\4.0\objrepos\code_templates folder, a sub-folder exists for each language type.

"Open URL in Browser" Custom Template

Here's how to create a code template to open an URL in your default browser:
  1. To create an empty custom code template click the "New" button on the templates toolbar dialog. Optionally, you can pick File | New | Other | Other Files | Code Template.
  2. A skeleton for your custom template will get displayed in the Code Editor.
    <?xml version="1.0" encoding="utf-8" ?> <codetemplate xmlns="http://schemas.borland.com/Delphi/2005/codetemplates" version="1.0.0">
    <template name="" invoke="manual">
    <description>
    </description>
    <author>
    </author>
    <code language=""><![CDATA[]]>
    </code>
    </template>
    </codetemplate>
  3. Firstly, set the name (attribute) of the template (tag). Let's call it "url"
  4. The "invoke" attribute specifies how this template is invoked. Possible values include: manual - invoked by pressing TAB; auto - invoked by pressing SPACE or TAB; none - invoked by using CTRL+J, CTRL+SPACE, or using the template viewer.
    Specify "auto".
  5. Secondly, fill the "author" and the "description" sections.
  6. Next, put in the template code (CDATA section) and specify which language we're targeting ("code" tag with attributes).
  7. Declare a "jump point" section so that the template parser knows where to put the url you type while using the template.
  8. Finally, save the template in the templates folder, name it "url.xml"
Here's the complete "Open URL in Browser" template:
<?xml version="1.0" encoding="utf-8" ?> <codetemplate xmlns="http://schemas.borland.com/Delphi/2005/codetemplates" version="1.0.0">
<template name="url" invoke="auto">
<description>
Open URL in Browser
</description>
<author>
Zarko Gajic
</author>
<point name="url">
<text>
delphi.about.com
</text>
<hint>
URL of the page
</hint>
</point>
<code language="Delphi" delimiter="|">
<![CDATA[ShellExecute(Application.Handle,
PChar('open'),
PChar('|url|'),
PChar(0),
nil,
SW_NORMAL) ;
]]>
</code>
</template>
</codetemplate>
That's it. Open a source file, type "url" and hit TAB. Whoah!

Related: Creating a simple template to make a more type safe descendant of TBucketList

Delphi tips navigator:
» How to Right Align a Menu Item
« How to Play a Sound When the Mouse Enters a (Delphi) Component

More Delphi Programming Quick Tips
Explore Delphi Programming
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. Delphi Programming
  4. Coding Delphi Applications
  5. Delphi Tips and Tricks
  6. Delphi 2006 Tips
  7. Creating Custom Code Templates in Delphi 2006

©2009 About.com, a part of The New York Times Company.

All rights reserved.