Integrate Moodle mform with mustache template

Integrate Moodle mform with mustache template

by Dasu Gunathunga -
Number of replies: 2

Hi,

Is there a way to integrate Moodle mform with mustache template. Mform elements generates through mustache templates. However I want to put all the html design into a mustache template. Now I have form classes with html tags like this,

 $mform->addElement('html', "<div class=\"col-sm-6 row\">");
 $mform->addElement('html', "<div class=\"col-sm-4 col-form-label col-label col-border-left\">");
 $mform->addElement('html', "Age:");
 $mform->addElement('html', "</div>");
 $mform->addElement('html', "<div class=\"col-sm-8 col-border-right\">");
 $age = $mform->addElement('text', 'age', '');
 $ageuom = array(  'years' => get_string('years', 'abc'),
                                               'months' => get_string('months', 'abc'),
                                                'weeks' => get_string('weeks', 'abc')
                                             );
 $ageselect = $mform->addElement('select', 'ageuom', '', $ageuom, array());
 $mform->addElement('html', "</div>");
 $mform->addElement('html', "</div>");

Is there a way to separate html tags from php file and put it into mustache template? 

My goal is to separate HTMl design from php file.

Appreciate your help.




Average of ratings: -
In reply to Dasu Gunathunga

Re: Integrate Moodle mform with mustache template

by Dominique Palumbo -
Picture of Particularly helpful Moodlers Picture of Plugin developers
Hi,

I never try it but maybe followinf this doc will help you.

https://docs.moodle.org/dev/Templates

It seems it's not only for theme.

/**
* Render via a template.
*
* @param mywidget $widget
*
* @return string HTML for the page
*/
protected function render_mywidget(mywidget $widget) {
$data = $widget->export_for_template($this);
// Do other logic if needed.
return $this->render_from_template('tool_myplugin/mywidget', $data);
}

Hope it's help.

Dominique.
In reply to Dasu Gunathunga

Re: Integrate Moodle mform with mustache template

by Richard Jones -
Picture of Plugin developers Picture of Testers

Hi Dasu

I'm not entirely sure what you're after but the Moodle Form API does have a method render with produces the html code for the form.  You can pass the return of this directly to a Mustache template rather than use the display method.

$data->form = $mform->render();

for example, where data is the object you are passing to the template. 

If you alternatively just want to pass individual parts of the form to Mustache then I don't know the answer, but I would be interested.

Richard

Average of ratings: Useful (1)