General developer forum

Mustache - use partial from another module's templates?

 
Picture of Dave Balch
Mustache - use partial from another module's templates?
Core developersParticularly helpful MoodlersPlugin developers

Hi,

I'm building an activity that includes defining a map (using Leaflet), and am looking for a good way to construct the UI.

This UI will enable configuring map features (e.g. zoom levels allowed, which map UI widgets are enabled), using javascript to watch form widgets for changes and then apply them to the map (and serialising them to save the activity).


I'm hoping to get the standard theming by using the Boost core_form templates - but can't see how to make it work without copying them into my own module.

My JS can render the a boosttemplate by specifying it's in the boost module - e.g. this works:

templates.render('core_form/element-button', context, 'boost')


...but if I try and include it as a template partial in my module's templates, I get "core_form/element-button Error code: filenotfound"

mod/dave/templates/configure.mustache:

    {{> core_form/element-button }}


Is there a way to specify the boost module in the template?

Cheers,
Dave.

 
Average of ratings: -
Picture of Joby Harding
Re: Mustache - use partial from another module's templates?
Core developers
Hi Dave, I've quickly tested including core_form/element-button from an activity plugin template rendered by PHP and it outputs without error when the active theme is Boost. I suspect what might be happening is that you are using a custom theme which does not inherit from Boost? If this is the case then the Boost templates are not included when the template loader resolves the template ID.

TLDR

I've included more detail below but in short if you're not including the form elements through the forms API it might be easier to apply the corresponding Bootstrap (4) classes to your own markup in order to ensure styling is consistent.

The Detail

To answer your question there isn't a way to explicitly specify theme when including a partial, or when rendering a template using PHP via $OUTPUT->render_from_template(). The PHP based loader automatically detects the active theme being used in the current page. The JavaScript core/templates module render method is the exception in this sense. Usually if a theme-based override is not present the renderer should fall back to the original core template rather than exiting with an error.

To add an extra dash of obscurity into the mix here you'll notice in the instance of core_form/element-button there isn't a core template even though Boost appears to be overriding it with templates/core_form/element-button.mustache. The forms renderer performs some of its own checks to see if a Mustache template for the current element is available. If form elements aren't being output via the Forms API (e.g. included directly as a partial) this logic isn't applied and the unresolved template will result in an error.

Hope that helps you get to the bottom of it! smile
 
Average of ratings: Useful (1)
Picture of Dave Balch
Re: Mustache - use partial from another module's templates?
Core developersParticularly helpful MoodlersPlugin developers

Hi Joby,

> using a custom theme which does not inherit from Boost
Yes, that's right (sorry I dodn't make that clear).

> apply the corresponding Bootstrap (4) classes to your own markup in order
Ok, thanks for confirming, and for the detailed explanation smile

Cheers,
Dave.

 
Average of ratings: -
Gareth J Barnard
Re: Mustache - use partial from another module's templates?
Core developersParticularly helpful MoodlersPlugin developers

Hi Dave,

What theme are you using please?

But.... if you have a renderer in the activity that follows the standard inheritance tree for renderers that would have the 'get_mustache()' method then you can override that to use your own template loader that could cope with the partial format:

{{> theme_boost/core_form/element-button }}

as I've done something similar (and published on Moodle.org in my Foundation theme), see:

  1. https://github.com/gjb2048/moodle-theme_foundation/blob/master/classes/output/core/output/mustache_template_finder.php
  2. https://github.com/gjb2048/moodle-theme_foundation/blob/master/classes/output/mustache_engine.php#L74
  3. https://github.com/gjb2048/moodle-theme_foundation/blob/master/classes/output/core_renderer.php#L39
  4. https://github.com/moodle/moodle/blob/MOODLE_35_STABLE/lib/outputrenderers.php#L479
  5. https://github.com/moodle/moodle/blob/MOODLE_35_STABLE/lib/outputrenderers.php#L53
  6. https://github.com/moodle/moodle/blob/MOODLE_35_STABLE/lib/outputrenderers.php#L80

G

 
Average of ratings: -