General developer forum

Mustache template based »cleanm« theme

 
This discussion has been locked because a year has elapsed since the last post. Please start a new discussion topic.
Picture of Urs Hunkler
Mustache template based »cleanm« theme
Group Core developers

To learn how the Moodle 2.9 Mustache templates are implemented I created a Mustache based version of the »clean« theme which I named »cleanm« - »clean« with an ending »m« for Mustache.

This was a nice adventure and I want to thank Damyon and all others who worked on the implementation - finally Moodle template based themes are possible.

How is the theme constructed?

  • The theme is based on autoloading renderer and renderable classes for the different page layouts. One renderer class, one »base_layout« renderable class with the common functionality and one renderable class for each layout which extends the »base_layout«.
  • There is only one central »layout.php« file.
  • The different page layouts are defined in the »$THEME->layouts« array. I added a new 'renderable' item holding the renderable class name that will be used.
  • There is one »wrapper« template with the header and footer part that is common for all page layouts.
  • With a dynamic partial Mustache helper the individual page partial template is included into the wrapper template. The name of the partial to use is set in the renderable for the layout type in a variable. 
  • The dynamic partial helper renders the Mustache partial tag with the given partial template name. 
  • I added the dynamic partial helper to the helpers in core. This helper I had developed for my former Moodle Mustache themes.
  • The information from the theme settings are added to the template variables directly in the renderable classes. I removed the function from »lip.php«.
Can you have a look at the »cleanm« Mustache template theme?

Yes, I committed the theme and the Mustache helper implementation to my Moodle fork https://github.com/uhunkler/moodle. The last three Commits on Mar 31, 2015 contain the theme and the helper. You may have a look at https://github.com/uhunkler/moodle/commits/master. The theme is committed to the master branch.

Your feedback is welcome

Martin and I had talked about a template based theme several years ago - now I could create it based on Moodle core without hacks thanks to the great Moodle changes - Mustache templates. I just needed to add the dynamic partial Mustache helper to core.

I am curious what you think about the »cleanm« template theme implementation ...

 
Average of ratings: Useful (5)
Picture of Urs Hunkler
Re: Mustache template based »cleanm« theme
Group Core developers

This is a first implementation. I solved all issues I discovered - there may be more.

 
Average of ratings: -
Dan at desk in Moodle HQ, Perth
Re: Mustache template based »cleanm« theme
Group Core developersGroup Moodle Course Creator Certificate holdersGroup Moodle HQGroup Particularly helpful MoodlersGroup Plugin developersGroup Testers

Thanks for trying this out - I am sure others will learn from your attempts - and you might be able to find early limitations in our integration with mustache which we can fix before release.

 
Average of ratings: -
Me!
Re: Mustache template based »cleanm« theme
Group Core developersGroup Moodle HQGroup Particularly helpful MoodlersGroup Plugin developersGroup Testers
Urs - this is great.

Thanks for trying the new code - and for sharing your experiment with others.

I was interested in your requirement for a dynamic partial helper - can you explain why you thought this was required?

It seems that you are using it to do something like this:

$data->pagelayout = 'theme_cleanm/partials/columns1';

So that a template can be like:

HEADER
{{> 'theme_cleanm/partials/columns1'}}
FOOTER

A different pattern would be to set:

$data->pagecontent = render_from_template('theme_cleanm/partials/columns1')

So that the template can be like:

HEADER
{{pagecontent}}
FOOTER

Would that alternative work without the helper?

In general my thoughts on helpers are that we should have as few as possible, and any we do add need to be 100% compatible between JS and PHP. The reasoning for having few helpers is so that we do not bloat the template renderer - and add a lot of special syntax that will not be familiar to devs who already know mustache.

 
Average of ratings: -
Picture of Urs Hunkler
Re: Mustache template based »cleanm« theme
Group Core developers

Great that you think that my work on the »cleanm« Mustache based theme may be helpful.

I had used the »dynamic partial« helper to be able to make Mustache templates more flexible. Your proposal to »render_from_template« the partial in the wrapper renderable into the »$data->pagecontent« variable should do the same. I'll try that approach and come back with the result.

With the approach to use as few helpers as possible I agree. 

But if I would need a helper for a certain task Mustache is very flexible by making it easy to just add the helper when I need it. The Moodle implementation locks the Mustache instance away and reduces the flexibility Mustache offers. That approach I see as not helpful. I would prefer to be able to access all Mustache features from within Moodle.

 
Average of ratings: -
Me!
Re: Mustache template based »cleanm« theme
Group Core developersGroup Moodle HQGroup Particularly helpful MoodlersGroup Plugin developersGroup Testers
One approach may be to allow themes to inject helpers - but only themes.

The reasoning is that a theme may want to write a template in a specific way and they need the flexibility - and they KNOW at that point how the template will be called (php or js).

A plugin dev is more dangerous here - they may add a helper with no javascript equivalent and use it from their templates. Later a themer tries to call their templates from JS and it will explode. A plugin dev also may not export their data properly if they can add logic (via helpers) to their templates. E.g. a helper to perform capability checks (very bad).

 
Average of ratings: -
Picture of Michael Aherne
Re: Mustache template based »cleanm« theme
Group Core developersGroup Particularly helpful MoodlersGroup Plugin developers

So stopping bad developers doing stupid stuff beats helping good developers do cool stuff? smile

 
Average of ratings: -
Picture of Urs Hunkler
Re: Mustache template based »cleanm« theme
Group Core developers

Damyon - I would like to see the Moodle Mustache implementation as transparent/open as possible.

The implementation should help avoid issues - but it should not try to protect from failure. 

When I make a mistake in my code and somebody reports a bug let's say half a year later I need to fix that bug. When somebody implements a helper only half way (only the PHP part and not the JavaScript part as in your example) and this half implementation becomes an issue sometimes later the helper needs to get fixed. That's how software development works - isn't it?

So the minimum I would like to see is that it will be possible to add helpers in the theme.

 
Average of ratings: Useful (1)
Me!
Re: Mustache template based »cleanm« theme
Group Core developersGroup Moodle HQGroup Particularly helpful MoodlersGroup Plugin developersGroup Testers
The problem with helpers, is that it is is easy to add something in php (which has full access to all the config values, database, filesystem, core apis etc) that is impossible to replicate in javascript, or would perform terribly in javascript and then we have a broken system.

So for plugin developers who do not know how their templates will be (re)used - they should not be using helpers and only rely on the basic functionality.

But as I said before - I am not against opening up this flexibility to themers, who are at the end of the chain and should know what their templates will be used for.

 
Average of ratings: -
Gareth J Barnard
Re: Mustache template based »cleanm« theme
Group Core developersGroup Particularly helpful MoodlersGroup Plugin developers

Hi Urs,

This looks really great smile.

