Add parameters to course mustache code

Add parameters to course mustache code

von jon webdev -
Anzahl Antworten: 6
Hi,

We are trying to add parameters to the JSON used for the course module mustache code.

What we're trying to achieve
Our old theme displayed the pluginname i.e. Forum, Assignment, Page etc above the activity name, in the cmname.mustache file.

The current Boost version doesn't show this but we would like to re-add this feature. I can get something similar by adding:

 {{#modname}} 
<div class="activitytype text-uppercase small"> 
<span>{{{modname}}}</span> 
</div> 
{{/modname}}

to cmname.mustache under theme_name/templates/core_courseformat/local/content/cm/cmname.mustache. This works but I'm having to use modname not pluginname as it seems to be empty and modname is a shortened version of plugin name so assignment become assign which is not ideal.

We would also like to use the variable purpose in a different part of the course module to change the activities background colour. I can do this by adding:

            'purpose' => plugin_supports('mod', $mod->modname, FEATURE_MOD_PURPOSE, MOD_PURPOSE_OTHER),
To line 111 of cmitem.php in the core files but would rather be able to do this from the theme.

Things We've tried:
We've tried following the guidance on https://moodledev.io/docs/apis/plugintypes/format#creating-the-basic-output-structure but my output classes don't have any effect when run from inside my theme.

Does anyone have a working example of doing this to help me on my why or can suggest reasons why following the above links instructions in a theme would not work?

Many Thanks,
Jon


Als Antwort auf jon webdev

Re: Add parameters to course mustache code

von Perial Dupont -
Hey Jon,
it is recommended to change directly the moodle core code. All you changes will disappear once you update the theme of moodle itself.
Instead you can create a child theme for or build a local plugin that exactly do what you describe.

To create a child theme look here: https://docs.moodle.org/dev/Creating_a_theme_based_on_boost
To create a local theme look here: https://moodledev.io/docs/apis/plugintypes/local

But if you said that you know what you are doing than you can try this:
1. Delete the cache on your side. ( Site administration -> purge caches).
2. Try to show the debugging mode on your moodle ( Site Administration-> debugging->Developper Extra .....) see if you get some warnings or errors.
Als Antwort auf Perial Dupont

Re: Add parameters to course mustache code

von jon webdev -
Thanks Perial,

This theme is built on Boost.
I am looking for a way to do this without editing core for preference.

Thanks,
Jon
Als Antwort auf jon webdev

Re: Add parameters to course mustache code

von Mark Sharp -
Nutzerbild von Core developers Nutzerbild von Particularly helpful Moodlers Nutzerbild von Plugin developers

Before it was taken out get_string was being used

get_string('pluginname', 'mod_' . $mod->modname);

You can get the same effect in the template:


{{#modname}}
    <div class="activitytype text-uppercase small">
        <span>{{#str}} pluginname, mod_{{modname}} {{/str}}</span>
    </div> 
{{/modname}}
Als Antwort auf Mark Sharp

Re: Add parameters to course mustache code

von jon webdev -
Thanks Mark,

That's definitely a neat way to do it. I didn't realise {{#str}} could be used to pull in the language pack info like that.

I'm having trouble with your example though. This pulls through the correct pluginname for mod_assign:

{{#str}} pluginname, mod_assign {{/str}}

but this errors out with 'Debug info: en_"mod_{{{modname}}}"_1701272511 Error code: codingerror':

{{#str}} pluginname, mod_{{modname}} {{/str}} 

I suspect it doesn't like the concatenation. I've tried escaping the modname with {{{modname}}} and wrapping with quotes but they all cause the same error.
Als Antwort auf jon webdev

Re: Add parameters to course mustache code

von Mark Sharp -
Nutzerbild von Core developers Nutzerbild von Particularly helpful Moodlers Nutzerbild von Plugin developers
Ah, that's a shame. If the string accepted arguments, then you could pass the modname in. e.g.
{{#str}} mystring, theme_mytheme, {{modname}} {{/str}}

Where mystring is defined in your theme:
$string['mystring'] = 'Modname {$a}';

They took out pluginname in 4.3. Which I think is a little odd as it was optional anyway.

I think the only way now, is to override the class in your course format.

Als Antwort auf Mark Sharp

Re: Add parameters to course mustache code

von jon webdev -
Thanks Mark,

That's been really helpful.

For the moment I've added the full plugin name with CSS like this:

.modtype_assign .activitytype span:after {
content:'ment';
}

as 'Assignment' seems to be the only one that gets shortened. I'll re-visit overriding the course format class at a later date lächelnd