Jump to content
darnocian

ANN: Sempare Template Engine for Delphi

Recommended Posts

I'd like to advertise the Sempare Template Engine for Delphi. The Sempare Template Engine for Delphi allows for flexible text manipulation. It can be used for generating email, html, source code, xml, configuration, etc.

 

It is available on github via https://github.com/sempare/sempare-delphi-template-engine

It is also available via Delphinus (https://github.com/Memnarch/Delphinus)

 

Simply add the 'src' directory to the search path to get started.

 

Sample usage:

program Example;
uses
    Sempare.Template;
type
    TInformation = record
        name: string;
        favourite_sport : string;
    end;
begin
    var tpl := Template.parse('My name is <% name %>. My favourite sport is <% favourite_sport %>.');
    var information : TInformation;
    information.name := 'conrad';
    information.favourite_sport := 'ultimate';
    writeln(Template.eval(tpl, information));	
end.

 

Features include:

  • statements
    • if, elif, else statements
    • for and while statements
    • include statement
    • with statement
    • function/method calls
  • expressions
    • simple expression evaluation (logical, numerical and string)
    • variable definition
    • functions and methods calls
    • dereference records, classes, arrays, JSON objects, TDataSet descendants and dynamic arrays
    • ternary operator
  • safety
    • max run-time protection
  • customisation
    • custom script token replacement
    • add custom functions
    • strip recurring spaces and new lines
  • lazy template resolution
  • parse time evaluation of expressions/statements
  • allow use of custom encoding (UTF-8 with BOM, UTF-8 without BOM, ASCII, etc)
  • extensible RTTI interface to easily dereference classes and interfaces (current customisations for ITemplateVariables, TDictionary, TJsonObject)
  •  

There are numerous unit tests that can be reviewed as to how to use the various features.

 

Happy for all to play with it. Released under GPL and Commercial License.

 

Any feedback welcome.

  • Like 8
  • Thanks 3

Share this post


Link to post

This library fills the gap of the missing of a real powerful template engine in the Delphi community.

DMustache is good, but it's logic-less. I'm not talking about it's bad - on the contrary, it's good for its use cases, but we need more powerful scripting in other use cases.

Had been working with the developer for XE4 compatibility few days ago (initially more modern language features were used), and he was able to quickly get thing sorted out without an XE4 compiler. Really impressed!

Share this post


Link to post

Was looking for something like this.

Created myself a few half working "engines" (just for own needs), Hopefully can replace those with one good one 🙂

 

Share this post


Link to post

Looking now to implement the engine in an application here,  but when i have a template like:

 


Value: <% value %>
Description: <% description %>

 

I get double crl/lfs in the result?

 

 

[edit]

after inspection it seems like that the Template.Eval function adds an extra CR is added.   (e.g.  #$D#$A becomes #$D#$D#$A)  

 

quickfix: 🙂

Result := Template.Eval(FParsedTemplate, objrec).Replace(#13#13, #13, [rfReplaceAll]);

 

Edited by mvanrijnen

Share this post


Link to post

I just looked at the tests... mostly testing with #$A. 😉 But made a test to replicate the issue. 

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×