I do have a question about the dynamic helper and see you've replied, but can I confirm that:  It's there because a renderer needs to translate the frankenstyle pagelayout partial path prefix such that it will work with Mustache's partial syntax of '{{>'?  If so, will 'dyn' be in M2.9?  As I'm really excited about the possibility of adapting one of my simpler themes - Shoelace - to use it and that theme uses 'tiles' which are the same concept.

Many kind regards,

Gareth

 
Average of ratings: -
Picture of Urs Hunkler
Re: Mustache template based »cleanm« theme
Group Core developers

Great that you think my work on the »cleanm« Mustache theme is helpful Gareth. You described the function of the »dynamic partial« helper. But as you can see from my related post it is not necessary. The render_from_template call in the renderable produces the same result without the helper.

I have removed the helper from my Moodle 29 branch again.

 
Average of ratings: Useful (1)
Picture of Urs Hunkler
Re: Mustache template based »cleanm« theme
Group Core developers

Great proposal Damyon to use the render_from_template('partial/partialname') call in the renderable instead of a »dynamic partial« helper. With very few changes in the code the helper is not needed any more.

I updated the moodle branch to the latest weekly and committed the theme changes. And I reverted the »dynamic partial« helper commit - it's not needed. You may get the changes form the GitHub repository - or have a look at https://github.com/uhunkler/moodle/commits/master.

 
Average of ratings: -
Picture of Urs Hunkler
Re: Mustache template based »cleanm« theme
Group Core developers

Do you know why the course context and the doctype

$contextcourse= context_course::instance(SITEID);
$doctype = $OUTPUT->doctype();

need to be called in the layout.php? I first had the two lines implemented in the »base_layout« renderable but Moodle complained - for example about the missing doctype call.

 
Average of ratings: -
Me!
Re: Mustache template based »cleanm« theme
Group Core developersGroup Moodle HQGroup Particularly helpful MoodlersGroup Plugin developersGroup Testers
There is a comment saying doctype() is designed to be called from layout files, but I am not sure exactly why. I think it just has to be called before the first call to $OUTPUT->header()

Im not sure what you mean about coursecontext, I don't think that is needed at all in a layout file.
 
Average of ratings: -
Picture of Urs Hunkler
Re: Mustache template based »cleanm« theme
Group Core developers

After some further thinking about the »dynamic partial helper« I come to the conclusion that I prefer to use a »dynamic partial helper« over the rendering step Damyon had proposed and I had implemented in the latest version of the »cleanm« theme.

The main reason is not technique but concept - I think separation of code generation and layout with templates is a big advantage. When I call a renderer with a template in the code generation process for another renderer with a template I start to mix code generation and layout again.

So I prefer to be able to think about the process as three steps with as much separation as possible

  1. Get and calculate all data
  2. Create a template with the page look
  3. Fill the data into placeholders in the template to render the output

With these steps I can concentrate and work on the best solutions for back-end and front-end solutions more separated. To be able to mentally separate step 1 and step 2 helps me a lot to finetune and optimize the result.

The conclusion

So please let's implement a »dynamic partial helper« - it will be useful the same way in PHP and JavaScript.

 
Average of ratings: Useful (1)
Picture of Urs Hunkler
Re: Mustache template based »cleanm« theme
Group Core developers

Damyon, did you consider to add the »dynamic partial helper« to core for Moodle 2.9? It would be very welcome and helpful.

 
Average of ratings: -
Picture of Urs Hunkler
Re: Mustache template based »cleanm« theme
Group Core developers

This question seams to have gotten lost within all the Moodle 2.9 preparation work. I still think it would be important to add the Mustache helper to Moodle 2.9 to support templating in Moodle. If you don't agree an answer would be nice.

Thanks.

 
Average of ratings: -
Picture of Urs Hunkler
Re: Mustache template based »cleanm« theme
Group Core developers

Ping.

 
Average of ratings: -
Picture of Urs Hunkler
Re: Mustache template based »cleanm« theme
Group Core developers

I have created a dedicated branch for the Moodle 29 Mustache work https://github.com/uhunkler/moodle/commits/MOODLE_29_mustaches. In this branch the actual state of the »cleanm« theme is one commit - I have cleaned up the commits. Please don't use the »master« branch but the »MOODLE_29_mustaches« when you want to test the »cleanm« theme.

 
Average of ratings: Useful (1)
Martin Dougiamas
Re: Mustache template based »cleanm« theme
Group Core developersGroup Documentation writersGroup Moodle HQGroup Plugin developersGroup Testers

Thanks for your great work on this Urs!   


 
Average of ratings: -
Picture of Urs Hunkler
Re: Mustache template based »cleanm« theme
Group Core developers

smile

 
Average of ratings: -
Picture of Howard Miller
Re: Mustache template based »cleanm« theme
Group Core developersGroup Documentation writersGroup Particularly helpful MoodlersGroup Plugin developers

Can I (just for my own interest) ask what the point is? I have never seen much point in using templating 'languages' with PHP. It's just (yet) another layer of complexity. Complexity == stuff to learn multiplied by things to go wrong. 

I do appreciate that moustache templates appear to be the latest cool thing. 

 
Average of ratings: Useful (2)
Picture of Howard Miller
Re: Mustache template based »cleanm« theme
Group Core developersGroup Documentation writersGroup Particularly helpful MoodlersGroup Plugin developers

that cool eh? tongueout

 
Average of ratings: -
Martin Dougiamas
Re: Mustache template based »cleanm« theme
Group Core developersGroup Documentation writersGroup Moodle HQGroup Plugin developersGroup Testers

Some years ago I would have agreed with you, Howard, when I dropped Smarty from Moodle.

But look at the problems that the current renderers have caused in many themes because designers (who are usually not expert programmers) have to implement PHP that slowly gains in complexity and unreadability until it becomes code that breaks on core upgrades.

The idea of proper templates is to separate design-related things from logic-related things and I think most designers will be really happy with us taking this approach using mustache.

 
Average of ratings: -
Tim at Lone Pine Koala Sanctuary
Re: Mustache template based »cleanm« theme
Group Core developersGroup Documentation writersGroup Particularly helpful MoodlersGroup Plugin developers

I only partly agree with this.

The goal is to separate design-related things from logic-related things.

Templates are a technical means to try to achieve that. However this is not really a technical problem. (It is like expecting anti-plagiarism software to stop your students Googling the answers to your questions.)

Renderers were a perfectly good technical approach to separating logic and design, but were let down because of lack of discipline. Developers (including HQ developers) wrote code mixing logic into the renderers, and that got through peer and integration review.

It is not necessary to use a cut down language to restrict what developers can do. E.g. Moodle language files are PHP files, but people do not abuse that to put crazy stuff in there. They just put in lines like $string['key'] = 'String'; as we expect them to do.

I also disagree with your claim that renderers are "code that breaks on core upgrades". The OU has some heavily customised renderers, and I don't remember that being a particular problematic area. There have been much more troublesome things we have had to deal with as part of Moodle version upgrades. (On the other hand, the things you can do with renderers has saved us from many core code modifications.)

It is also not clear to me that the switch to templates will magically solve all API compatibility issues. When parts of the standard Moodle interface change, that will break anyone who has customised that bit of interface, whether that customisation is done in templates or PHP code.

Anyway, let us hope that this latest evolution in Moodle's output code achieves the goal we all want, but we will have to wait and see.

 
Average of ratings: Useful (6)
Picture of Howard Miller
Re: Mustache template based »cleanm« theme
Group Core developersGroup Documentation writersGroup Particularly helpful MoodlersGroup Plugin developers

...also let's hope this gets some *proper* performance testing.

With my (usual) cynical hat on, I can't help thinking that this looks more like a fun thing to do than something that will actually benefit end users. As you say, Tim, let's (also) hope that it achieves its goals. 

Interestingly, Mahara has had proper templates from the off and they've still managed to properly break themes between versions on several occasions. The template language then just becomes something else to worry about. I know template languages are rarely that onerous but it's yet another thing on the growing list. 

 
Average of ratings: -
Picture of Urs Hunkler
Re: Mustache template based »cleanm« theme
Group Core developers
From my point of view there is the potential for great benefit for the users. When the page the user sees gets a better representation in the code the potential is there to create better working pages/interfaces for the learning content. For me it will be much easier to create great pages.
 
Average of ratings: -
Picture of Urs Hunkler
Re: Mustache template based »cleanm« theme
Group Core developers

Tim and Howard I think it may be helpful for the discussion to understand that there are not only PHP developers involved in creating Moodle. There are people like me with a different background and probably different mental models. With my visual design background and experience in creating web pages in HTML I can contribute some expertise in creating Moodle pages.

As I explained in another posting for my way of thinking PHP renderers or HTML/PHP mixes are too complex - logic less templates come closest to the final result. And I am sure there are several more people thinking like me working with/for Moodle.

 
Average of ratings: -
Picture of Urs Hunkler
Re: Mustache template based »cleanm« theme
Group Core developers

Tim, templates will surely not magically help to avoid issues. But they will help many theme designers to better understand the »code« they work with - with the result to more easily be able to follow changes and implement them in the themes.

A huge step forward would be to develop pages / page areas / widgets or however you may call it in an »element library« or »pattern library« and developers and theme designers consequently using these sources.

 
Average of ratings: -
Picture of Urs Hunkler
Re: Mustache template based »cleanm« theme
Group Core developers

A very important aspect in working with web development/design are the mental models people are using.

I am an educated Graphic Designer and a developer. When I work with Moodle I always »see« the resulting pages in the browser. And I »see« the DOM tree in the browser's developer tools. My mental model is the styled page with the underlying DOM tree.

The best results I can achieve when I am able to use code that comes close to my mental model - that means something looking similar to the DOM tree which is something similar to HTML.

Code coming close to a HTML page is a template. Preferable a logic less template language (like Mustache). So with Mustache templates I am able to create better results with less effort - because the code I work with looks close to the mental model I have from the final page.

PHP as a template engine would help if it would be used in the pure form: Write HTML and echo out the data - without any logic in the page. By the way Mustache templates technically come close to that approach, they »only« abstract out the echo statements and the conditional/loop statements in visually compact code and make the page better readable as a HTML page.

Mustache:

<ul class="listing">
   {{#data}}
    <li class="list-item">{{oneitem}}</li>
   {{/data}}
</ul>


HTML/PHP mix:

<ul class="listing">
   <?php foreach ($data as $oneitem) { ?>
   <li class="list-item"><?php echo $oneitem ?></li>
   <?php } ?>
</ul>


Or with a renderer:

<ul class="listing">
   <?php echo $OUTPUT->listitems($data); ?>
</ul>


The less »foreign« code is used in the page the clearer the HTML page structure gets. In the example with the renderer I don't even get any information how the list items will look. I need to check the renderer code. The page drifts away from my mental model of the page in the browser the user sees with each step of abstraction. A Mustache template is the closest I can get - it works with the least amount of non HTML code.

I guess the principle to separate code generation from code output is somehow agreed about - it is the same prerequisite for renderers and for templates.

  • For code generation Moodle uses PHP
  • To display the page in the browser Moodle uses HTML
  • To get the code into the page with least cluttering HTML Moodle uses Mustache


 
Average of ratings: Useful (7)
Picture of Urs Hunkler
Re: Mustache template based »cleanm« theme
Group Core developers

One additional aspect that makes the Mustache template easy to read without the need to analyze each line is the use of the different "brackets" - "<>" for HTML, "{{}}" for the non HTML placeholders. One directly sees the difference.

 
Average of ratings: -
Picture of Howard Miller
Re: Mustache template based »cleanm« theme
Group Core developersGroup Documentation writersGroup Particularly helpful MoodlersGroup Plugin developers

To be completely honest... I have never actually encountered a graphic designer actually 'developing' the HTML pages (using any method). They tend to bash something together in Photoshop and then I get to do the work (and there's nothing wrong with that, I can't do design to save my life!)

I've got no huge objection to templating languages. In a different existence I use Symfony and hence the Twig templating language without giving it a second thought (although it's another documentation tab to have open in the browser). 

I accept that the resulting page is easier on the eye for the developer. However, the notion that a page created in any non-trivial web application can be 'logic less' seems very far-fetched to me. Surely the presence of logic is almost 'by definition'. 

 
Average of ratings: -
Picture of Urs Hunkler
Re: Mustache template based »cleanm« theme
Group Core developers

Another valuable aspect of Mustache templates is that you can create representations of page areas in a template. 

  • With PHP you can use this template on the server to render the HTML page delivered to the browser. 
  • With Javascript you can use the exactly same template to render the page area in the browser when the data has been delivered for example from an AJAX call.
 
Average of ratings: -
Picture of Urs Hunkler
Re: Mustache template based »cleanm« theme
Group Core developers

Howard, I think PHP and Mustache are not that »cool« at all as you call it.

To work with Jade templates, the Stylus CSS preprocessor and JavaScript written in CoffeeScript is cool. With this combination of languages/transpilers even the development language specific elements like brackets, semicolons etc. are stripped out. The structure produces the meaning. It is possible to work with the »pure« information.

The content plays the main role and not the language specific representation. I would like to exaggerate a bit in the context of this discussion and say code is drilled down to it's essence.

I like the puristic and structured form of development. This might also be an explanation why I prefer Mustache templates over PHP/HTML mixes or PHP renderers.

 
Average of ratings: -
Picture of Danny Wahl
Re: Mustache template based »cleanm« theme
Group Core developersGroup Plugin developers

I think that mustache has the potential to make things a little less complex.  I say potential because I've setup a ghost blog to teach myself mustache, and from 0.5.6 to 0.6.0 they've added increasingly more and complex helpers with each release.  The potential for things to go wrong is if we add a million logical helpers into mustache that exponentially multiply the amount of output variables by the number of renderer tweaks and overrides.

So, as long as mustache is actually logic-less I don't care, but If I have to start controlling logic from a renderer (override) and then manipulating the output with mustache then it becomes a problem.

 
Average of ratings: -
Tim at Lone Pine Koala Sanctuary
Re: Mustache template based »cleanm« theme
Group Core developersGroup Documentation writersGroup Particularly helpful MoodlersGroup Plugin developers

The problem is, anyone who says 'logic-less output' is clearly self-documenting their contribution to the discussion as rather naive.

What is displayed on the screen in Moodle is full of logic:

  • Which permissions the current user has.
  • What their preferred HTML editor, set in their profile is.
  • The current state of the thing being displayed (e.g. a forum post can only be deleted if it is less than 30 mins old and does not have a reply).
  • A particularly difficult case, which is why I am sensitive to this issue, is quiz questions. What can be displayed depends on the settings the teacher made under Review options, and the current state of the question.
  • The admin setting for which user profile fields should be displayed whenever a list of users is displayed.
  • ...

You cannot take logic out of output. You can only structure the code (PHP + templates) to make things more or less comprehensible and maintainable.


 
Average of ratings: -
Me!
Re: Mustache template based »cleanm« theme
Group Core developersGroup Moodle HQGroup Particularly helpful MoodlersGroup Plugin developersGroup Testers
"Which permissions the current user has."

The difference with templates is that it a renderer it is tempting to say:
if (has_capability('foo', ...)) {
echo 'thingy';
}

But the decision about what capability is required to see 'thingy' is logic. If a theme overrides this renderer, then they need to copy/paste that capability check. And then when we find a bug and change has_capability('foo') to has_any_capability('foo', 'bar') in the core renderer, the theme will also require updating because it has copy/pasted the logic.

The mustache version
(in export_for_template)
$context->canseethingy = has_capability('foo');

In the template:
{{#canseethingy}}
thingy
{{/canseethingy}}


"What can be displayed depends on the settings the teacher made under Review options" - this is fine - just do the logic checks and store the result in booleans in the context then the templates will be much simpler.
 
Average of ratings: Useful (2)
Tim at Lone Pine Koala Sanctuary
Re: Mustache template based »cleanm« theme
Group Core developersGroup Documentation writersGroup Particularly helpful MoodlersGroup Plugin developers

Right, and that is already how the question renderers have worked since Moodle 2.1, to keep the logic as separate as possible, with not a template in sight.

"tempting" is the only word in your post justifying a separate templating language.

 
Average of ratings: -
Picture of Howard Miller
Re: Mustache template based »cleanm« theme
Group Core developersGroup Documentation writersGroup Particularly helpful MoodlersGroup Plugin developers

...all that will happen is that you'll get a rush of tracker requests for loads of booleans to be added to the data available to templates. Currently, although it may be frowned up, at least you *can* stick some logic in a renderer if you must. I'm not really too upset about that.

There's is no way that core developers will anticipate all the possible changes in custom renderers/templates so it just becomes (effectively) a set of handcuffs for plugin developers and theme developers. 

While I appreciate MVC as much as the next guy I don't pray in its church. If I want to write a nasty plugin - because it's the only way to get something done - then I'd like to have that option. Please wink

 
Average of ratings: -
Picture of Danny Wahl
Re: Mustache template based »cleanm« theme
Group Core developersGroup Plugin developers

The problem is, anyone who says 'logic-less output' is clearly self-documenting their contribution to the discussion as rather naive.


In hope to reframe the perception of my contributions from naive to ignorant I offer the following clarification:

The core should have a permissions check (logic) in a renderer

function show_thingy() {
if(has_capability("viewthingy") {
return $thingy;
}

that renderer can be overridden by a theme in a custom renderer

function show_thingy() {
if(has_capability("viewthingy") {
return $thingy;
} else {
return "you need capability 'viewthingy' to do that";
}
}

but a mustache template should be logic-less in that it only outputs whatever the logic of the renderer has told it to:

{{thingy}}

I was under the impression that the use of mustache templates was mostly intended for themes, and so I realize that the need for renderable objects to have a mustache output exists I'm saying that the handling of the logic of whether or not to display them (or how to) should be restricted to renderers, instead of using mustache helpers.

{{if capability("viewthingy")}}
{{thingy}}
{{/if}}

Perhaps I am actually ignorant/naive to the discussion and I will readily admit to being one of the "designers" that the mustache helpers are intended for tongueout

 
Average of ratings: Useful (1)
Picture of Mike Churchward
Re: Mustache template based »cleanm« theme
Group Core developersGroup Particularly helpful MoodlersGroup Plugin developersGroup Plugins guardiansGroup Testers

"In hope to reframe the perception of my contributions from naive to ignorant I offer the following clarification:"

big grin


 
Average of ratings: -
Me!
Re: Mustache template based »cleanm« theme
Group Core developersGroup Moodle HQGroup Particularly helpful MoodlersGroup Plugin developersGroup Testers
In this simple case, you should not put things in the context that the user can not see.

So in export_for_template you would have:


$context->thingy = null;
if (has_capability('viewthingy')) {
$context->thingy = fetch_thingy();
}


And in the template you just have:

{{thingy}}

Which either does or does not display anything depending on what is in the context.
 
Average of ratings: -
Picture of Chris Lamb
Re: Mustache template based »cleanm« theme
 

I apologise for coming late to this conversation. I have only now started to work with 2.9.

I am confused about implementation of the templating in this release. Urs has provided an implementation with the "cleanm" theme. The only issue straight from GIT sources is that he has a partials subdirectory. Move these templates into the '/templates' directory and remove the path from his classes - and it all works. A nice model I can clearly understand - I can see his data object and I can see the benefits immediately.

From the Moodle docs - I get confused. I see references to renderables and renderers - but no implementation I can completely follow - the admin tool, for instance, which used as a basis for tests leads nowhere for me.

So - should I consider for my early testing that Urs implementation is sound and pursue it?

Sorry, again, if this appears to be off topic - I cannot find many other implementation discussions.



 
Average of ratings: Useful (1)
Picture of Urs Hunkler
Re: Mustache template based »cleanm« theme
Group Core developers

Chris, great that you think the »cleanm« theme is a good and understandable example to work with. The work I have investigated seams to help others.

»cleanm« works with renderables and renderers. For example »class columns3_layout extends base_layout implements renderable, templatable«.

With the Moodle docs I find myself often in the same situation - abstract descriptions of complex topics which are difficult to understand without examples. The cookbook approach is much more helpful for me in many cases. I learned about the Moodle Mustache implementation much more while looking and analyzing Damyon's »learningpath« template example in his Git repository than by reading the docs.

The »partial« subdirectory I have created to save all »dynamic partials« which had been loaded with a »dynamic partial helper« in the first implementation. Damyon proposed to render the »partials« within the renderables without a »dynamic partial helper«. I had proposed to add the »dynamic partial helper« to Moodle core because I think it makes definitely sense when working with templates. But I never have gotten an answer and Moodle 2.9 shipped without a »dynamic partial helper«.

By the way - why do you think the partials subdirectory is an issue? Technically is doesn't matter if you save the templates flat or in an organized subdirectory structure. I prefer the organizational subdirectory structure to offer e better overview. 

 
Average of ratings: Useful (1)
Picture of Chris Lamb
Re: Mustache template based »cleanm« theme
 

Urs, many thanks for your response.

I agree with you that I most often read through a working implementation to learn. The docs are welcome and frequently informative but often not focussed on working implementations.

The reason I moved the partials - having read your 'helper' discussion earlier in the thread - was that I discovered your codebase provoked an error "sub-directories are not allowed" when I tried to test it.

It appeared a simple solution to add the partials to the directory above and alter the references to them. Personally, I would like the partials not to reside in a flat-file structure but it is not an issue, really.

I find your implementation of the renderables and renderers easy to "assemble" in my head. It feels like other structures I have worked with. Thanks, once again.

 
Average of ratings: -
Picture of Urs Hunkler
Re: Mustache template based »cleanm« theme
Group Core developers

Ah, I checked »cleanm« with the latest Moodle and see the issue now.

I don't understand the reason why the subdirectory restriction has been added in MDL-50085. What is the advantage to disallow subdirectories? I filed a tracker issue to remove this restriction again.

MDL-50346 »Remove the restriction to forbid subdirectories in the templates directory«

When I comment the restriction out in »lib/classes/output/mustache_template_finder.php« on line 106 ff the subdirectories work again.

Damyon, can you please explain the restriction and its advantages?

 
Average of ratings: -
Picture of Urs Hunkler
Re: Mustache template based »cleanm« theme
Group Core developers

In the my »MOODLE_29_mustaches« branch I have removed the »templates/partial« subdirectory to get »cleanm« working again. This does not mean that I agree with the changes - I only want to avoid issues for people who think »cleanm« may help with their work. »cleanm« should work without issues.

 
Average of ratings: Useful (1)
I Teach With Moodle
Re: Mustache template based »cleanm« theme
Group Particularly helpful MoodlersGroup Plugin developersGroup Testers

Thank you for your work on this, Urs. 

For those looking for the 'learningplan' example mentioned in Urs's message, you can find it at https://github.com/damyon/moodle/tree/LP2

The folder of interest is found in admin/tool/learningplan. Damyon, would it be useful to add that link to the output documentation page? 

Cheers, 

Fred

 
Average of ratings: -
Me!
Re: Mustache template based »cleanm« theme
Group Core developersGroup Moodle HQGroup Particularly helpful MoodlersGroup Plugin developersGroup Testers
That's not the latest version of that code - and probably not the best place to look.

Learning Plans is still work-in-progress - but the latest code can be found from the tracker item https://tracker.moodle.org/browse/MDL-49458
 
Average of ratings: -
I Teach With Moodle
Re: Mustache template based »cleanm« theme
Group Particularly helpful MoodlersGroup Plugin developersGroup Testers

Thanks. Good to know it's not the best place to look. 

Cheers, 

Fred

 
Average of ratings: -
Gareth J Barnard
Re: Mustache template based »cleanm« theme
Group Core developersGroup Particularly helpful MoodlersGroup Plugin developers

Hi all,

Trying Cleanm on M2.9 and get 'Coding error detected, it must be fixed by a programmer: Templates cannot be placed in sub directories (theme_cleanm/partials/columns3 requested)' - so why are template sub-folders not allowed?  Why!  Just WHY!!!!  It would be so OO if it was!  Or rant - should Moodle be renamed to Mfunctdle ?

Gareth

 
Average of ratings: -
Gareth J Barnard
Re: Mustache template based »cleanm« theme
Group Core developersGroup Particularly helpful MoodlersGroup Plugin developers

P.S.

I've removed the 'partials' bit and got the theme working for M2.9 - attached.  All credit goes to Urs for the code.

Cheers,

Gareth

 
Average of ratings: -
Gareth J Barnard
Re: Mustache template based »cleanm« theme
Group Core developersGroup Particularly helpful MoodlersGroup Plugin developers

P.P.S

This still needs fixing though:

This page did not call $PAGE->set_url(...). Using http://moodle29.chloe/admin/purgecaches.php?confirm=1&sesskey=jRbmSxDDjp&returnurl=%2F%3Fredirect%3D0
line 560 of \lib\pagelib.php: call to debugging()
line 768 of \lib\pagelib.php: call to moodle_page->magic_get_url()
line 812 of \lib\outputrenderers.php: call to moodle_page->__get()
line 3142 of \lib\outputrenderers.php: call to core_renderer->is_login_page()
line 83 of \theme\cleanm\classes\output\base_layout.php: call to core_renderer->user_menu()
line 40 of \theme\cleanm\classes\output\embedded_layout.php: call to theme_cleanm\output\base_layout->export_for_template()
line 40 of \theme\cleanm\classes\output\renderer.php: call to theme_cleanm\output\embedded_layout->export_for_template()
line 188 of \lib\outputrenderers.php: call to theme_cleanm\output\renderer->render_embedded_layout()
line 43 of \theme\cleanm\layout\layout.php: call to renderer_base->render()
line 1015 of \lib\outputrenderers.php: call to include()
line 945 of \lib\outputrenderers.php: call to core_renderer->render_page_layout()
line 881 of \lib\outputrenderers.php: call to core_renderer->header()
line ? of unknownfile: call to core_renderer->redirect_message()
line 1839 of \lib\setuplib.php: call to call_user_func_array()
line 2674 of \lib\weblib.php: call to bootstrap_renderer->__call()
line 2674 of \lib\weblib.php: call to bootstrap_renderer->redirect_message()
line 44 of \admin\purgecaches.php: call to redirect()
 
Average of ratings: -
Gareth J Barnard
Re: Mustache template based »cleanm« theme
Group Core developersGroup Particularly helpful MoodlersGroup Plugin developers

Can be fixed with:

    public function export_for_template(renderer_base $output) {
        global $CFG, $SITE, $PAGE;
        if (!$PAGE->has_set_url()) {
            $thispageurl = new \moodle_url(\qualified_me());
            $PAGE->set_url($thispageurl, $thispageurl->params());
        }

in 'base_layout.php'.

 
Average of ratings: -
Picture of Ben Kelada
Re: Mustache template based »cleanm« theme
 

Great work Urs,

It has been really helpful using this as there is not much in core so far ;)

 
Average of ratings: -
Picture of Ben Kelada
Re: Mustache template based »cleanm« theme
 

havent had time to debug yet, but using this theme with templates, the purege caches page gets this warning:


This page did not call $PAGE->set_url(...). Using http://blahblahblah.test.com/admin/purgecaches.php?confirm=1&sesskey=r4JffcQNR3&returnurl=%2Ftheme%2Findex.php

line 560 of /lib/pagelib.php: call to debugging()

line 768 of /lib/pagelib.php: call to moodle_page->magic_get_url()

line 812 of /lib/outputrenderers.php: call to moodle_page->__get()

line 3142 of /lib/outputrenderers.php: call to core_renderer->is_login_page()

line 83 of /theme/cleanm/classes/output/base_layout.php: call to core_renderer->user_menu()

line 40 of /theme/cleanm/classes/output/embedded_layout.php: call to theme_cleanm\output\base_layout->export_for_template()

line 40 of /theme/cleanm/classes/output/renderer.php: call to theme_cleanm\output\embedded_layout->export_for_template()

line 188 of /lib/outputrenderers.php: call to theme_cleanm\output\renderer->render_embedded_layout()

line 43 of /theme/cleanm/layout/layout.php: call to renderer_base->render()

line 1015 of /lib/outputrenderers.php: call to include()

line 945 of /lib/outputrenderers.php: call to core_renderer->render_page_layout()

line 881 of /lib/outputrenderers.php: call to core_renderer->header()

line ? of unknownfile: call to core_renderer->redirect_message()

line 1798 of /lib/setuplib.php: call to call_user_func_array()

line 2649 of /lib/weblib.php: call to bootstrap_renderer->__call()

line 2649 of /lib/weblib.php: call to bootstrap_renderer->redirect_message()

line 44 of /admin/purgecaches.php: call to redirect()

 
Average of ratings: -