Note There are a couple of other frameworks named also contemplate
- There is an older and quite different template engine for node named also "contemplate" here and here
This repository and project is completely unrelated to these frameworks.
(it seems the word contemplate is nice for a template engine :) )
- Online Example
- Keywords Reference
There are many templating engines out there, which are elegant, fast, multipurpose (eg. smarty mustache twig handlebars jade doT and so on..)
Most of the sophisticated engines use a custom parser (and usually a full-fledged framework) to build the engine.
This is highly versatile:
but can have performance issues sometimes
and / or requires to learn a (completely) new syntax for building a template.
These drawbacks can be compensated if one uses PHP itself as templating engine. PHP already IS a templating language and a very fast at it.
This can create very simple, intuitive and fast templates.
The drawbacks of this approach are:
It can be cumbersome to combine or iterate over templates and parts.
Contemplate seeks to find the best balance between these requirements.
Contemplate does a minimum parsing (and caching) in order to create dynamic templates and trying to contain the needed functionality inside the common language subset.
Most of the time this can be accomplished, the rest functionality is built with custom functions which mostly resemble the PHP syntax, yet work the same in all the engine's implementations.
Simple and light-weight ( only one (relatively small) class for each implementation, no other dependencies )
Fast , can cache templates dynamically (filesystem caching has 3 modes, NONE which uses only in-memory caching, NOUPDATE which caches the templates only once and AUTOUPDATE which re-creates the cached template if original template has changed, useful for debugging)
Generated cached template code is formatted and annotated with comments, for easy debugging
Syntax close to PHP (there was an effort to keep the engine syntax as close to PHP syntax as possible, to avoid learning another language syntax)
Easily extensible , configurable
Localization , Date formatting built-in and configurable easily ( simple Data escaping is also supported)
Date manipulation similar to PHP format (ie date function). An extended, localized version of php's date function ldate is also implemented in the framework
Loops can have optional elsefor() statement when no data, or data is empty (see tests)
Templates can include other templates (similar to PHP include directive), these includes wil be compiled into the the template that called them
Templates can call another template using template directive, these templates are called as templates subroutines and parsed by themselves
Template Inheritance , templates can extend/inherit other templates using extends directive and override blocks using block , endblock directives (see examples)
- Only 3 classes are used (Contemplate.php, Contemplate.js, Contemplate.py), no other dependencies
- PHP 5.2+ supported
- Node 0.8+ supported
- Python 2.x or 3.x supported
- all major browsers
- allow the engine to be extended by (custom) plugins
- add Contemplate implementations for Perl, Java, Scala
- transform Contemplate (for PHP) into a PHP C-extension, Contemplate (for node) into standalone executable (eg. https://github.com/crcn/nexe)
- keep-up with php, node, browsers, python updates
Use test.php (for php), test.js (for node), test.py (for python) under tests folder, to test the basic functionality