Getting data back from a mustache template

Getting data back from a mustache template

by Richard Bergemann -
Number of replies: 8

Hello everyone.

I am doing some development in moodle 3.7 for a new admin tool plugin.  In this plugin I am generating a page from a somewhat complex custom made .mustache template.  This template contains a number of values that are defined and managed within it, that I want to access once a submit button is pressed.

The issue I am running into is that I am unable to access these submitted values that are defined in the template.  I can see their values in debugging under $form->_form->_submitValues but I cannot get to them due to _form being a protected variable of $form.  I called $form->get_data(), but that only gave me access to the elements that I defined in php, not in the template.  I'm not sure if this is the right place to ask this question, but if anyone has any help they could give me on a path forward it would be much appreciated.

I am adding the template to the page using the function:

$mform->addElement('html', $OUTPUT->render_from_template(PLUGIN_TOOL_NAME/members', $rendercontext));


Please let me know any other information you may need.


Average of ratings: -
In reply to Richard Bergemann

Re: Getting data back from a mustache template

by Richard Jones -
Hi Richard

Seems like an odd way to proceed. If you have a Moodle form why wouldn't you just put your values in there as hidden items? Also I'm not quite sure why you need to use the addElement method here. Does the Mustache template really need to be a part of the form? Couldn't you output the template outside the form element?

Maybe a clearer step back to see what you are trying to achieve overall would be useful?
In reply to Richard Jones

Re: Getting data back from a mustache template

by Richard Bergemann -

Hello Richard,


Thank you for your response.  The template I am using is much like the one used for defining site admins within core moodle.  Its goal is to allow the easy addition/removal/searching of users from specific groups.  This template stores information on the index of the selected row of the list, which I am wanting to use on submit to add/remove a member, or change their role in the group (make them owner for example).  I currently store the javascript needed to update the selected_id variable within the .mustache file to keep the functionality close.  This means however that I cannot update the hidden elements outside of the .mustache file, as far as I know.


As for your question on why I am using the addElement function to add the template file, it is really due to the fact that I haven't really found any other way to add it to the page.  I do not have a custom renderer currently (wasn't able to understand how that implementation worked), so I just generated the html using $OUTPUT->render_from_template().  If there is a better way to add the template to a page, please let me know.  I have not done a crazy amount with moodle development so far, so there is definitely plenty I do not yet understand.

In reply to Richard Bergemann

Re: Getting data back from a mustache template

by Richard Jones -

Hi Richard

Hopefully someone more knowledgeable will hop on to this thread now as I'm probably out of my depth. 

I haven't yet experimented with JavaScript within a Mustache template. (https://docs.moodle.org/dev/Templates#What_if_a_template_contains_javascript.3F).

As for using renderers there's a useful discussion here:

https://moodle.org/mod/forum/discuss.php?d=389713#p1570869

Basically you should have a renderer.php file in your plugin which passes data to the template to render. I have a really simple example of that process here:

https://github.com/richardjonesnz/moodle-tool_simpletool/blob/add_classes/renderer.php

HTH

Richard

Average of ratings: Useful (1)
In reply to Richard Jones

Re: Getting data back from a mustache template

by Richard Bergemann -
Thanks so much for the example! I'll definitely look into that. I know this is now a bit off topic of the original question, but could you explain by chance what the usefulness of using a custom renderer is? instead of just using $OUTPUT?
In reply to Richard Bergemann

Re: Getting data back from a mustache template

by Andreas Grabs -
Picture of Core developers Picture of Peer reviewers Picture of Plugin developers Picture of Translators

Hi Richard,

there a at least two points I think that is very useful.

  1. you can put additional methods into the renderer and you can override methods

  2. Your renderer can be overridden by a theme. That is very important if you want to be more open for other designes.

I hope it claryfies it a bit.

Best regards
Andreas

In reply to Richard Bergemann

Re: Getting data back from a mustache template

by Neill Magill -
Picture of Core developers Picture of Peer reviewers Picture of Plugin developers
It sounds to me like you want to be creating a class that extends user_selector_base

For an example of it in action see the site admin selector code

If that does not work for you because you want it integrated into other form fields then you must defines all the fields you want to use in your moodleform (and not via the html element which is designed only to display information that is not submitted)
Average of ratings: Useful (1)
In reply to Neill Magill

Re: Getting data back from a mustache template

by Richard Bergemann -
Thanks for your response!

I went back and adjusted my javascript to be in a separate file instead of within my template. I also went and defined my variables in my mform instead of within the template. With both of those changes I was then able to use that seperate javascript file to take an id from mustache and update the mforms variables (finding that variable using its name, from as far as I know it doesn't have an id). Thank you for your answer, it helped a ton!