General developer forum

Extending/Overriding Core Mustache Templates

 
Picture of Tony Finlay
Extending/Overriding Core Mustache Templates
 

If I extend a core template in my theme (blocks.mustache for example), how can I output theme data inside it? 

I've added the template to: theme_name/templates/core/blocks.mustache and I've tried adding a simple variable with the site name but it outputs no data.

Is there any way of doing this without extending the renderer?

 
Average of ratings: -
Picture of Kathrin Osswald
Re: Extending/Overriding Core Mustache Templates
Group Core developersGroup Plugin developers

Hi Tony,

in Boost child themes, it's not possible to put mutsache files in a sub directory - even if they are located there within Boost itself.

The child theme calls a function and there you can read following:

[yourserver]/lib/classes/output/mustache_template_finder.php
public static function get_template_filepath($name, $themename = '') {
(...)
107: throw new coding_exception('Templates names must be specified as "componentname/templatename"'
113: throw new coding_exception('Templates cannot be placed in sub directories
}
So you simply have to put it in your own templates folder without any sub directories:

[theme_name]/templates/blocks.mustache

And then call it with render_from_template_function:

return $this->render_from_template('[theme_name]/blocks', $context);


Cheers, Kathrin

 
Average of ratings: Useful (2)
Picture of Richard Oelmann
Re: Extending/Overriding Core Mustache Templates
Group Core developersGroup Particularly helpful MoodlersGroup Plugin developersGroup Testers

You can't.

Besides Katherin's comments above, mustache needs to be fed all the variables its displaying via the context it is sent. So if the variable you want to display isn't already part of the context being sent to your template (I am assuming its not as you said you added a variable to the template), you would have to override the renderer or whatever function or layout file is sending the context to the template.

Richard

 
Average of ratings: Useful (2)
Picture of Kathrin Osswald
Re: Extending/Overriding Core Mustache Templates
Group Core developersGroup Plugin developers

Hi Richard,

good point. Forgot this aspect.

 
Average of ratings: -
Picture of Tony Finlay
Re: Extending/Overriding Core Mustache Templates
 

Hi Richard/Katherin, thanks for the replies, kind of hoping this wasn't the case.

 
Average of ratings: -
Picture of Richard Oelmann
Re: Extending/Overriding Core Mustache Templates
Group Core developersGroup Particularly helpful MoodlersGroup Plugin developersGroup Testers

Hmmm - looking at this some more, it seems you cannot put a template into a sub-folder if you are calling it yourself from a context, because its in the calling the template that the script Katherine refers to seems to be called

However, if you are overriding an existing template e.g. the main.mustache tempalte for the new myOverview block then that does need to be in a sub-folder structure (as it is in Boost)

/mytheme/templates/block_myoverview/main.mustache

So, if you are extending it and changing the context and therefore having to call the template yourself, then it appears to be as Katherine already said. But if you are simply re-arranging the existing template, using the existing call and context, then it goes in a subfolder structure representing where the original template that is being cloned came from.

Hmmm - seems overly complicated to me. We obviously need the subfolder structure if you are overriding one (there may be many blocks with a 'main.mustache' file, but if I want to change the context for several of those, we would have to rename them as well, rather than put them in a subfolder. Surely for ease of development and for consistency, subfolders should be allowed to enable this kind of development more easily?

(or have I misunderstood the way this works???)


 
Average of ratings: -