Contemplate: Template engine that provides programming controls

Recommend this page to a friend!
  Info   View files Example   Screenshots Screenshots   View files View files (117)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2020-09-15 (2 months ago) RSS 2.0 feedNot yet rated by the usersTotal: 52 This week: 2All time: 9,887 This week: 102Up
Version License PHP version Categories
contemplate 1.0Artistic License5PHP 5, Templates
Description Author

This package implements a template engine that provides programming controls.

It can process a given template string to render it by processing the template tags like a programming language.

The template format may contain placeholder marks that define where values may be inserted from variables or supported functions.

It also supports programming controls like process sections depending on the evaluation of conditions and iteration of sections like loops.

The package also provides the implementation of the same template engine in JavaScript and Python besides PHP.

Picture of Nikos M.
Name: Nikos M. is available for providing paid consulting. Contact Nikos M. .
Classes: 13 packages by
Country: Greece Greece
Age: 44
All time rank: 108610 in Greece Greece
Week rank: 13 Up1 in Greece Greece Up
Innovation award
Innovation award
Nominee: 5x

 

Details

Contemplate

__Light-weight, fast and flexible "object-oriented" template engine for PHP, Python, Node.js, Browser client-side and XPCOM JavaScript__

Contemplate

Contemplate.js, Contemplate.min.js

Meaning of Yoga in Indian Philosophy (wikipedia) ("unifying through yoga, contemplate and attain mastery")

see also:

  • HtmlWidget html widgets used as (template) plugins and/or standalone for PHP, Python, Node.js / Browser / XPCOM Javascript both client and server-side (can be used as plugins for Contemplate)
  • Tao A simple, tiny, isomorphic, precise and fast template engine for handling both string and live dom based templates
  • ModelView a light-weight and flexible MVVM framework for JavaScript/HTML5
  • ModelView MVC jQueryUI Widgets plug-n-play, state-full, full-MVC widgets for jQueryUI using modelview.js (e.g calendars, datepickers, colorpickers, tables/grids, etc..) (in progress)
  • Importer simple class & dependency manager and loader for PHP, Python, Node.js / Browser / XPCOM Javascript
  • PublishSubscribe a simple and flexible publish-subscribe pattern implementation for PHP, Python, Node.js / Browser / XPCOM Javascript
  • Dromeo a flexible, and powerfull agnostic router for PHP, Python, Node.js / Browser / XPCOM Javascript
  • Dialect a simple cross-vendor & cross-platform SQL construction for PHP, Python, Node.js / Browser / XPCOM Javascript
  • Xpresion a simple and flexible eXpression parser engine (with custom functions and variables support) for PHP, Python, Node.js / Browser / XPCOM Javascript
  • GrammarTemplate versatile and intuitive grammar-based templating for PHP, Python, Node.js / Browser / XPCOM Javascript
  • GrammarPattern versatile grammar-based pattern-matching for Node / XPCOM / JS (IN PROGRESS)
  • Regex Analyzer/Composer Regular Expression Analyzer and Composer for PHP, Python, Node.js / Browser / XPCOM Javascript
  • DateX eXtended & localised Date parsing, diffing, formatting and validation for PHP, Python, Node.js / Browser / XPCOM Javascript
  • Abacus a fast combinatorics and computation library for PHP, Python, Node.js / Browser / XPCOM Javascript
  • RT client-side real-time communication for Node/XPCOM/JS with support for Poll / BOSH / WebSockets
  • Asynchronous a simple manager for async, linearised, parallelised, interleaved and sequential tasks for JavaScript

This started as a a __proof-of-concept__ but developed into a __full-blown and versatile__ template engine.

