New in RAD Studio XE2: Project "Template Libraries"

Posted by on in Blogs
Clearly the biggest new feature of RAD Studio XE2, Delphi XE2 and C++Builder XE2 is FireMonkey: The New Generation Business Application Development Platform. However there are lots of bigger and smaller new features in RAD Studio XE2 that just do not get noticed. One of my favorite new IDE features are "Template Libraries". To be precise it is not completely a new feature. Template Libraries were available in Delphi 2007 and now they are back in XE2.

Template Libraries is a very powerful IDE repository mechanism for adding new projects to the "New Items" dialog. Back in 2007 it was Nick Hodges, Delphi Product Manager, who coined the term "improved start from scratchability" and that's exactly what template libraries are for.

Let's say you are constantly creating the same type of a project and starting from the same steps, like adding a menu, or maybe also a status bar. Instead of repeating your steps, you can be more productive by creating a template library that will be pointing to your "base" project, so every time you click on your new "base" project item in the "New Items" dialog, the IDE will copy all files from your "template" project to a directory of your choice. That could be also very useful for demos. You always want to start from the same state.

You can read more about Template Libraries in RAD Studio XE2 online docwiki: "Template Libraries Overview" and "Creating Template Libraries".

Template Libraries can be used for all types of projects that RAD Studio XE2 supports, for both Delphi and C++Builder projects. Let's give it a try! Here I'm going to walk you step by step through the process of creating a template library containing a "hello world" Delphi HD FireMonkey project with a "TScaledLayout" component for achieving the functionality of automatically resizable form.

Step 1: Create a project to be used as a "template"

Create a new Delphi "FireMonkey HD Application". Select "File \ New \ Other" and in the "New Items" dialog select "Delphi Projects \ FireMonkey HD Application".



Now it is an important moment. We need to decide where to save our new application that is going to serve later as a starting point for other applications. This is going to be our "application template". It makes a lot of sense to create a new directory for storing all custom templates. Let's create a new directory called "template_libs" at the same level as RAD Studio "Projects", "code_templates" folders inside your Windows document directory. I'm on Windows 7 and my user name is "pawel", so I have created "template_libs" folder in "C:\Users\Pawel\Documents\RAD Studio" directory. Any other directory would do as well.



Now inside this directory I'm going to create a folder for my scalable HD form application template called "ScalableHDApp".

Select "File \ Save All". Save the form unit inside this new directory as "ScalableFormUnit" and the project as "ScalableFormApp".

You can now resize the form so it is a little bit smaller. Drop "TScaledLayout" component on the form and set its "Align" property to "alClient", so it occupies the whole form.

That's our template. This is a good starting point for arbitrary Delphi FireMonkey HD applications with automatically scalable main form. Save all and close the project.

One last thing. We will need an icon for our project template. That could be any icon. For the purpose of this demo, let's use a Delphi icon that comes with the RAD Studio XE2 installation. Find "delphi_PROJECTION.ico" file in the "bin" folder of the RAD Studio XE2 installation and copy it to our project directory.

Step 2: Create "template library" xml file

Create a new empty XML file in RAD Studio. Select "File \ New \ Other" and inside the "New Items" dialog select "Web Documents \ XML File".



Save this file directly in the "template_libs" directory, just one level above the "ScalableHDApp" where all the files that make up the template project are stored. The name of this file is not important, but it is important to make sure that the extension of the file is "bdstemplatelib". I'm going to save my empty xml file as "mytemplatelib1.bdstemplatelib".

Now replace the content of the xml file with the following markup:


<?xml version="1.0" encoding="UTF-8"?>




<TemplateLibrary Version="1.0" id="MyCompanyXe2Templates1">



<Name>My Templates</Name>



<Description>My cool project templates</Description>



<Items>

<Item id="MyCompanyScalableHDApp1" Creator="DelphiProjectRepositoryCreator">

<Name>Scalable HD App</Name>

<Description>Delphi FireMonkey HD app with scalable form</Description>

<Author>Embarcadero</Author>

<Icon>ScalableHDApp\delphi_PROJECTICON.ico</Icon>

<Projectfile>ScalableHDApp\ScalableFormApp.dproj</Projectfile>

<DefaultProjectName>ScalableFormApp</DefaultProjectName>

<FilePath>ScalableHDApp</FilePath>

</Item>

</Items>



</TemplateLibrary>

