Copyright (c) 2019-2024 Sempare Limited
Contact: info@sempare.ltd
License: Apache v2.0 or Sempare Limited Commercial License
Open Source: https://github.com/sempare/sempare-delphi-template-engine
Questionaire: https://docs.google.com/forms/d/e/1FAIpQLScioIiDxvsWK01fMFqYr9aJ6KhCGeiw4UaU_esGuztEE7vYwA/viewform
Template engines are used often in technology where text needs to be customised by substituting variables with values from a data source. Examples where this may take place:
The Sempare Template Engine is a small templating (scripting) engine for Delphi (Object Pascal) that allows for templates to be created easily and efficiently by providing a simple and easy to use API.
Example usage:
program Example;
uses
Sempare.Template;
type
TInformation = record
name: string;
favourite_sport : string;
count : integer;
end;
begin
var tpl := Template.Parse(
'My name is <% name %>.'#13#10 +
'My favourite sport is <% favourite_sport %>.'#13#10 +
'Counting... <% for i := 1 to count %><% i %><% betweenitems %>, <% end %>' +
'Counting... <% for i := 1 to count ; print(i) ; betweenitems ; print(', '); onbegin; print('[ '); onend; print('] '); end %>'
);
var info : TInformation;
info.name := 'conrad';
info.favourite_sport := 'ultimate';
info.count := 3;
writeln(Template.Eval(tpl, info));
end.
The project allows for almost any type to be dereferenced within the template script.
In the example above, you can see that the '<%' start and '%>' end the scripting statement respectively. Within a scripting statement, you can reference variables, assign variables, use conditions, for and while loops, and include other templates.
NOTE In examples in this documentation I may use the latest Delphi syntax, e.g. inline variable declarations. This is not backward compatible as they were introduced in Delphi 10.2 and are used to shorten the code/examples being illustrated in the documentation. The codebase will attempt to be as backward compatible as possible.
Please 'star' the project on github.
There are a few ways to get started quickly.
Try the demo if you want to dive in quick and play with the template engine.
The Sempare Template Engine is not intended to be a fully featured general purpose programming language such as PHP where the script itself could be a self contained programming language (but it does have most of the features).
Sempare Template Engine aims to provide just enough functionality to allow you to easily work with the 'view' aspects of a template. Any enhanced functionality required from the scripting environment should be provided by the custom functions written in Object Pascal.
The template engine works with modern versions of Delphi.
Tests currently run using the DUnitX TestFramework.
An attempt has been made not to use the latest features to ease backward compatability. The following versions have been tested:
There should be no platform specific restrictions.
Have a look at Sempare.Template.Compiler.inc. The following defines can be defined if appropriate:
The Sempare Template Engine for Delphi can be installed via the Embarcadero GetIt manager
This will add the src folder to the search path so you can start working immediately.
The Sempare Template Engine for Delphi can be installed via the Boss package manager.
Simply run:
boss install sempare/sempare-delphi-template-engine
The Sempare Template Engine for Delphi can be installed via the Delphinus package manager.
This will add the src folder to the search path so you can start working immediately.
Start by adding the src folder to the Delphi search path. Otherwise, there are some projects you can use:
Open Sempare.Template.Engine.Group.groupproj which will include:
Sempare.Template.Pkg.dproj
The core template project. (runtime)
Sempare.Template.Tester.dproj
180+ unit tests
demo\SempareTemplatePlayground\Sempare.TemplateEngine.Playground.dproj
The Sempare Template Playground demo which provides a rich experience for testing the various template language features.
You can raise issues on GitHub and they will be addressed based on priority.
Most features have some basic tests in place. If a bug is been discovered, please include a basic test/scenario replicating the issue if possible as this will ease the investigation process.
Review contibution terms and conditions to contribute to the project.
Please followow the following steps when adding a feature or making an update:
The deployment process is based off the dev branch. Once we decide to push a new version, we will merge 'dev' into 'main'.
The Sempare Template Engine is dual-licensed. You may choose to use it under the restrictions of the Apache v2.0 at no cost to you, or you may license it for use under the Sempare Limited Commercial License
The dual-licensing scheme allows you to use and test the library under the appropriate license.
A commercial licence grants you the right to use Sempare Template Engine in your own applications, royalty free, and without any requirement to disclose your source code nor any modifications to Sempare Template Engine or to any other party. A commercial license helps to ensure the project is maintained with continuous integration, patches, etc.
A support fee of $70 per developer is appreciated.
The following payment links allow you to quickly subscribe. Please note that the initial license and support links are seperate.
The following payment links are available for site licenses. Please note that the initial license and support links are seperate.
Please send an e-mail to info@sempare.ltd to request an invoice which will contain alternative payment details.
Support and enhancement requests submitted by users that pay for support will be prioritised. New developments may incur additional costs depending on time required for implementation.