* * *

Author Topic: Simple Templating Engine  (Read 1177 times)

sash

  • Jr. Member
  • **
  • Posts: 73
Simple Templating Engine
« on: May 03, 2018, 01:12:49 pm »
Hello all. In hope someone will need it, there's a Simple Templating Engine for Free Pascal: a set of classes to generate text (html, rtf, markdown) document, based on template.

* Pre-assigned variables
* OnExpandTag (global) callback
* Per-tag callbacks
* IF/ELSE blocks
* FOR blocks (dataset iteration)
* Nested blocks
* Separated syntax parsing (prepare) and variable expansion (process) on prepared templates.

Most basic usage:

Code: Pascal  [Select]
  1. with TSTEProcessor.Create do
  2.   try
  3.     SetValue('variable', 'your value');
  4.     ResultText := GenerateToString('Simple template text, your value is "<?variable?>"');
  5.   finally
  6.     Free;
  7.   end;
  8.  


Example included. Probably needs some docs, refactoring, polishing. In case of interest I will setup repo with docs, more examples.
« Last Edit: May 03, 2018, 05:27:37 pm by sash »
Lazarus 1.8.4 Unversioned directory FPC 3.0.4 x86_64-linux-gtk2

Thaddy

  • Hero Member
  • *****
  • Posts: 6568
Re: Simple Templating Engine
« Reply #1 on: May 03, 2018, 02:57:19 pm »
Did you know FPC already has a templating engine in the standard distribution...?
It is in /packages/fcl-base/src/fptemplate.pp
« Last Edit: May 03, 2018, 03:11:17 pm by Thaddy »
Ada's daddy wrote this:"Fools are my theme, let satire be my song."

howardpc

  • Hero Member
  • *****
  • Posts: 2718
Re: Simple Templating Engine
« Reply #2 on: May 03, 2018, 03:05:18 pm »
I think your chosen description of 'simple' for this template engine is good.
You've kept the code to the bare minimum of what is needed, and it is elegantly written.
In less than 1,000 lines of code you have implemented a sophisticated parser and processor for templates based on a simple, straightforward tag-format, with a surprisingly large feature set for something so 'simple'.
Thanks for sharing.

Thaddy

  • Hero Member
  • *****
  • Posts: 6568
Re: Simple Templating Engine
« Reply #3 on: May 03, 2018, 03:12:57 pm »
It is nice code indeed, Howard, but what's wrong with fptemplate? If any it is even simpler. (720 lines) and I use it all the time.
To me it feels like a hexagon shaped wheel. Close, but not round.

But compliments to the coder!
« Last Edit: May 03, 2018, 03:18:49 pm by Thaddy »
Ada's daddy wrote this:"Fools are my theme, let satire be my song."

howardpc

  • Hero Member
  • *****
  • Posts: 2718
Re: Simple Templating Engine
« Reply #4 on: May 03, 2018, 03:35:02 pm »
My comment did not in any way imply disparagement of FPC's fpTemplate (which is also good).
I shall probably (unlike you) never have occasion to use either one.
Nevertheless, credit where credit is due.
I always think it is worth pointing people towards well-written code. If it is not too long and complicated it is very instructive to peruse. Well, even very complex code is instructive, but it does require more effort, and is not likely to attract the casual browser.

edwinyzh

  • New member
  • *
  • Posts: 32
Re: Simple Templating Engine
« Reply #5 on: May 03, 2018, 03:59:35 pm »
@sash, thanks for sharing, it's valuable, would you setup a github repository? I'll check back this page sometime later...

sash

  • Jr. Member
  • **
  • Posts: 73
Re: Simple Templating Engine
« Reply #6 on: May 03, 2018, 05:27:15 pm »
Did you know FPC already has a templating engine in the standard distribution...?

Yes, I knew and tried it. But it seems like TFPTemplate doesn't support conditional/iteration blocks like IF, FOR, only tag replacements.
Of course, such logic could be implemented in Pascal code, but this way you're forced to put those parts of markup in Pascal string constants.
I'm personally preferring the idea that visual part (markup) should belong to template, not code.
Lazarus 1.8.4 Unversioned directory FPC 3.0.4 x86_64-linux-gtk2

korba812

  • Jr. Member
  • **
  • Posts: 79
Re: Simple Templating Engine
« Reply #7 on: May 03, 2018, 05:41:33 pm »
This one has less than 450 lines and has loops and conditionals:
https://github.com/michalgw/SimpleTpl/blob/master/simpletpl.pas

sash

  • Jr. Member
  • **
  • Posts: 73
Re: Simple Templating Engine
« Reply #8 on: May 03, 2018, 08:04:30 pm »
@korba812, seems like mine one is a little more advanced: works automatically for datasets and (mostly) for conditionals, without additional callbacks. So this is why more code here.

Anyway, I didn't want it sound like a contest. This is what I have and what works for me.
Will setup repo with more docs.
« Last Edit: May 03, 2018, 08:06:12 pm by sash »
Lazarus 1.8.4 Unversioned directory FPC 3.0.4 x86_64-linux-gtk2

Leledumbo

  • Hero Member
  • *****
  • Posts: 7937
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Simple Templating Engine
« Reply #9 on: May 04, 2018, 02:53:03 am »
The point of a template engine is to allow frontend engineers to build UI codes having variable contents without needing to tinker with how the data is produced, they only know if it can be displayed, checked or iterated. So this is always something worth appreciating as we don't have many alternatives.

I personally use Mustache standalone implementation (dmustache) from the author of mORMot, but I think the logicless concept that mustache wants to enforce for frontend isn't actually logicless. A traditional imperative style rendering like this is better IMO, frontend engineers can code imperatively (well, most of them) anyway, so why limit their freedom that much?

I do hope the for loop is more generic rather than limited to dataset, though.

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus