32

I'm looking for a simple Template engine that works both on client side with JS ánd on server side with PHP.

That means I want to be able to use the same template definition for both use cases.

Do you know any templating engines that have official implementations in both JS and PHP?

John Slegers
  • 45,213
  • 22
  • 199
  • 169
Florian
  • 3,145
  • 1
  • 27
  • 38
  • 1
    I'm asking myself same question nowadays, and looks to me more about existence of working implementations and their status than a recommendation, the problem is obvious, edit templates only once, should be a common problem so why there are so few (or not production ready) solutions? what are best practices to share templates? or if it is a evil thing, why? – Alex Sep 21 '14 at 18:31
  • 1
    See [Contemplate engine](https://github.com/foo123/Contemplate) which was specificaly designed to be isomorphic and be used in js/node/php/python. It is also both extremely fast and extremely flexible supporting template inherittance and many features (similar to twig/swig) (ps I am the author) – Nikos M. Nov 06 '18 at 13:35

4 Answers4

30

If you like JS->PHP priority :) then you have this two jquery-tmpl compatible template renderer for PHP backend

  1. https://github.com/abackstrom/jquery-tmpl-php
  2. https://github.com/xyu/jquery-tmpl-php

If you prefer more PHP->JS priority :) then you can try this Javascript implementation of popular PHP templating Smarty

  1. http://code.google.com/p/jsmart/

Or you can try something more neutral like:

  1. Mustache http://mustache.github.com/
mPrinC
  • 9,147
  • 2
  • 32
  • 31
17

You have a new option to compile templates for both PHP and JS using Twig + Twig.js

GromNaN
  • 582
  • 5
  • 7
16

Do you mean something like mustache?

robertc
  • 74,533
  • 18
  • 193
  • 177
  • That's it, i saw this some days ago, but i neither remembered where i saw it nor what it was called :) Thanks! (I have to wait 4 more minutes to accept your answer). – Florian May 14 '11 at 18:55
  • 2
    Depending on your needs, Mustache may be too simple - if you're hoping to e.g. `json_encode()` your view-models and render your views both client- and server-side, you may find that simple things like formatting numbers or dates still requires you to write both client and server functions, whereas Twig and Twig.js may provide a lot more of that stuff out of the box. – mindplay.dk Nov 21 '13 at 13:50
6

Use Mustache!

AFAIK, Mustache is the only template engine that has official implementations in both PHP and JS... along with two dozen programming/scripting languages.

  • Twig does have a JS version, but that's incomplete and AFAIK unofficial.

  • Smarty does have a JS version, but it's also an unofficial port... and Smarty is getting a bit outdated.

John Slegers
  • 45,213
  • 22
  • 199
  • 169