Support Mustache partials in Mustache template module
authorjdlrobson <jdlrobson@gmail.com>
Sat, 25 Apr 2015 00:12:46 +0000 (17:12 -0700)
committerTimo Tijhof <krinklemail@gmail.com>
Tue, 22 Dec 2015 01:33:40 +0000 (17:33 -0800)
* 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

resources/src/mediawiki/mediawiki.template.mustache.js
tests/phpunit/data/templates/conds.mustache [new file with mode: 0644]
tests/qunit/QUnitTestResources.php
tests/qunit/suites/resources/mediawiki/mediawiki.template.mustache.test.js [new file with mode: 0644]

index 624986a..7f62256 100644 (file)
@@ -4,8 +4,27 @@
        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 ) ) );
                                }
                        };
                }
diff --git a/tests/phpunit/data/templates/conds.mustache b/tests/phpunit/data/templates/conds.mustache
new file mode 100644 (file)
index 0000000..5ebd2ea
--- /dev/null
@@ -0,0 +1 @@
+{{#list}}oh no{{/list}}{{#foo}}none of this should render{{/foo}}
\ No newline at end of file
index 545718a..926e986 100644 (file)
@@ -71,6 +71,7 @@ return array(
                        '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',
@@ -126,6 +127,7 @@ return array(
                        'mediawiki.toc',
                        'mediawiki.Uri',
                        'mediawiki.user',
+                       'mediawiki.template.mustache',
                        'mediawiki.template',
                        'mediawiki.util',
                        'mediawiki.special.recentchanges',
diff --git a/tests/qunit/suites/resources/mediawiki/mediawiki.template.mustache.test.js b/tests/qunit/suites/resources/mediawiki/mediawiki.template.mustache.test.js
new file mode 100644 (file)
index 0000000..38ae5e4
--- /dev/null
@@ -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 ) );