Skip to content

CHH/MustacheJsCompiler

master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
 
 
 
 
 
 
 
 
 
 

MustacheJsCompiler

Compiles server-side Mustache templates to self-sufficient client-side JS functions. Uses the excellent PhlyMustache.

Install

composer require 'chh/mustache-js-compiler':~1.0@dev

Usage

The compiler needs an instance of Phly\Mustache\Mustache to function.

If you intend to share templates between the server and the client, than it's recommended to use the same Mustache instance which your application uses so the template paths are setup the same way (for partials to be compiled correctly).

<?php

$mustache = new Phly\Mustache\Mustache;
// or use the same Mustache environment than your application:
$mustache = $app['mustache'];

Then create the compiler and pass it the Mustache environment:

$jsCompiler = new CHH\MustacheJsCompiler\MustacheJsCompiler($mustache);

The compiler has a compile method which works the same way as $mustache->tokenize(). It looks up the template name in the template path, or uses the passed Mustache code. The compile method returns a self-sufficient JavaScript function which executes the template.

The template looks like this:

{{! user/show.mustache }}
Hi {{name}}!

We can render the template using this code:

<div id="user-widget"></div>
<script>
  (function() {
    var template = <?php echo $jsCompiler->compile('user/show') ?>;
    var widget = document.getElementById('user-widget');
    widget.innerHTML = template({name: "Christoph"});
  })();
</script>

Unimplemented Mustache Features

  • Filters

About

Compiles server-side Mustache templates to self-sufficient client-side JS functions

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages