3 ms·
We have the use case to render the same mustache [1] templates on the server side (PHP) and on the client side (JS). Problem here is that the template helper vi
by marcel-oelke 15y ago
We have the use case to render the same mustache [1] templates on the server side (PHP) and on the client side (JS). Problem here is that the template helper view functions have to be written in two languages, PHP and javascript.
Therefore we started two weeks ago to evaluate javascript rendering of mustache templates from inside PHP. First we tried the spidermonkey extension, but had some issues with it.
Currently we are using v8js on mac and ubuntu. Works like a charm so far, and we have to write template functions only once.
Now we can use the same mustache templates AND helper functions from within PHP and client side javascript. Even compiled hogan [2] templates work. The templates and javascript files are stored in the PHP APC cache to avoid disk seeks.
So far we are very happy with the results. Here is some php pseudo code how it works with mustache:
pseudo-php-example:
\V8Js::registerExtension("mustache", apc_fetch("hogan.js-content"));
$v8 = new \V8Js('ContextName', array(), $extensions);
$v8->view = array("stuff" => "available in the js template");
$v8->view = array("partialName" => apc_fetch("partialTemplateContent"))
$v8->template = "{{stuff}}"
$script .= 'Mustache.render(PHP.template, PHP.view, PHP.partials);';
$renderedTemplate = $v8>executeString($script);
echo $renderedTemplate; // prints available in the js template
I recommend checking the v8js samples [3].
[1] http://mustache.github.com/ http://mustache.github.com/
[2] http://twitter.github.com/hogan.js/ http://twitter.github.com/hogan.js/
[3] http://svn.php.net/viewvc/pecl/v8js/trunk/samples/ http://svn.php.net/viewvc/pecl/v8js/trunk/samples/