Mustache is a Logic-Less Templates by Chris Wanstrath. Mustache can be used for HTML, config files, source code – anything. It is called as “logic-less” because there there are no if statements, else clauses, or for loops. Instead there are only tags. Some tags are replaced with a value, some nothing, and others a series of values. It works by expanding tags in a template using values provided in a hash or object. DMustache is a Delphi implementation of the Mustache templating language by Arnaud Bouchez and others.
Features :
- 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 – including all weird whitespace process;
- It allows the data context to be supplied as JSON or our TDocVariant custom variant type;
- Almost no memory allocation is performed during the rendering;
- It is natively UTF-8, from the ground up, with optimized conversion of any string data;
- Performance has been tuned, with benefit from SynCommons optimized code;
- Each parsed template is thread-safe and re-entrant;
- It follows the SOLID Open/Close principle so that any aspect of the process can be customized and extended (e.g. for any kind of data context);
- It is perfectly integrated with the other bricks of our mORMot framework, ready to implement dynamic web sites with true MVC design, and full separation of concerns in the views written in Mustache, the controllers being e.g. interface-based services;
Version Supported : Delphi 6 up to Delphi 10.3.
License : This library is part of the Open Source mORMot framework, so is released under the same disjunctive tri-license giving you the choice of one of the three following sets of free software/open source licensing terms:
- Mozilla Public License, version 1.1 or later;
- GNU General Public License, version 2.0 or later;
- GNU Lesser General Public License, version 2.1 or later.
Sample Code :
- Create a Windows VCL Application. Add the Downloaded Synopse/Dmustache source from Github and add to the project.
- Include SynCommons, SynMustache in your Unit uses clause.
- Declare variables of TMustache and Variant to Parse and Render. See the Sample Below for a simple Tag type Name and Value.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
procedure TForm1.Button1Click(Sender: TObject); var mustache: TSynMustache; doc: variant; begin TDocVariant.New(doc); doc.name := 'Chris'; doc.value := 10000; mustache := TSynMustache.Parse( 'Hello {{name}}'#13#10'You have just won {{value}} dollars!'); ShowMessage(mustache.Render(doc)); // now html='Hello Chris'#13#10'You have just won 10000 dollars!' end; |
To know more about Sections, Partials and Internationalization Check out the full source code and documentation of DMustache here.
Design. Code. Compile. Deploy.
Start Free Trial Upgrade Today
Free Delphi Community Edition Free C++Builder Community Edition