Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

https://github.com/synopse/dmustache

A Delphi implementation of the Mustache templating language
https://github.com/synopse/dmustache

Last synced: about 1 month ago
JSON representation

A Delphi implementation of the Mustache templating language

Lists

README

        

`SynMustache` is a Delphi/FPC implementation of the [Mustache template language](http://mustache.github.io/).

Presentation
============

* SynMustache is the first Delphi implementation of Mustache, supporting Delphi 6 up to latest Delphi, and FPC/Lazarus;
* It has a separate parser and renderer (so you can compile your templates ahead of time);
* The parser features a shared cache of compiled templates;
* It [passes all official Mustache specification tests](https://github.com/mustache/spec) - including all weird whitespace process;
* External partials can be supplied as `TSynMustachePartials` dictionaries;
* `{{.}}`, `{{-index}}` and `{{"some text}}` pseudo-variables were added to the standard Mustache syntax;
* `{{#-first}}`, `{{#-last}}` and `{{#-odd}}` pseudo-sections were added to the standard Mustache syntax;
* `{{helperName value}}` *Expression Helpers* were added to the standard Mustache syntax;
* `{{if value<=>value}}` *Expression Helper* for conditional sections;
* Internal partials can be defined via `{{partial}}'#$A'3');
html := mustache.RenderJSON('{}',TSynMustachePartials.CreateOwned(['partial','1'#$A'2']));
// now html='1'#$A'23','external partials'

Here `TSynMustachePartials.CreateOwned()` expects the partials to be supplied as name/value pairs.

Internal partials (one of the SynMustache extensions), can be defined directly in the main template:

mustache := TSynMustache.Parse('{{partial}}4');
html := mustache.RenderJSON('{name:3}');
// now html='1'#$A'234','internal partials'

Internationalization
--------------------

You can define `{{"some text}}` pseudo-variables in your templates, which text will be supplied to a callback, ready to be transformed on the fly: it may be convenient for i18n of web applications.

By default, the text will be written directly to the output buffer, but you can define a callback which may be used e.g. for text translation:

procedure TTestLowLevelTypes.MustacheTranslate(var English: string);
begin
if English='Hello' then
English := 'Bonjour' else
if English='You have just won' then
English := 'Vous venez de gagner';
end;

Of course, in a real application, you may assign one `TLanguageFile.Translate(var English: string)` method, as defined in the `mORMoti18n.pas` unit.

Then, you will be able to define your template as such:

mustache := TSynMustache.Parse(
'{{"Hello}} {{name}}'#13#10'{{"You have just won}} {{value}} {{"dollars}}!');
html := mustache.RenderJSON('{name:?,value:?}',[],['Chris',10000],nil,MustacheTranslate);
// now html='Bonjour Chris'#$D#$A'Vous venez de gagner 10000 dollars!'

All text has indeed been translated as expected.

Some Links
==========

We wrote a series of blog articles, about Mustache in general, and `SynMustache` unit in particular:

* [Mustache Logic-less templates for Delphi - part 1: general presentation of Mustache](https://blog.synopse.info/?post/2014/04/28/Mustache-Logic-less-templates-for-Delphi-part-1);
* [Mustache Logic-less templates for Delphi - part 2: the Mustache syntax](https://blog.synopse.info/?post/2014/04/28/Mustache-Logic-less-templates-for-Delphi-part-2);
* [Mustache Logic-less templates for Delphi - part 3: SynMustache implementation](https://blog.synopse.info/?post/2014/04/28/Mustache-Logic-less-templates-for-Delphi-part-3).

You can use also [Synopse forums](http://synopse.info/forum/viewtopic.php?id=1720) to obtain direct support from the developpers, or send your feedback.

The documentation is [available as a single pdf file](http://blog.synopse.info/public/Documents/SynMustache.pdf), if needed. Note that this `pdf` can be outdated, so you should better consult the "Mustache" part of the *mORMot* SAD pdf, which should be more accurate.

*The Synopse team*