HTML Writer API
Moodle has a class called HTML writer which allows you to output basic HTML tags. This is typically used within renderer functions, for example question/type/*pluginname*/renderer.php
.
tip
Please consider using templates as an alternative to the HTML writer.
note
There is no documentation for most of this class. Please read HTML Writer Class Reference for further information.
Methods
div
html_writer::div(content, class="", attributes="");
Example usage:
html_writer::div('anonymous'); // <div>anonymous</div>
html_writer::div('kermit', 'frog'); // <div class="frog">kermit</div>
Attributes can be set by an array with key-value pairs.
html_writer::div('Mr', 'toad', array('id' => 'tophat'));
// <div class="toad" id="tophat">Mr/div>
span
html_writer::start_span('zombie') . 'BRAINS' . html_writer::end_span();
// <span class="zombie">BRAINS</span>
Generic tags
html_writer::tag(tag_name, contents, attributes=null);
html_writer::start_tag(tag_name, attributes=null;);
html_writer::end_tag(tag_name);
html_writer::empty_tag(tag_name, attributes=null);
html_writer::nonempty_tag(tag_name, content, attributes=null);
html_writer::attribute(name, value);
html_writer::attributes(attributes_array);