Jump to content
Edwin Yip

The BEST template engine for generating webpages on the server side?

Recommended Posts

Posted (edited)

For my new project I need a template engine for generating web pages, while I have concluded which suited my needs the best and I'm quite confident about my conclusion, your opinions and comments might help me to make my final decision.

 

Since mORMot is my go-to framework for server-side web development, the first candidate to consider is obviously DMustache, which is a fast Delphi/FPC implementation of the popular logic-less Mustache template engine. Actually I've used it in several of my projects and it's great. But sometimes I need more features, but as you know, logic-less means no "if", "while", and so on.

 

And fortunately I found Sempare Template Engine, which is the only feature-rich template engine for Delphi ( Yes, I meant it, because we all know Delphi was/is mostly focused on GUI development but not web development...)

 

Pros

===

- Very very feature-rich, "if", "for and while loop", "include", "JSON", "custom methods", and so on. Basically, the feature set it's very close to template engines you can find in web-focused languages such as Java. No other Delphi template engine is even comparable (sorry, I love DMustache, but I also need "if" and "while"...).

- Very well designed! You can tell by just looking at the unit namespaces and the source!

- Documented, and with examples.

- Well-tested (with DUnitX tests)

 

Cons: While It's also open source, but for commercial uses you need to buy a license key with a very reasonable price.

 

There are other template engines too, but IMHO, only DMustache and Sempare Template are serious, so I won't list them here...

 

Your comments are welcomed!

Edited by Edwin Yip
  • Like 1

Share this post


Link to post

@Fr0sT.Brutal, Thanks for the link!

From the overview webpage the features seems to be quite powerful, but it doesn't seem to be compatible with my XE4 compiler:

[dcc32/dcc64 Error] TemplateEngine.pas(2253): E2030 Duplicate case label

 

Feature-set aside, it still has less documents and examples than Sempare Template.

But thanks for the link!

Share this post


Link to post
1 hour ago, Edwin Yip said:

@Fr0sT.Brutal, Thanks for the link!

From the overview webpage the features seems to be quite powerful, but it doesn't seem to be compatible with my XE4 compiler:

[dcc32/dcc64 Error] TemplateEngine.pas(2253): E2030 Duplicate case label

 

Feature-set aside, it still has less documents and examples than Sempare Template.

But thanks for the link!

Oh yep. I fixed that in my fork, here's the diff (IDK if it will apply onto initial file)

 TemplateEngine.pas | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/TemplateEngine.pas b/TemplateEngine.pas
index aa8bb38..11a59ef 100644
--- a/TemplateEngine.pas
+++ b/TemplateEngine.pas
@@ -31,6 +31,7 @@ unit TemplateEngine;
 {$B-}    //Boolean short-circuit evaluation
 {$O+}    //Optimization on
 {$R-}    //Range checking off
+{$HIGHCHARUNICODE ON}
 {.$DEFINE SMARTYDEBUG}
 
 interface
@@ -2307,7 +2308,7 @@ begin
       '>': Result := Result + '>';
       '''': Result := Result + ''';
       '"': Result := Result + '"';
- {     #160: Result := Result + ' ';
+      #160: Result := Result + ' ';
       #161: Result := Result + '&iexcl';
       #162: Result := Result + '¢';
       #163: Result := Result + '£';
@@ -2555,7 +2556,7 @@ begin
       #9827: Result := Result + '♣';
       #9829: Result := Result + '♥';
       #9830: Result := Result + '♦';
-}    else
+    else
       Result := Result + Ch;
     end;
   end;

With the commit message "! Fixed compiling HTMLEncodeEntities under XE2, uncommented stuff. Dumb compiler behavior when in considers char constants ANSI" 😄

Share this post


Link to post
1 hour ago, Edwin Yip said:

Feature-set aside, it still has less documents and examples than Sempare Template.

They have pretty good help file and rich set of functions. Worth a try. Yes it seems abandoned but it's opensource with friendly license.

Share this post


Link to post

@mvanrijnen @Edwin Yip Thanks for the references. 

 

The aim with the Sempare Template Engine was to have a template engine that was native to Delphi and could be used for web, but also for other kinds of purposes such as code or config generation. At the time I wrote it, dmustache was mainly what was available, but I wanted a template language that was more readable and pascal-like, and from a library perspective providing more flexibility in terms of extension points (custom functions) and the ability to support various sub-template inclusion patterns (content with header/footer, main template referencing content template, etc) and dynamic loading. Due to the fact that looping is a feature, it also has to be safe and offers a timeout if template processing takes too long due to data processing or bad logic. 

 

It does rely on RTTI, which does mean there is a performance penalty, but I chose functionality/flexibility over performance to start. I have not had problems reported yet, so I suspect performance has been acceptable for users, as it has been for my use cases. 

 

Documentation wise, it should be fairly well covered on github, but I have been working on a CHM help edition which can be used from the IDE with context sensitive help which may be a bit more friendly for those that don't want to necessarily have to browse to the the github docs. Please email me conrad@sempare.ltd/conrad.vermeulen@gmail.com if you are interested and can notify you when it is available.


If you have feature requests/comments feel free to raise them on the issue tracker (https://github.com/sempare/sempare-delphi-template-engine/issues). I do check here periodically, but may miss things or reply late.

 

In the next release, I'm allowing for discovery of variables and functions that are referenced. So if you provide a template editor and are using a dictionary/json to pass variables, the UI can know upfront what the required variables are and perform additional validation if required, prompting the user if anything is missing. Alternatively, it could be a way to identify variables that are required, allowing for optimal querying of data from database before populating the template. This is just a complementary feature to an existing feature where errors can be raised if a referenced variable is not present.

 

Further, I hope to create some more examples with more popular web frameworks soon, as I think many people find it easier to see how things work from demos.

 

I realise licensing as GPL may be contentious, but I've gone with the 'free as in speech vs free as in beer' approach. So for a nominal one time fee, you can use it under the commercial license without any restriction.

 

I'll keep you posted on new developments as they unfold.

  • Like 5

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

×