Git Product home page Git Product logo

contemplate's Introduction

Contemplate

Light-weight, fast and flexible template engine for PHP, Python, Node and client-side JavaScript, ActionScript(TODO)

Contemplate

Contemplate.js, Contemplate.min.js

see also:

  • 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)
  • Dromeo a flexible, agnostic router for Node/JS, PHP, Python, ActionScript
  • PublishSubscribe a flexible Publish-Subscribe implementation for Node/JS, PHP, Python, ActionScript
  • Xpresion a flexible eXpression parser for Node/JS, PHP, Python, ActionScript

This started as a a proof-of-concept . The 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

This repository and project is completely unrelated to these frameworks.

(it seems the word contemplate is nice for a template engine :) )


Contemplate

###Contents

###Rationale

There are many templating engines out there, which are elegant, fast, multipurpose (eg. smarty mustache twig swig 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:

  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.

  • Engine Implementations for PHP , Python , Node and client-side JavaScript

  • Simple and light-weight ( only one relatively small class for each implementation, no other dependencies ) ~28kB minified, ~11kB 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

  • 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 , Pluralisation , 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

  • 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)

  • Nested Blocks , template blocks can be nested and repeated in multiple ways

  • Custom Plugins , can be used as template functions to enhance/extend the engine functionality

###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 major browsers

###Todo

  • support asynchronous template loading/rendering for node/browser
  • 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

###Performance

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 various javascript template engines. More tests should be done.

Contemplate engine (JavaScript, 0.6.5) jsperf performance tests are here

Contemplate 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. Contemplate engine (JavaScript, 0.6.7) new jsperf tests are here (see changelog for 0.6.6, 0.6.7)

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

contemplate rendering jsperf

Parse / Compilation Time

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

Contemplate engine (JavaScript, 0.6.7, parse method made public) is (consistently) 2nd place (near 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

URL Nikos Web Development
URL WorkingClassCode

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.