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:- 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.
- 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> - Firstly, set the name (attribute) of the template (tag). Let's call it "url"
- 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". - Secondly, fill the "author" and the "description" sections.
- Next, put in the template code (CDATA section) and specify which language we're targeting ("code" tag with attributes).
- Declare a "jump point" section so that the template parser knows where to put the url you type while using the template.
- Finally, save the template in the templates folder, name it "url.xml"
<?xml version="1.0" encoding="utf-8" ?> <codetemplate xmlns="http://schemas.borland.com/Delphi/2005/codetemplates" version="1.0.0">That's it. Open a source file, type "url" and hit TAB. Whoah!
<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>
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


