Recent

Author Topic: Port of Tera templating engine to Free Pascal  (Read 1034 times)

Gustavo 'Gus' Carreno

  • Hero Member
  • *****
  • Posts: 1329
  • Professional amateur ;-P
Port of Tera templating engine to Free Pascal
« on: July 31, 2025, 04:51:40 pm »
Hey Y'All,

We all know how bare the templating engines landscape is in Free Pascal.

With that mind, I decided to ask the Oracle for some help in implementing something like the Rust of Go templates.
The Oracle said that they both take a lot of inspiration in the Tera Templating Engine, and another that is JINI1(????)
The Oracle gave me an initial implementation, but no AST. I then pressed for a full AST example and a good Expression Parser.

The result of both the Oracle output and my own customisation and addition of the AST walker for rendering is now present here: fp-tera-tpl.

A bullet list of accomplished objectives:
  • The {% include %} tag.
  • The {% if %} tag with full expression evaluation.
  • The {% for..in %} tag.
  • The {{ variable }} tag.

I still want to attempt to add Left and Right Trim with these tag starting and ending: {%- and -%}.

Cheers,
Gus
Lazarus 4.99(main) FPC 3.3.1(main) Ubuntu 25.04 64b Dark Theme
http://github.com/gcarreno

Thaddy

  • Hero Member
  • *****
  • Posts: 18324
  • Here stood a man who saw the Elbe and jumped it.
Re: Port of Tera templating engine to Free Pascal
« Reply #1 on: July 31, 2025, 04:59:45 pm »
fcl-web contains a pretty good templating engine.
You can expand that if required.
It is written for - but not tied to - webpages, but is is really agnostic.
« Last Edit: July 31, 2025, 05:03:04 pm by Thaddy »
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

Gustavo 'Gus' Carreno

  • Hero Member
  • *****
  • Posts: 1329
  • Professional amateur ;-P
Re: Port of Tera templating engine to Free Pascal
« Reply #2 on: July 31, 2025, 05:19:12 pm »
Hey Thaddy,

fcl-web contains a pretty good templating engine.
You can expand that if required.
It is written for - but not tied to - webpages, but is is really agnostic.

I've used that templating engine before. I was quite frustrated that there was only call back functionality. And sure, with some time I could probably get it to a semblance of the Tera one. I think...

Both Rust and Go have this quite appealing syntax, with includes, conditionals and loops. Not to mention the fact that you can send a Class/Struct into them and they just consume it. That's still in the To Do list for mine.
I've now got fp-tera-cli to a point where you don't fight the call backs and have a quite comprehensive Expression Parser for conditionals.
Which is to say that, out-of-the-box, you can just drop a .tpl file, and it just works. With fp-web, there's a lot of assembly required!!

Again, the template engine that comes with fp-web is not that bad. It just lacks some basic features, IMHO. And after doing it with the Context concept, I quite like it.

Cheers,
Gus
Lazarus 4.99(main) FPC 3.3.1(main) Ubuntu 25.04 64b Dark Theme
http://github.com/gcarreno

Thaddy

  • Hero Member
  • *****
  • Posts: 18324
  • Here stood a man who saw the Elbe and jumped it.
Re: Port of Tera templating engine to Free Pascal
« Reply #3 on: July 31, 2025, 05:36:05 pm »
It is in fcl-base, but we are talking about the same.
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

gues1

  • Guest
Re: Port of Tera templating engine to Free Pascal
« Reply #4 on: July 31, 2025, 05:49:34 pm »
I don't know if I understand good, but there is something insede here that can help ?

https://castle-engine.io/

Gustavo 'Gus' Carreno

  • Hero Member
  • *****
  • Posts: 1329
  • Professional amateur ;-P
Re: Port of Tera templating engine to Free Pascal
« Reply #5 on: July 31, 2025, 05:58:55 pm »
Hey Thaddy,

It is in fcl-base, but we are talking about the same.

OOPS!! Mea culpa, sorry !!!
Thanks for the correction!!

I'm tired. Spent about 20H on the port...

Cheers,
Gus
Lazarus 4.99(main) FPC 3.3.1(main) Ubuntu 25.04 64b Dark Theme
http://github.com/gcarreno

Gustavo 'Gus' Carreno

  • Hero Member
  • *****
  • Posts: 1329
  • Professional amateur ;-P
Re: Port of Tera templating engine to Free Pascal
« Reply #6 on: July 31, 2025, 06:01:12 pm »
Hey Gues1,

I don't know if I understand good, but there is something insede here that can help ?

https://castle-engine.io/

Hummmm.... Never considered that Castle Engine might have a templating engine of itself.

Will have to have a look and maybe update My TestStar entry about templates.

Thanks for the hint !!

Cheers,
Gus
Lazarus 4.99(main) FPC 3.3.1(main) Ubuntu 25.04 64b Dark Theme
http://github.com/gcarreno

 

TinyPortal © 2005-2018