Fony Tinlay
Fony Tinlay

Reputation: 59

Moodle 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?

Upvotes: 1

Views: 3053

Answers (2)

John
John

Reputation: 1808

If you just want to override parent theme templates inside your extended child theme then:

  1. Create a folder in your theme folder name {yourtheme}/templates/theme_{parent_theme_name} (e.g. if you extended boost create a folder at {yourtheme}/templates/theme_boost

  2. Copy the mustache file you want to override from the parent theme into this folder. (e.g. if you are extending boost and you want to override header.mustache, then copy theme/boost/templates/header.mustache to theme/{yourtheme}/templates/theme_boost/header.mustache and then edit it)

Upvotes: 7

Kaushtuv
Kaushtuv

Reputation: 489

Edit: I did not see the part about not extending the renderer. I have left the answer for other users looking for a solution involving a renderer.

You will need to create a custom core renderer that extends the core_renderer. Then re-create (copy from core_renderer class) the function block(block_contents $bc, $region)

You can then assign a variable to the $data there and it would be available in the template.

class theme_yourthemename_core_renderer extends core_renderer {
... 
    public function block(block_contents $bc, $region) {
        $this->init_block_hider_js($bc);
        $data = \core\output\block::from_block_contents($bc, $this);

        // Your code here
        $data->yourvar = 'Some val'

        return $this->render_from_template('core/block', $data);
    }
}

Upvotes: 2

Related Questions