Step 3: Install template library in the IDE

The last step is to install our new template library in the IDE. In the "Tools" menu select "Template Libraries". In the dialog click on "Add" button and select the "mytemplatelib1.bdstemplatelib" file. Click OK to close the dialog.



Now we should be able to find our new template in the "New Items" dialog.



It is there! We did it! Now it is time to see our new template in action!

Click on the "Scalable HD App" icon. Now you have to select a folder where you want the IDE to copy the contents of your template folder to.



I'm going to create a "ScalableAppTest" directory under the RAD Studio "Projects" folder. Select the folder in the dialog and click OK. Disregard the error message saying that the "bdsproj" file was not found. Click on "File \ Open Project" and inside of the folder that we have just specified you should fine a copy of all files from a template project. Open "ScalableFormApp" project and save it under some new name, depending on what you want to build now. I'm going to save the project as "ScalableHelloWorld".

Drop a "TButton" on the form.



Click on the green triangle button to run the project without debugging.

Play with resizing the application form and notice that the button is being resized with the form itself!



That's the power of vector-based FireMonkey graphics in a Delphi XE2 application!

Summary

In this blog post I have taken you through the process of creating a template library in the RAD Studio XE2. Template Libraries is a great productivity improvement feature that let you work from "improved start from scratchability"


About
Gold User, Rank: 9, Points: 364
Crazy about Delphi Programming!

