Support Mustache partials in Mustache template module

* Add template partial support which matches the server-side implementation
  and means that we have full mustache support.

https://mustache.github.io/mustache.5.html#Partials

Bug: T97188
Change-Id: Ic752f52669dbffa21c4a514509c3ea1da8ac5d9c
This commit is contained in:
jdlrobson 2015-04-24 17:12:46 -07:00 committed by Timo Tijhof
parent e144319440
commit 2b7d281f06
4 changed files with 56 additions and 2 deletions

View file

@ -4,8 +4,27 @@
mw.template.registerCompiler( 'mustache', { mw.template.registerCompiler( 'mustache', {
compile: function ( src ) { compile: function ( src ) {
return { 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 ) ) );
} }
}; };
} }

View file

@ -0,0 +1 @@
{{#list}}oh no{{/list}}{{#foo}}none of this should render{{/foo}}

View file

@ -71,6 +71,7 @@ return array(
'tests/qunit/suites/resources/mediawiki/mediawiki.RegExp.test.js', 'tests/qunit/suites/resources/mediawiki/mediawiki.RegExp.test.js',
'tests/qunit/suites/resources/mediawiki/mediawiki.storage.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.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.test.js',
'tests/qunit/suites/resources/mediawiki/mediawiki.html.test.js', 'tests/qunit/suites/resources/mediawiki/mediawiki.html.test.js',
'tests/qunit/suites/resources/mediawiki/mediawiki.Title.test.js', 'tests/qunit/suites/resources/mediawiki/mediawiki.Title.test.js',
@ -126,6 +127,7 @@ return array(
'mediawiki.toc', 'mediawiki.toc',
'mediawiki.Uri', 'mediawiki.Uri',
'mediawiki.user', 'mediawiki.user',
'mediawiki.template.mustache',
'mediawiki.template', 'mediawiki.template',
'mediawiki.util', 'mediawiki.util',
'mediawiki.special.recentchanges', 'mediawiki.special.recentchanges',

View file

@ -0,0 +1,32 @@
( 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 ) );