Note:

This site is no longer used and is in read-only mode. Instead please go to our new Moodle Developer Resource site.

html writer: Difference between revisions

From MoodleDocs
m Added ref to file where source code is
m Protected "html writer": Developer Docs Migration ([Edit=Allow only administrators] (indefinite))
 
(6 intermediate revisions by 5 users not shown)
Line 1: Line 1:
Moodle has a class called html_writer which allows you to output basic HTML tags. This is typically used within renderer functions (or by naughty people in scripts which are outputting content without using a renderer).
{{Template:Migrated|newDocId=/docs/apis/core/htmlwriter/}}
= Usage =
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.


There is no documentation for most of this class. Please look at the source code in moodle/lib/outputcomponents.php
There is no documentation for most of this class. Please look at the source code in moodle/lib/outputcomponents.php.


== div, span methods ==
To allow shorter code when outputting the div and span tags with classes, <syntaxhighlight lang="php">div</syntaxhighlight> and <syntaxhighlight lang="php">span</syntaxhighlight> methods were added in Moodle 2.5.
 
= Methods =
== the div method ==


{{Moodle 2.5}}
{{Moodle 2.5}}


To allow shorter code when outputting the div and span tags with classes, <code>div</code> and <code>span</code> methods were added in Moodle 2.5.
<syntaxhighlight lang="php">html_writer::div(content, class="", attributes="");</syntaxhighlight>
 
Attributes is a key-value array.


Example usage:
Example usage:


  $out .= html_writer::div('anonymous');
  $out .= html_writer::div('anonymous'); // &lt;div>anonymous&lt;/div>
  $out .= html_writer::div('kermit', 'frog');
  $out .= html_writer::div('kermit', 'frog'); // &lt;div class="frog">kermit&lt;/div>
  $out .= html_writer::start_span('zombie') . 'BRAINS' . html_writer::end_span();
$out .= html_writer::div('Mr', 'toad', array('id' => 'tophat')); // &lt;div class="toad" id="tophat">Mr&lt;/div>
$out .= html_writer::div('Mr', 'toad', array('id' => 'tophat'));
 
== spans ==
  $out .= html_writer::start_span('zombie') . 'BRAINS' . html_writer::end_span(); // &lt;span class="zombie">BRAINS&lt;/span>
 
== generic tags ==
<syntaxhighlight lang="php">
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);
</syntaxhighlight>


This will result in the following HTML from each line:
= References =
[[html writer | http://xref-diff.mukudu-dev.net/moodle32/lib/outputcomponents.php.html#html_writer]]


&lt;div>anonymous&lt;/div>
[[phpcrossref.com/xref/moodle/lib/outputcomponents.php.html]]
&lt;div class="frog">kermit&lt;/div>
&lt;span class="zombie">BRAINS&lt;/span>
&lt;div class="toad" id="tophat">Mr&lt;/div>


For detailed usage information, see the source code.
[[Category: Development]]

Latest revision as of 09:51, 25 October 2022

Important:

This content of this page has been updated and migrated to the new Moodle Developer Resources. The information contained on the page should no longer be seen up-to-date.

Why not view this page on the new site and help us to migrate more content to the new site!

Usage

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.

There is no documentation for most of this class. Please look at the source code in moodle/lib/outputcomponents.php.

To allow shorter code when outputting the div and span tags with classes,

div

and

span

methods were added in Moodle 2.5.

Methods

the div method

Moodle 2.5


html_writer::div(content, class="", attributes="");

Attributes is a key-value array.

Example usage:

$out .= html_writer::div('anonymous');  // <div>anonymous</div>
$out .= html_writer::div('kermit', 'frog'); // <div class="frog">kermit</div>
$out .= html_writer::div('Mr', 'toad', array('id' => 'tophat')); // <div class="toad" id="tophat">Mr</div>

spans

$out .= 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);

References

http://xref-diff.mukudu-dev.net/moodle32/lib/outputcomponents.php.html#html_writer

phpcrossref.com/xref/moodle/lib/outputcomponents.php.html