hypejunction/mustache

Mustache templating in Elgg

Installs: 29

Dependents: 2

Suggesters: 0

Stars: 0

Watchers: 1

Forks: 0

Open Issues: 0

Language:JavaScript

Type:elgg-plugin

1.0.0 2015-10-02 08:20 UTC

README

Elgg 2.0

Features

  • Templates can be rendered both client and server side
  • Compatible with Elgg's view system
  • AMD-friendly

Usage

  1. Create a new template as an Elgg view
// /my_plugin/views/default/path/to/template.html
<div class="mustache-template">
	<h3>Hello, <b>{{name}}</b></h3>
</div>

You can also use PHP views, e.g. template.html.php. This allows you to call other views and do stuff with PHP. If you use this approach, be sure to register the template views in simplecache, so that they are accessible with AMD.


// /start.php
elgg_register_simplecache_view('path/to/template.html.php');
// /my_plugin/views/default/path/to/template.html.php
<div class="mustache-template">
	<h3>Hello, <b>{{name}}</b></h3>
	<?= elgg_view('path/to/other/template.html') ?>
</div>
  1. Render server-side

	echo mustache()->render(elgg_view('mustache/template.html'), array(
		'name' => elgg_get_logged_in_user_entity()->name,
	));
  1. Render client-side
define(function(require) {

	var elgg = require('elgg');
	var mustache = require('mustache');
	var template = require('text!mustache/template.html');

	var view = mustache.render(template, {
		name: elgg.get_logged_in_user_entity().name
	});

	$('body').html(view);

});

Docs