5

For web applications, it would be helpful if Delphi object properties and methods could be "connected" with HTML code.

This could be used in many ways, both during the HTML response build stage and the request processing stage:

  • access a server-side object property value to output it in the HTML code
  • bind a server-side object property to a HTML form input field, so that the web application sends the form HTML with its current value, and set the property to the entered value when the client submits the form
  • bind a HTML form button to a server-side object method, which will be executed when the client submits the form

I have not found a native Delphi library which would make it easier to add such HTML to Delphi object binding.

My question:

Is there a template engine for scripted code generation like Apache Velocity or Freemarker (see examples below) which I can use in Delphi applications? (not in the IDE - I am not lookig for a OTA based solution, or Delphi code templates)

The template engine would bind Delphi objects to a template and replace the script variables, and execute iterations / conditions based on values in the objects. Templates can be nested (call other templates).

A typical use case would be the dynamic generation of HTML code, but also work for Delphi code generators.

Velocity example:

<HTML>
<BODY>
Hello $customer.Name!
<table>
#foreach( $mud in $mudsOnSpecial )
   #if ( $customer.hasPurchased($mud) )
      <tr>
        <td>
          $flogger.getPromo( $mud )
        </td>
      </tr>
   #end
#end
</table>

Freemarker example:

<html>
<head>
  <title>Welcome!</title>
</head>
<body>
  <h1>Welcome ${user}!</h1>
  <p>Our latest product:
  <a href="${latestProduct.url}">${latestProduct.name}</a>!
</body>
</html>  

Other Template Engines which are similar to what I am looking for are Visual Studio's T4 Code Generation and Smarty.

mjn
  • 36,362
  • 28
  • 176
  • 378
  • 2
    You can take a look at our [Mustache logic-less template system](https://forums.embarcadero.com/thread.jspa?messageID=650500), available with our other [Open Source libraries](http://blog.synopse.info/post/2014/04/28/Mustache-Logic-less-templates-for-Delphi-part-1) (part of [mORMot](http://mormot.net), but could be used without it). [Mustache](http://mustache.github.io) is a great template engine. – Arnaud Bouchez Apr 28 '14 at 14:32
  • you may be interested in https://stackoverflow.com/a/71780444/5781320 –  Apr 07 '22 at 10:31

2 Answers2

2

Please have a look here: http://dvdchief.com/delphi/ This is a free template engine for delphi like smarty for php.

Mahdi
  • 247
  • 2
  • 6
1

Delphi on Rails, hosted at google code, may have something that you can use. http://code.google.com/p/delphionrails/wiki/Getting_Started

Vegar
  • 12,828
  • 16
  • 85
  • 151
  • Thanks for the link! Lua is a scripting engine, how would I use it to create and run something like the example HTML templates in my question? – mjn Oct 07 '10 at 05:41
  • I have not used it my self, but the examples showed what I thought you where after. All code is available from goole code, so you should download and start digging to see if it is possible to reuse some of it or learn something to make your own solution. – Vegar Oct 07 '10 at 13:11