Comments

  • Guest
    Simon J Stuart Monday, 28 November 2011

    Does this also work for Project Groups?

    As in... can I save an entire project group as a single Template, then create new Project Groups from that template (containing each project of their varying types such as Package, DLL, VCL App, FMX App all in the same project group)?

  • Guest
    Pawel Glowacki Wednesday, 21 December 2011

    Hi Simon,
    I guess that it just copies the entire contents of a folder with a template to a destination folder, so it should also work for Project Groups.

  • Guest
    Darian Miller Friday, 24 February 2012

    Thanks for the article! Everything seems to work, as I get the full project re-created in a new folder, except I get "Project file '\.bdsproj' not found

    There seems to be some discrepency in the FilePath per docs: http://docwiki.embarcadero.com/RADStudio/en/Creating_Template_Libraries

    "Specify the relative path to the .bdstemplatelib.xml in the value. For example, if your project is in C:\MyProjects\TemplateLibraries\MyTemplate, and you put the XML file in C:\MyProjects\TemplateLibraries, the value in the XML file would be TemplateLibraries\MyTemplate"

    Shouldn't this doc example be MyTemplate?



    Error info:
    [2068FF6F]{coreide160.bpl} Template.TProjectTemplate.OpenCopiedProject (Line 675, "Template.pas" + 16) + $27
    [2068FDDD]{coreide160.bpl} Template.TProjectTemplate.Selected (Line 645, "Template.pas" + 13) + $B
    [204E8996]{coreide160.bpl} GalleryTypes.TGalleryItem.DoSelect (Line 175, "GalleryTypes.pas" + 5) + $6
    [20696C8B]{coreide160.bpl} GalBrow.BrowseNewDialog (Line 212, "GalBrow.pas" + 23) + $10
    [20696B82]{coreide160.bpl} GalBrow.AddIcons (Line 166, "GalBrow.pas" + 16) + $9
    [2069B424]{coreide160.bpl} Gallery.LoadGallery (Line 711, "Gallery.pas" + 33) + $27
    [0041E5AB]{bds.exe } Sanctuary.LicenseFileHelper.TLicenseFileHelper.FindHelperDir (Line 125, "Sanctuary.LicenseFileHelper.pas" + 5) + $1E
    [500B533B]{rtl160.bpl } System.Classes.TBasicAction.Update (Line 13380, "System.Classes.pas" + 1) + $6
    [5031B705]{vcl160.bpl } Vcl.ActnList.TContainedAction.Execute (Line 448, "Vcl.ActnList.pas" + 8) + $2C
    [5031C4F0]{vcl160.bpl } Vcl.ActnList.TCustomAction.Execute (Line 1094, "Vcl.ActnList.pas" + 7) + $8
    [500B51FF]{rtl160.bpl } System.Classes.TBasicActionLink.SetAction (Line 13306, "System.Classes.pas" + 1) + $5
    [21734D6D]{vclactnband160.bpl} Vcl.ActnMenus.TCustomActionMenuBar.ExecAction (Line 1076, "Vcl.ActnMenus.pas" + 6) + $D
    [217365E4]{vclactnband160.bpl} Vcl.ActnMenus.TCustomActionMenuBar.TrackMenu (Line 1842, "Vcl.ActnMenus.pas" + 19) + $15
    [21739F1E]{vclactnband160.bpl} Vcl.ActnMenus.TCustomActionMainMenuBar.TrackMenu (Line 3636, "Vcl.ActnMenus.pas" + 5) + $3
    [2173496D]{vclactnband160.bpl} Vcl.ActnMenus.TCustomActionMenuBar.CMItemKeyed (Line 989, "Vcl.ActnMenus.pas" + 3) + $4
    [5033280D]{vcl160.bpl } Vcl.Controls.TControl.WndProc (Line 7204, "Vcl.Controls.pas" + 91) + $6
    [5033715B]{vcl160.bpl } Vcl.Controls.TWinControl.WndProc (Line 9976, "Vcl.Controls.pas" + 152) + $6
    [5003D4F7]{rtl160.bpl } System.TMonitor.TryEnter (Line 14786, "System.pas" + 10) + $0
    [5003D06C]{rtl160.bpl } System.TMonitor.Enter (Line 14489, "System.pas" + 4) + $2
    [5003CF10]{rtl160.bpl } System.TMonitor.CheckOwningThread (Line 14411, "System.pas" + 2) + $0
    [5003D216]{rtl160.bpl } System.TMonitor.Exit (Line 14600, "System.pas" + 9) + $7
    [5003D24F]{rtl160.bpl } System.TMonitor.Exit (Line 14614, "System.pas" + 2) + $7
    [50313627]{vcl160.bpl } Vcl.Graphics.FreeMemoryContexts (Line 7043, "Vcl.Graphics.pas" + 12) + $8
    [503367B0]{vcl160.bpl } Vcl.Controls.TWinControl.MainWndProc (Line 9689, "Vcl.Controls.pas" + 3) + $6
    [503367C5]{vcl160.bpl } Vcl.Controls.TWinControl.MainWndProc (Line 9692, "Vcl.Controls.pas" + 6) + $0
    [500B6030]{rtl160.bpl } System.Classes.MakeObjectInstance (Line 13921, "System.Classes.pas" + 0) + $0
    [5033715B]{vcl160.bpl } Vcl.Controls.TWinControl.WndProc (Line 9976, "Vcl.Controls.pas" + 152) + $6
    [217366FA]{vclactnband160.bpl} Vcl.ActnMenus.TCustomActionMenuBar.WndProc (Line 1881, "Vcl.ActnMenus.pas" + 25) + $4
    [503367B0]{vcl160.bpl } Vcl.Controls.TWinControl.MainWndProc (Line 9689, "Vcl.Controls.pas" + 3) + $6
    [500B6030]{rtl160.bpl } System.Classes.MakeObjectInstance (Line 13921, "System.Classes.pas" + 0) + $0
    [5032D636]{vcl160.bpl } Vcl.Controls.FindControl (Line 3540, "Vcl.Controls.pas" + 6) + $9
    [504525E7]{vcl160.bpl } Vcl.Forms.TApplication.ProcessMessage (Line 10164, "Vcl.Forms.pas" + 23) + $1
    [5045262A]{vcl160.bpl } Vcl.Forms.TApplication.HandleMessage (Line 10194, "Vcl.Forms.pas" + 1) + $4
    [5045295D]{vcl160.bpl } Vcl.Forms.TApplication.Run (Line 10331, "Vcl.Forms.pas" + 26) + $3

  • Guest
    Darian Miller Thursday, 22 March 2012

    QC entry for the annoying pop-up stating that the newly created project is not found right after creation: http://qc.embarcadero.com/wc/qcmain.aspx?d=104352

    Also, I couldn't get the DefaultProjectName to alter the behavior of the new project: http://qc.embarcadero.com/wc/qcmain.aspx?d=104351

  • Guest
    Tomohiro Takahashi Friday, 12 April 2013

    The xml file contains a mistake
    This line
    ScalableHDApp\ScalableFormApp.dproj
    should be
    ScalableHDApp\ScalableFormApp.dproj
    xml parser is case sensitive

  • Please login first in order for you to submit comments
  • Page :
  • 1

Check out more tips and tricks in this development video: