mw.template.registerCompiler( 'mustache', {
compile: function ( src ) {
return {
- render: function ( data ) {
- return $( $.parseHTML( Mustache.render( src, data ) ) );
+ /**
+ * @ignore
+ * @return {string} The raw source code of the template
+ */
+ getSource: function () {
+ return src;
+ },
+ /**
+ * @ignore
+ * @param {Object} data Data to render
+ * @param {Object} partialTemplates Map partial names to Mustache template objects
+ * returned by mw.template.get()
+ */
+ render: function ( data, partialTemplates ) {
+ var partials = {};
+ if ( partialTemplates ) {
+ $.each( partialTemplates, function ( name, template ) {
+ partials[ name ] = template.getSource();
+ } );
+ }
+ return $( $.parseHTML( Mustache.render( src, data, partials ) ) );
}
};
}
--- /dev/null
+{{#list}}oh no{{/list}}{{#foo}}none of this should render{{/foo}}
\ No newline at end of file
'tests/qunit/suites/resources/mediawiki/mediawiki.RegExp.test.js',
'tests/qunit/suites/resources/mediawiki/mediawiki.storage.test.js',
'tests/qunit/suites/resources/mediawiki/mediawiki.template.test.js',
+ 'tests/qunit/suites/resources/mediawiki/mediawiki.template.mustache.test.js',
'tests/qunit/suites/resources/mediawiki/mediawiki.test.js',
'tests/qunit/suites/resources/mediawiki/mediawiki.html.test.js',
'tests/qunit/suites/resources/mediawiki/mediawiki.Title.test.js',
'mediawiki.toc',
'mediawiki.Uri',
'mediawiki.user',
+ 'mediawiki.template.mustache',
'mediawiki.template',
'mediawiki.util',
'mediawiki.special.recentchanges',
--- /dev/null
+( function ( mw ) {
+
+ QUnit.module( 'mediawiki.template.mustache', {
+ setup: function () {
+ // Stub register some templates
+ this.sandbox.stub( mw.templates, 'get' ).returns( {
+ 'test_greeting.mustache': '<div>{{foo}}{{>suffix}}</div>',
+ 'test_greeting_suffix.mustache': ' goodbye'
+ } );
+ }
+ } );
+
+ QUnit.test( 'render', 2, function ( assert ) {
+ var html, htmlPartial, data, partials,
+ template = mw.template.get( 'stub', 'test_greeting.mustache' ),
+ partial = mw.template.get( 'stub', 'test_greeting_suffix.mustache' );
+
+ data = {
+ foo: 'Hello'
+ };
+ partials = {
+ suffix: partial
+ };
+
+ html = template.render( data ).html();
+ htmlPartial = template.render( data, partials ).html();
+
+ assert.strictEqual( html, 'Hello', 'Render without partial' );
+ assert.strictEqual( htmlPartial, 'Hello goodbye', 'Render with partial' );
+ } );
+
+}( mediaWiki ) );