Recent

Author Topic: Simple Template Engine  (Read 5643 times)

sash

  • Sr. Member
  • ****
  • Posts: 366
Simple Template 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.

https://github.com/sash-rc/ste
« Last Edit: December 07, 2019, 05:36:09 pm by sash »
Lazarus 2.0.10 FPC 3.2.0 x86_64-linux-gtk2 @ Ubuntu 20.04 XFCE

Thaddy

  • Hero Member
  • *****
  • Posts: 14205
  • Probably until I exterminate Putin.
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 »
Specialize a type, not a var.

howardpc

  • Hero Member
  • *****
  • Posts: 4144
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: 14205
  • Probably until I exterminate Putin.
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 »
Specialize a type, not a var.

howardpc

  • Hero Member
  • *****
  • Posts: 4144
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: 43
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

  • Sr. Member
  • ****
  • Posts: 366
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 2.0.10 FPC 3.2.0 x86_64-linux-gtk2 @ Ubuntu 20.04 XFCE

korba812

  • Sr. Member
  • ****
  • Posts: 392
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

  • Sr. Member
  • ****
  • Posts: 366
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 2.0.10 FPC 3.2.0 x86_64-linux-gtk2 @ Ubuntu 20.04 XFCE

Leledumbo

  • Hero Member
  • *****
  • Posts: 8746
  • 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.

sash

  • Sr. Member
  • ****
  • Posts: 366
Re: Simple Templating Engine
« Reply #10 on: November 26, 2019, 02:13:12 pm »
As a response to request, I've attached more recent version.
Some cosmetic improvements and a new feature: templates caching (greedy/lazy). Optional class that may be useful for standalone webserver.

Sorry, no public repo this time yet.
Lazarus 2.0.10 FPC 3.2.0 x86_64-linux-gtk2 @ Ubuntu 20.04 XFCE

sash

  • Sr. Member
  • ****
  • Posts: 366
Re: Simple Template Engine
« Reply #11 on: December 07, 2019, 05:41:18 pm »
Replaced attachment with a repository link.
Added a Wiki entry in https://wiki.freepascal.org/Template_Engines

Sorry, still not much docs.
Lazarus 2.0.10 FPC 3.2.0 x86_64-linux-gtk2 @ Ubuntu 20.04 XFCE

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Simple Template Engine
« Reply #12 on: December 07, 2019, 06:12:23 pm »
Sorry, still not much docs.

Better start on the docs while it is still "simple" ;D

I'd suggest a small change, though: make it a runtime package so that users don't have to add the extra path to their projects. Adding extra paths to projects seems to be prone to unexpected compile-time "bugs".
« Last Edit: December 07, 2019, 06:18:09 pm by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

sash

  • Sr. Member
  • ****
  • Posts: 366
Re: Simple Template Engine
« Reply #13 on: December 07, 2019, 10:04:46 pm »
I'd suggest a small change, though: make it a runtime package so that users don't have to add the extra path to their projects. Adding extra paths to projects seems to be prone to unexpected compile-time "bugs"

Hmm... I thought adding library in a form of plain source units is a most basic operation, even for novice.
And I doubt someone will benefit by adding extra package instead of extra path to a project.
Both is done by selecting files/dirs in GUI. And the worst "bug" you can get is "Cannot find xxx used by yyy",
which is pretty simple to fix: invalid paths are greyed.
Also, non-Lazarus environments (pure FPC, another Pascal compilers, m.b. Delphi) will not be affected by this feature at all.

So honestly I think it's overkill. But if someone want to improve it, you're welcome. Fork it, or I could even give you an access as collaborator.

But I'd better write documentation and better samples.


Lazarus 2.0.10 FPC 3.2.0 x86_64-linux-gtk2 @ Ubuntu 20.04 XFCE

 

TinyPortal © 2005-2018