Moodle 2 Javascript output

Moodle 2 Javascript output

by Mark Johnson -
Number of replies: 5
Core developersನ ಚಿತ್ರ Particularly helpful Moodlersನ ಚಿತ್ರ Peer reviewersನ ಚಿತ್ರ Plugin developersನ ಚಿತ್ರ

Moodle 2 comes with a lovely html_writer API for outputting HTML tags, putting and end to potentially messy hand-written and echoed HTML.  It also comes with some lovely new Javascript APIs. However, I'm wondering what the preferred method is for outputting HTML from within Javascript code. Is there an html_writer implementation that I'm yet to find, should we be building DOM nodes at attaching them to the relevant parent, or should we just be echoing HTML as we used to in PHP?

Average of ratings: -
In reply to Mark Johnson

Re: Moodle 2 Javascript output

by Tim Hunt -
Core developersನ ಚಿತ್ರ Documentation writersನ ಚಿತ್ರ Particularly helpful Moodlersನ ಚಿತ್ರ Peer reviewersನ ಚಿತ್ರ Plugin developersನ ಚಿತ್ರ

Use DOM, or the YUI3 DOM wrappers, or output the bulk of HTML in PHP, but hidden, and just releal it using JS.

Average of ratings: Useful (1)
In reply to Tim Hunt

Re: Moodle 2 Javascript output

by Mark Johnson -
Core developersನ ಚಿತ್ರ Particularly helpful Moodlersನ ಚಿತ್ರ Peer reviewersನ ಚಿತ್ರ Plugin developersನ ಚಿತ್ರ

Thanks Tim,

I was thinking mainly about displaying data from AJAX requests as HTML, so DOM manipulation is probably the best option here.

In reply to Mark Johnson

Re: Moodle 2 Javascript output

by Tim Hunt -
Core developersನ ಚಿತ್ರ Documentation writersನ ಚಿತ್ರ Particularly helpful Moodlersನ ಚಿತ್ರ Peer reviewersನ ಚಿತ್ರ Plugin developersನ ಚಿತ್ರ

One example is http://cvs.moodle.org/moodle/user/selector/module.js?view=markup. Look at the output_options function.

Average of ratings: Useful (1)
In reply to Tim Hunt

Re: Moodle 2 Javascript output

by Mark Johnson -
Core developersನ ಚಿತ್ರ Particularly helpful Moodlersನ ಚಿತ್ರ Peer reviewersನ ಚಿತ್ರ Plugin developersನ ಚಿತ್ರ

Great example, thanks. I'd not found Y.Node.create() yet, looks like a handy solution!

In reply to Mark Johnson

Re: Moodle 2 Javascript output

by sam marshall -
Core developersನ ಚಿತ್ರ Peer reviewersನ ಚಿತ್ರ Plugin developersನ ಚಿತ್ರ

Depending on the information you're displaying, it may be appropriate to make the AJAX response return html (as part or all of its return value). That way you can write the html in php.

IMO this approach is quite appropriate for 'displaying extra thing on page' type AJAX requests, especially if code is shared with a non-AJAX version to display the same type of thing. But it's not so appropriate for 'returning data to modify existing page element' type AJAX requests. Opinions might differ...

--sam