Sign in Join
Wiki copytemporary

Project Templates: what changed

Revision 1 to 2. Removed lines have a minus sign; added lines have a plus sign.

OldNewLine
11# Overview
22
33You can adapt how to create new projects. The IDE allows to register your own project types via designtime packages. The package **projtemplates** (the normal lazarus binary packages have this preinstalled) allows to simply use existing projects as templates.
44
55# Package projtemplates
66
77This package implements project templates and comes with the lazarus sources (components/projecttemplates/projtemplates.lpk).
88
99The package comes preinstalled with the binary packages of Lazarus (e.g. the windows installer). This package adds a category to the **File-New** dialog: **Template projects**
1010
1111It also adds a **New project from Template** menu item to the **File** menu, as well as a **project template options** menu under the **Tools** menu. This menu item is visible only if the templates directory (see next paragraph) exists and contains at least one template subdirectory.
1212
1313In the *Project template options*, a directory can be selected. This directory should contain a subdirectory per template. Each template directory contains a template for a project: A collection of files which will be copied and used as a new project.
1414
1515During the copy, the package will scan the file/directory names and the contents of the files for variable substitution: **__VARNAME__** will be replaced by the value of a variable VARNAME
1616
1717By default, the engine knows 2 variables:
1818
1919- ProjDir: The directory where the new project will be created.
2020- ProjName: The name of the new project in ProjDir.
2121
2222Additional variables must be defined in a file called **project.ini** in the directory of the project template. This is a file in Windows INI format.
2323
2424The section *\[Variables\]* will be scanned for variable names:
2525
2626``` ini
2727 [Variables]
2828 VarName1=Description 1[|DefaultValue 1]
2929 VarName2=Description 2[|DefaultValue 2]
3030```
3131
3232The variables found will be presented with their description in a dialog, and the user must present a value for the variables.
3333
3434The' 'project.ini*file can contain a second section, called*Project'', which can contain some info about the project. The following keywords can be found there:
3535
3636## ProjectFile
3737
3838The filename (no extension) of the template project lpi-file. Any file that has a name equal to this (no extension) is treated specially, it is renamed to the new project name (ProjName). That is *ProjectFile=example* will replace *example.lpi*, *example.lpr* and *example.cfg* with the name given by the user. By default, the value of **ProjectFile** is assumed to be *project*. (note that the .ini file is not copied)
3939
4040ProjectFile is not mandatory. The first *.lpi file in the template project folder will be taken as ProjectFile if ProjectFile is not specified or doesn't exist.
4141
4242## Name
4343
4444Name of the template. The Name and description will be presented in the 'File-New' dialog.
4545
4646## Author
4747
4848Author of the template
4949
5050## Description
5151
5252Short (one-line) description of the project. The Name and description will be presented in the 'File-New' dialog.
5353
5454## Recurse
5555
5656A boolean value (1/0) which tells the engine to recurse in subdirectories or not.
5757
5858## Exclude
5959
6060Comma separated list of filename extensions which should not be searched for keyword subsitution.
6161
6262## Getting Started
6363
6464**Step 1:**
6565
6666Create your own template directory (for example ~/.lazarus/mytemplates) and set this path in Lazarus (menu: Tools -\> Project Template Options).
6767
6868**Step 2:**
6969
7070For any template functions to even appear in the Lazarus File menu, you will need at least one template which you can base on your existing (normal) project;
7171
7272\- Create a directory in your template directory (for example ~/.lazarus/mytemplates/myfirsttemplate)
7373
7474\- Copy your project files in this new directory (*.lfm, *.pas, *.ico, *.lpi, *.lpr, *.res)
7575
7676**Step 3:**
7777
7878Create a "project.ini" file in the new template directory.
7979
8080The minimum should look something like this:
8181
8282``` ini
8383 [Variables]
8484
8585 [Project]
8686 Name=My Form Template
8787```
8888
8989**Step 4:**
9090
9191Restart Lazarus to apply changes.
9292
9393*Optional:*
9494
9595The nice thing of Project Templates is that it supports variables. Since a project will probably not be named the same as your example template you will have to do some code editing and file renaming.
9696
9797**Files**:
9898
9999Rename the project file (ico, lpi, lpr, res) to __PROJNAME__.ext (where extension is the original extension of course). For example: project.lpi becomes __PROJNAME__.lpi. Note: the content of the lpi file might need some cleaning, for example removing history, reference to files, not sure if the lpi file is even needed.
100100
101101**File content**:
102102
103103For all files that referred to project.lpi etc., change the content of your files so all "project" references become "__PROJNAME__", e. g.:
104104
105105```text
106106 <Filename Value="project.lpr"/>
107107 <Filename Value="__PROJNAME__.lpr"/>
108108```
109109
110110You can optionally define your own variables, for example to give the "unit.pas" a different filename. You could define for example __MAINUNIT__ and __MAINFORM__ in project.ini as MAINUNIT and MAINFORM - during creation Lazarus will ask you for these names. Now every occurrence of __MAINUNIT__, for example, will be replaced with the text you provided (avoid special characters and spaces).
111111
112112Note that this works for all variables. Every occurrence of a variable in filenames and file content will be replaced by what you entered on creation of a new project based on your template. Predefined variables are PROJNAME and PROJDIR but I didn't find a purpose for PROJDIR just yet.
113113
114114**Some examples of a project.ini:**
115115
116116project.ini:
117117
118118``` ini
119119 [Variables]
120120 MAINUNIT=Filename for the default unit.pas file
121121 MAINFORM=Form name
122122
123123 [Project]
124124 Name=My Custom Form
125125 Author=Author's Name
126126 Description=Lazarus LCL form specifically for my needs
127127```
128128
129129__PROJNAME__.lpr:
130130
131131``` pascal
132132program __PROJNAME__;
133133
134134{$mode objfpc}{$H+}
135135
136136uses
137137 {$IFDEF UNIX}
138138 cthreads,
139139 {$ENDIF}
140140 {$IFDEF HASAMIGA}
141141 athreads,
142142 {$ENDIF}
143143 Interfaces, // this includes the LCL widgetset
144144 Forms, __MAINUNIT__
145145 { you can add units after this };
146146
147147{$R *.res}
148148
149149begin
150150 RequireDerivedFormResource:=True;
151151 Application.Scaled:=True;
152152 Application.Initialize;
153153 Application.CreateForm(T__MAINFORM__, __MAINFORM__);
154154 Application.Run;
155155end.
156156```
157157
158158__MAINUNIT__.pas:
159159
160160``` pascal
161161unit __MAINUNIT__;
162162
163163{$mode ObjFPC}{$H+}
164164
165165interface
166166
167167uses
168168 Classes, SysUtils, Forms, Controls, Graphics, Dialogs;
169169
170170type
171171 T__MAINFORM__ = class(TForm)
172172 private
173173
174174 public
175175
176176 end;
177177
178178var
179179 __MAINFORM__: T__MAINFORM__;
180180
181181implementation
182182
183183{$R *.lfm}
184184
185185end.
186186```
187187
188188__MAINUNIT__.lfm:
189189
190-``` pascal
190+``` lfm
191191object __MAINFORM__: T__MAINFORM__
192192 Left = 725
193193 Height = 366
194194 Top = 433
195195 Width = 503
196196 Caption = '__MAINFORM__'
197197 LCLVersion = '2.2.6.0'
198198end
199199```
200200
201201Keep in mind that this brief tutorial is just to get you started, the variable replacement mechanism offered can be utilized for many different purposes. For example to define a form caption, a copyright statement, a web link on your forms, specific objects, etc. --Hansaplast 16:30, 4 August 2012 (UTC)
202202
203203---
204204*From the Free Pascal wiki, page "Project Templates", as it stood on 2023-12-15 after a change by Mark Paley, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).*

Back to history