The original inspiration came from an old post by John Resig (http://ejohn.org/blog/javascript-micro-templating/)

Note There are a couple of other frameworks named also contemplate

  • http://www.arlomedia.com/software/contemplate/assembled/introduction.html
  • 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.

Contemplate

Contents

If you use Contemplate in your application and you want to share it, feel free to submit an example link

Rationale

There are many templating engines out there, which are elegant, fast, multipurpose 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:

  1. but can have performance issues sometimes
  2. 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:

  1. It works only with `PHP`, and many times the same template needs to be used also by `JavaScript`
  2. It can be cumbersome to combine or iterate over templates and parts.

Contemplate seeks to find the best balance between these requirements.

The solution is inspired by _John Resig's post_ (see above) and the fact that PHP, Python and JavaScript share a __common language subset__.

Features:

  • `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(s).
  • 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.
  • __Uniform functionality__, Engine Implementations for __PHP__ , __Python__ , __Node.js__ , __XPCOM__ and __Browser client-side JavaScript__ (NOTE `javascript` engine supports both sync and async operations both callback-based and promise-based)
  • Simple and __light-weight__ ( only one relatively small class for each implementation, no other dependencies ) `~50kB` minified, `~16kB` zipped
  • __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 (note: `javascript` cached templates are UMD modules which can be used in both `node.js`/`AMD`/`XPCOM`/`browser`/`es6 module fallback`)
  • 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__
  • __Object-oriented__ , templates implement inheritance and polymorphism in a full object-oriented manner (see below)
  • Supports multiple __dynamic contexts__ , and __contextual settings__ so that different modules of an application can use the engine independantly (see examples and manual)
  • __Localization__ , __Pluralisation__ , __Date formatting__ built-in and configurable easily ( simple __Data escaping__ is also supported)
  • `X-GetText` / `POEdit` translation-friendly localisation (keywords: `locale`, `xlocale:1,2c`, `nlocale:2`, `nlocale:3`, `nxlocale:2,4c`, `nxlocale:3,4c`)
  • __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 `tpl` function, these templates are called as templates subroutines and parsed by themselves
  • Templates and template functions can also have inline templates as parameters via `inline` template function
  • __Template Inheritance__ , templates can extend/inherit other templates using `extends` directive and override blocks using `block` , `endblock` directives (see examples)
  • __Direct Super reference__ , templates can use the `super` template function to directly reference (and call) a super block if needed in OO manner (see examples)
  • __Nested Blocks__ , template blocks can be nested and repeated in multiple ways (see examples)
  • __Custom Plugins__ , can be used as template functions to enhance/extend the engine functionality (see examples)
  • custom plugins can be also inlined, i.e their code can be expanded at compile-time using `Contemplate::inline` templates in their definition, e.g saving unnecessary look-ups at render-time (see examples)

Dependencies

  • 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 browsers
  • `Contemplate` is also a `XPCOM JavaScript Component` (Firefox) (e.g to be used in firefox browser addons/plugins for templating)

Todo

  • add support for multiple `contexts` (which include separate `templates`, `cache` directories and related parameters) so that the engine can be used in same application by different modules independantly [DONE]
  • simplify template `directives`, `functions` and `plugins` notation by eliminating the `%` prefix (compatibility mode to older versions also supported) [DONE]
  • support asynchronous template loading/parsing/rendering/writing for `node/browser` [DONE]
  • add `Contemplate` implementations for `ActionScript, Perl, Java, Scala` [TODO?]
  • transform `Contemplate` (for `PHP`) into a `PHP` `C`-extension, `Contemplate` (for node) into standalone executable (eg. https://github.com/crcn/nexe) [TODO?]
  • keep-up with `php`, `node`, `python`, browsers updates

Performance

(for php see /tests/perf/perf.php)

Note: The engines included in the (following) tests, have different philosophy and in general provide different features. These are only illustrative modulo all the other features.

Render Time

The following tests were made on a revision of a 2013 jsperf test for resig micro-templating, handlebars, contemplate, mustache, underscore, doT and kendoui template engines. More tests should be done.

Contemplate (0.6.5) was 2nd place on Firefox and 3rd (or close) place on Opera, IE, while Contemplate was average to slower on Chrome. The reason was mostly that Contemplate was using a code to copy/isolate the input data every time inside the render function, which most of the time is redundant, else user can use the Contemplate.data method to create a shallow copy suitable to be used as render data. So this was removed, plus some minor refactoring and minor loop optimisation.

This resulted in great performance increase as shown below. (see changelog)

Previous tests are here jsperf/0.6.5, jsperf/0.6.7, jsperf/0.7, jsperf/0.7.1, jsperf/0.8, jsperf/0.8.1, jsperf/1.0.0, jsperf/1.0.0 (1.0.0+ format)

Contemplate (1.0.0) is (consistently) near 1st place on all browsers.

contemplate rendering jsperf

Parse / Compilation Time

The following tests involve swig, handlebars, contemplate and mustache template engines. More tests should be done.

Previous tests are here jsperf/0.6.7, jsperf/0.7, jsperf/0.7.1, jsperf/0.8, jsperf/0.8.1, jsperf/1.0.0

Contemplate (1.0.0) is (consistently) 1st place on all browsers.

contemplate parse jsperf

Tests

Use test.php (for php), test.js (for node), test.py (for python) under /tests folder, to test the basic functionality

Screenshots

Sample Template markup Template markup

Data to be used for the template Template data

PHP and JavaScript rendering of the template on same page (see test.php) Template output

Screenshots  
  • screenshots/template_markup.png
  • screenshots/template_output.png
  • screenshots/template_data.png
  • screenshots/jsperf-rendering.png
  • screenshots/contemplate.jpg
  • screenshots/jsperf-compilation.png
  • screenshots/contemplate-interactive.png
  Files folder image Files  
File Role Description
Files folder imagesrc (7 directories)
Files folder imagetests (3 files, 10 directories)
Accessible without login Plain text file changelog.md Data Auxiliary data
Accessible without login Plain text file manual.md Doc. Documentation manual
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files  /  src  
File Role Description
Files folder imageactionscript (1 file)
Files folder imagegrammar (1 file)
Files folder imagejava (1 file)
Files folder imagejs (2 files, 1 directory)
Files folder imageperl (1 file)
Files folder imagephp (1 file, 1 directory)
Files folder imagepython (2 files, 1 directory)

  Files folder image Files  /  src  /  actionscript  
File Role Description
  Accessible without login Plain text file todo.txt Doc. Documentation

  Files folder image Files  /  src  /  grammar  
File Role Description
  Accessible without login Plain text file contemplate.js Data Auxiliary data

  Files folder image Files  /  src  /  java  
File Role Description
  Accessible without login Plain text file todo.txt Doc. Documentation

  Files folder image Files  /  src  /  js  
File Role Description
Files folder imageplugins (1 file)
  Accessible without login Plain text file Contemplate.js Data Auxiliary data
  Accessible without login Plain text file Contemplate.min.js Data Auxiliary data

  Files folder image Files  /  src  /  js  /  plugins  
File Role Description
  Accessible without login Plain text file plugins.txt Doc. Documentation

  Files folder image Files  /  src  /  perl  
File Role Description
  Accessible without login Plain text file todo.txt Doc. Documentation

  Files folder image Files  /  src  /  php  
File Role Description
Files folder imageplugins (1 file)
  Plain text file Contemplate.php Class Class source

  Files folder image Files  /  src  /  php  /  plugins  
File Role Description
  Accessible without login Plain text file plugins.txt Doc. Documentation

  Files folder image Files  /  src  /  python  
File Role Description
Files folder imageplugins (2 files)
  Accessible without login Plain text file Contemplate.py Data Auxiliary data
  Accessible without login Plain text file __init__.py Data Auxiliary data

  Files folder image Files  /  src  /  python  /  plugins  
File Role Description
  Accessible without login Plain text file plugins.txt Doc. Documentation
  Accessible without login Plain text file __init__.py Data Auxiliary data

  Files folder image Files  /  tests  
File Role Description
Files folder imageasync (1 file, 2 directories)
Files folder imagectx (29 files)
Files folder imagefolders (4 files, 2 directories)
Files folder imagefuncs (3 files)
Files folder imagejs (1 file)
Files folder imageoo (19 files)
Files folder imageperf (4 files)
Files folder imagevars (8 files)
Files folder image_tplcache (13 files)
Files folder image_tpls (5 files)
  Accessible without login Plain text file test.js Data Auxiliary data
  Accessible without login Plain text file test.php Example Example script
  Accessible without login Plain text file test.py Data Auxiliary data

  Files folder image Files  /  tests  /  async  
File Role Description
Files folder image_tplcache (1 file, 2 directories)
Files folder image_tpls (1 file, 3 directories)
  Accessible without login Plain text file test.js Data Auxiliary data

  Files folder image Files  /  tests  /  async  /  _tplcache  
File Role Description
Files folder imagecommon (2 files)
Files folder imagelayout (1 file)
  Accessible without login Plain text file demo_tpl_html_tpl__global.js Data Auxiliary data

  Files folder image Files  /  tests  /  async  /  _tplcache  /  common  
File Role Description
  Accessible without login Plain text file bar_tpl_html_tpl__global.js Data Auxiliary data
  Accessible without login Plain text file foo_tpl_html_tpl__global.js Data Auxiliary data

  Files folder image Files  /  tests  /  async  /  _tplcache  /  layout  
File Role Description
  Accessible without login Plain text file base_tpl_html_tpl__global.js Data Auxiliary data

  Files folder image Files  /  tests  /  async  /  _tpls  
File Role Description
Files folder imagecommon (2 files)
Files folder imageinclude (1 file)
Files folder imagelayout (1 file)
  Accessible without login HTML file demo.tpl.html Doc. Documentation

  Files folder image Files  /  tests  /  async  /  _tpls  /  common  
File Role Description
  Accessible without login HTML file bar.tpl.html Doc. Documentation
  Accessible without login HTML file foo.tpl.html Doc. Documentation

  Files folder image Files  /  tests  /  async  /  _tpls  /  include  
File Role Description
  Accessible without login HTML file sub.tpl.html Doc. Documentation

  Files folder image Files  /  tests  /  async  /  _tpls  /  layout  
File Role Description
  Accessible without login HTML file base.tpl.html Doc. Documentation

  Files folder image Files  /  tests  /  ctx  
File Role Description
  Accessible without login HTML file global.html Doc. Documentation
  Accessible without login Plain text file global_tpl__ctx1.js Data Auxiliary data
  Plain text file global_tpl__ctx1.php Class Class source
  Accessible without login Plain text file global_tpl__ctx1.py Data Auxiliary data
  Accessible without login Plain text file global_tpl__ctx2.js Data Auxiliary data
  Plain text file global_tpl__ctx2.php Class Class source
  Accessible without login Plain text file global_tpl__ctx2.py Data Auxiliary data
  Accessible without login Plain text file global_tpl__ctx3.js Data Auxiliary data
  Plain text file global_tpl__ctx3.php Class Class source
  Accessible without login Plain text file global_tpl__ctx3.py Data Auxiliary data
  Accessible without login Plain text file out-js Data Auxiliary data
  Accessible without login Plain text file out-php Data Auxiliary data
  Accessible without login Plain text file out-py Data Auxiliary data
  Accessible without login Plain text file test.js Data Auxiliary data
  Accessible without login Plain text file test.php Aux. Auxiliary script
  Accessible without login Plain text file test.py Data Auxiliary data
  Accessible without login HTML file tpl1.html Doc. Documentation
  Accessible without login HTML file tpl2.html Doc. Documentation
  Accessible without login HTML file tpl3.html Doc. Documentation
  Accessible without login Plain text file tpl_tpl__ctx1.js Data Auxiliary data
  Plain text file tpl_tpl__ctx1.php Class Class source
  Accessible without login Plain text file tpl_tpl__ctx1.py Data Auxiliary data
  Accessible without login Plain text file tpl_tpl__ctx2.js Data Auxiliary data
  Plain text file tpl_tpl__ctx2.php Class Class source
  Accessible without login Plain text file tpl_tpl__ctx2.py Data Auxiliary data
  Accessible without login Plain text file tpl_tpl__ctx3.js Data Auxiliary data
  Plain text file tpl_tpl__ctx3.php Class Class source
  Accessible without login Plain text file tpl_tpl__ctx3.py Data Auxiliary data
  Accessible without login Plain text file __init__.py Data Auxiliary data

  Files folder image Files  /  tests  /  folders  
File Role Description
Files folder imagefolder1 (1 directory)
Files folder imagefolder2 (1 file)
  Accessible without login Plain text file test.js Data Auxiliary data
  Accessible without login Plain text file test.php Aux. Auxiliary script
  Accessible without login Plain text file test.py Data Auxiliary data
  Accessible without login HTML file tpl.html Doc. Documentation

  Files folder image Files  /  tests  /  folders  /  folder1  
File Role Description
Files folder imagesubfolder1 (1 file)

  Files folder image Files  /  tests  /  folders  /  folder1  /  subfolder1  
File Role Description
  Accessible without login HTML file tpl1.html Doc. Documentation

  Files folder image Files  /  tests  /  folders  /  folder2  
File Role Description
  Accessible without login HTML file tpl2.html Doc. Documentation

  Files folder image Files  /  tests  /  funcs  
File Role Description
  Accessible without login Plain text file test.js Data Auxiliary data
  Accessible without login Plain text file test.php Aux. Auxiliary script
  Accessible without login Plain text file test.py Data Auxiliary data

  Files folder image Files  /  tests  /  js  
File Role Description
  Accessible without login Plain text file Contemplate.min.js Data Auxiliary data

  Files folder image Files  /  tests  /  oo  
File Role Description
  Accessible without login Plain text file out-js Data Auxiliary data
  Accessible without login Plain text file out-php Data Auxiliary data
  Accessible without login Plain text file out-py Data Auxiliary data
  Accessible without login Plain text file test.js Data Auxiliary data
  Accessible without login Plain text file test.php Aux. Auxiliary script
  Accessible without login Plain text file test.py Data Auxiliary data
  Accessible without login HTML file tpl1.html Doc. Documentation
  Accessible without login Plain text file tpl1_tpl__global.js Data Auxiliary data
  Plain text file tpl1_tpl__global.php Class Class source
  Accessible without login Plain text file tpl1_tpl__global.py Data Auxiliary data
  Accessible without login HTML file tpl2.html Doc. Documentation
  Accessible without login Plain text file tpl2_tpl__global.js Data Auxiliary data
  Plain text file tpl2_tpl__global.php Class Class source
  Accessible without login Plain text file tpl2_tpl__global.py Data Auxiliary data
  Accessible without login HTML file tpl3.html Doc. Documentation
  Accessible without login Plain text file tpl3_tpl__global.js Data Auxiliary data
  Plain text file tpl3_tpl__global.php Class Class source
  Accessible without login Plain text file tpl3_tpl__global.py Data Auxiliary data
  Accessible without login Plain text file __init__.py Data Auxiliary data

  Files folder image Files  /  tests  /  perf  
File Role Description
  Accessible without login HTML file contemplate.tpl.html Doc. Documentation
  Accessible without login Plain text file out-perf Data Auxiliary data
  Accessible without login Plain text file perf.php Aux. Auxiliary script
  Accessible without login Plain text file php.tpl.php Aux. Auxiliary script

  Files folder image Files  /  tests  /  vars  
File Role Description
  Accessible without login Plain text file test.js Data Auxiliary data
  Plain text file test.php Class Class source
  Accessible without login Plain text file test.py Data Auxiliary data
  Accessible without login HTML file test.tpl.html Doc. Documentation
  Accessible without login Plain text file test_tpl__global.js Data Auxiliary data
  Plain text file test_tpl__global.php Class Class source
  Accessible without login Plain text file test_tpl__global.py Data Auxiliary data
  Accessible without login Plain text file __init__.py Data Auxiliary data

  Files folder image Files  /  tests  /  _tplcache  
File Role Description
  Accessible without login Plain text file base_tpl__global.js Data Auxiliary data
  Plain text file base_tpl__global.php Class Class source
  Accessible without login Plain text file base_tpl__global.py Data Auxiliary data
  Accessible without login Plain text file demo_tpl__global.js Data Auxiliary data
  Plain text file demo_tpl__global.php Class Class source
  Accessible without login Plain text file demo_tpl__global.py Data Auxiliary data
  Accessible without login Plain text file main_tpl__global.js Data Auxiliary data
  Plain text file main_tpl__global.php Class Class source
  Accessible without login Plain text file main_tpl__global.py Data Auxiliary data
  Accessible without login Plain text file sub_tpl__global.js Data Auxiliary data
  Plain text file sub_tpl__global.php Class Class source
  Accessible without login Plain text file sub_tpl__global.py Data Auxiliary data
  Accessible without login Plain text file __init__.py Data Auxiliary data

  Files folder image Files  /  tests  /  _tpls  
File Role Description
  Accessible without login HTML file base.tpl.html Doc. Documentation
  Accessible without login HTML file date.tpl.html Doc. Documentation
  Accessible without login HTML file demo.tpl.html Doc. Documentation
  Accessible without login HTML file main.tpl.html Doc. Documentation
  Accessible without login HTML file sub.tpl.html Doc. Documentation

 Version Control Unique User Downloads Download Rankings  
 100%
Total:52
This week:2
All time:9,887
This week:102Up

For more information send a message to info at phpclasses dot org.