How can I add a Mustache helper to Mustache from a Moodle plugin or theme?

How can I add a Mustache helper to Mustache from a Moodle plugin or theme?

by Urs Hunkler -
Number of replies: 2
Picture of Core developers

It is possible to add Mustache helpers dynamically to Mustache. I didn't see an obvious solution to add an helper in Moodle.

How can I add a Mustache helper to Mustache from a Moodle plugin or theme?

Average of ratings: -
In reply to Urs Hunkler

Re: How can I add a Mustache helper to Mustache from a Moodle plugin or theme?

by Damyon Wiese -
In an early version of the mustache code I allowed themers to define custom helpers for mustache - but the problem is that it was easy for php - but hard for javascript and we really really need templates to work identically for both.

E.g. the template library lets you browse all the templates on the system and if the templates has the correct comments - it can be "rendered" in the current page/theme. This is done by the JS renderer - so if there were php only helpers added by a theme the templates would not render correctly in JS.

In reply to Damyon Wiese

Re: How can I add a Mustache helper to Mustache from a Moodle plugin or theme?

by Urs Hunkler -
Picture of Core developers

Please see my thoughts on helpers in my comment https://moodle.org/mod/forum/discuss.php?d=311119#p1245495.

In my first simple local Mustache test plugin my first approach was to just use the line 

»$OUTPUT->render_from_template('local_mustache_tests/templatename', array('text' => 'Some text to show on the page'));«

But Moodle complained that there is no template »/lib/templates/templatename« (I don't remember the exact path - it just gave me the information that Moodle only looks for templates in the core templates).

When I looked at the Moodle code I saw what you described above - the JavaScript template library browses the system for templates but from PHP only templates included via renderers/renderables are found. I would prefer to be able to use Mustache in Moodle with PHP and JavaScript with all flexibility Mustache offers.

Why is it better to restrict the Mustache template handling in Moodle? 

For core code I see advantages to uses a few common patterns. But as a plugin developer I would prefer to be able to use Mustache's features and don't have Moodle lock Mustache away from the plugin code. Probably it would help already to make the »get_mustache« method public and not »protected«.

If I had access to the Mustache instance it would be easy to tell Mustache about additional template sources.

When I looked at the code again just now while writing this comment I understand the reason why Moodle implements the Mustache template handling in a special restricted way: with this implementation Moodle can guarantee that templates can be overridden in themes. If plugins could add their own templates overriding these plugin templates would be more difficult.

I am not sure if it might be possible to implement a more flexible template integration in Moodle.