Moodle PHP Documentation 4.5
Moodle 4.5dev (Build: 20240606) (d3ae1391abe)
html_writer Class Reference

Simple html output class. More...

Static Public Member Functions

static alist (array $items, array $attributes=null, $tag='ul')
 Shortcut for quick making of lists.
 
static attribute ($name, $value)
 Outputs a HTML attribute and value.
 
static attributes (array $attributes=null)
 Outputs a list of HTML attributes and values.
 
static checkbox ($name, $value, $checked=true, $label='', array $attributes=null, array $labelattributes=null)
 Generates a simple checkbox with optional label.
 
static div ($content, $class='', array $attributes=null)
 Creates a.
 
static empty_tag ($tagname, array $attributes=null)
 Outputs an empty tag with attributes.
 
static end_div ()
 Ends a.
 
static end_span ()
 Ends a tag.
 
static end_tag ($tagname)
 Outputs a closing tag.
 
static img ($src, $alt, array $attributes=null)
 Generates a simple image tag with attributes.
 
static input_hidden_params (moodle_url $url, array $exclude=null)
 Returns hidden input fields created from url parameters.
 
static label ($text, $for, $colonize=true, array $attributes=array())
 Renders form element label.
 
static link ($url, $text, array $attributes=null)
 Generates a simple html link.
 
static nonempty_tag ($tagname, $contents, array $attributes=null)
 Outputs a tag, but only if the contents are not empty.
 
static random_id ($base='random')
 Generates random html element id.
 
static script ($jscode, $url=null)
 Generate a script tag containing the the specified code.
 
static select (array $options, $name, $selected='', $nothing=array(''=> 'choosedots'), array $attributes=null)
 Generates a simple select form field.
 
static select_time ($type, $name, $currenttime=0, $step=5, array $attributes=null, $timezone=99)
 This is a shortcut for making an hour selector menu.
 
static select_yes_no ($name, $selected=true, array $attributes=null)
 Generates a simple select yes/no form field.
 
static span ($content, $class='', array $attributes=null)
 Creates a tag.
 
static start_div ($class='', array $attributes=null)
 Starts a.
 
static start_span ($class='', array $attributes=null)
 Starts a tag.
 
static start_tag ($tagname, array $attributes=null)
 Outputs an opening tag with attributes.
 
static table (html_table $table)
 Renders HTML table.
 
static tag ($tagname, $contents, array $attributes=null)
 Outputs a tag with attributes and contents.
 

Detailed Description

Simple html output class.

License
http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
Since
Moodle 2.0

Member Function Documentation

◆ alist()

static html_writer::alist ( array $items,
array $attributes = null,
$tag = 'ul' )
static

Shortcut for quick making of lists.

Note: 'list' is a reserved keyword ;-)

Parameters
array$items
array$attributes
string$tagul or ol
Return values
string

◆ attribute()

static html_writer::attribute ( $name,
$value )
static

Outputs a HTML attribute and value.

Parameters
string$nameThe name of the attribute ('src', 'href', 'class' etc.)
string$valueThe value of the attribute. The value will be escaped with s()
Return values
stringHTML fragment

◆ attributes()

static html_writer::attributes ( array $attributes = null)
static

Outputs a list of HTML attributes and values.

Parameters
array$attributesThe tag attributes (array('src' => $url, 'class' => 'class1') etc.) The values will be escaped with s()
Return values
stringHTML fragment

◆ checkbox()

static html_writer::checkbox ( $name,
$value,
$checked = true,
$label = '',
array $attributes = null,
array $labelattributes = null )
static

Generates a simple checkbox with optional label.

Parameters
string$nameThe name of the checkbox
string$valueThe value of the checkbox
bool$checkedWhether the checkbox is checked
string$labelThe label for the checkbox
array$attributesAny attributes to apply to the checkbox
array$labelattributesAny attributes to apply to the label, if present
Return values
stringhtml fragment

◆ div()

static html_writer::div ( $content,
$class = '',
array $attributes = null )
static

Creates a.

tag. (Shortcut function.)
Parameters
string$contentHTML content of tag
string$classOptional CSS class (or classes as space-separated list)
array$attributesOptional other attributes as array
Return values
stringHTML code for div

◆ empty_tag()

static html_writer::empty_tag ( $tagname,
array $attributes = null )
static

Outputs an empty tag with attributes.

Parameters
string$tagnameThe name of tag ('input', 'img', 'br' etc.)
array$attributesThe tag attributes (array('src' => $url, 'class' => 'class1') etc.)
Return values
stringHTML fragment

◆ end_div()

static html_writer::end_div ( )
static

Ends a.

tag. (Shortcut function.)
Return values
stringHTML code for close div tag

◆ end_span()

static html_writer::end_span ( )
static

Ends a tag.

(Shortcut function.)

Return values
stringHTML code for close span tag

◆ end_tag()

static html_writer::end_tag ( $tagname)
static

Outputs a closing tag.

Parameters
string$tagnameThe name of tag ('a', 'img', 'span' etc.)
Return values
stringHTML fragment

◆ img()

static html_writer::img ( $src,
$alt,
array $attributes = null )
static

Generates a simple image tag with attributes.

Parameters
string$srcThe source of image
string$altThe alternate text for image
array$attributesThe tag attributes (array('height' => $max_height, 'class' => 'class1') etc.)
Return values
stringHTML fragment

◆ input_hidden_params()

static html_writer::input_hidden_params ( moodle_url $url,
array $exclude = null )
static

Returns hidden input fields created from url parameters.

Parameters
moodle_url$url
array$excludelist of excluded parameters
Return values
stringHTML fragment

◆ label()

static html_writer::label ( $text,
$for,
$colonize = true,
array $attributes = array() )
static

Renders form element label.

By default, the label is suffixed with a label separator defined in the current language pack (colon by default in the English lang pack). Adding the colon can be explicitly disabled if needed. Label separators are put outside the label tag itself so they are not read by screenreaders (accessibility).

Parameter $for explicitly associates the label with a form control. When set, the value of this attribute must be the same as the value of the id attribute of the form control in the same document. When null, the label being defined is associated with the control inside the label element.

Parameters
string$textcontent of the label tag
string | null$forid of the element this label is associated with, null for no association
bool$colonizeadd label separator (colon) to the label text, if it is not there yet
array$attributesto be inserted in the tab, for example array('accesskey' => 'a')
Return values
stringHTML of the label element

◆ link()

static html_writer::link ( $url,
$text,
array $attributes = null )
static

Generates a simple html link.

Parameters
string | moodle_url$urlThe URL
string$textThe text
array$attributesHTML attributes
Return values
stringHTML fragment

◆ nonempty_tag()

static html_writer::nonempty_tag ( $tagname,
$contents,
array $attributes = null )
static

Outputs a tag, but only if the contents are not empty.

Parameters
string$tagnameThe name of tag ('a', 'img', 'span' etc.)
string$contentsWhat goes between the opening and closing tags
array$attributesThe tag attributes (array('src' => $url, 'class' => 'class1') etc.)
Return values
stringHTML fragment

◆ random_id()

static html_writer::random_id ( $base = 'random')
static

Generates random html element id.

@staticvar int $counter @staticvar string $uniq

Parameters
string$baseA string fragment that will be included in the random ID.
Return values
stringA unique ID

◆ script()

static html_writer::script ( $jscode,
$url = null )
static

Generate a script tag containing the the specified code.

Parameters
string$jscodethe JavaScript code
moodle_url | string$urloptional url of the external script, $code ignored if specified
Return values
stringHTML, the code wrapped in <script> tags.

◆ select()

static html_writer::select ( array $options,
$name,
$selected = '',
$nothing = array('' => 'choosedots'),
array $attributes = null )
static

Generates a simple select form field.

Note this function does HTML escaping on the optgroup labels, but not on the choice labels.

Parameters
array$optionsassociative array value=>label ex.: array(1=>'One, 2=>Two) it is also possible to specify optgroup as complex label array ex.: array(array('Odd'=>array(1=>'One', 3=>'Three)), array('Even'=>array(2=>'Two'))) array(1=>'One', '–1uniquekey'=>array('More'=>array(2=>'Two', 3=>'Three')))
string$namename of select element
string | array$selectedvalue or array of values depending on multiple attribute
array | bool | null$nothingadd nothing selected option, or false of not added
array$attributeshtml select element attributes
Return values
stringHTML fragment

◆ select_time()

static html_writer::select_time ( $type,
$name,
$currenttime = 0,
$step = 5,
array $attributes = null,
$timezone = 99 )
static

This is a shortcut for making an hour selector menu.

Parameters
string$typeThe type of selector (years, months, days, hours, minutes)
string$namefieldname
int$currenttimeA default timestamp in GMT
int$stepminute spacing
array$attributes- html select element attributes
float | int | string$timezonethe timezone to use to calculate the time
Return values
stringHTML fragment

◆ select_yes_no()

static html_writer::select_yes_no ( $name,
$selected = true,
array $attributes = null )
static

Generates a simple select yes/no form field.

Parameters
string$namename of select element
bool$selected
array$attributes- html select element attributes
Return values
stringHTML fragment

◆ span()

static html_writer::span ( $content,
$class = '',
array $attributes = null )
static

Creates a tag.

(Shortcut function.)

Parameters
string$contentHTML content of tag
string$classOptional CSS class (or classes as space-separated list)
array$attributesOptional other attributes as array
Return values
stringHTML code for span

◆ start_div()

static html_writer::start_div ( $class = '',
array $attributes = null )
static

Starts a.

tag. (Shortcut function.)
Parameters
string$classOptional CSS class (or classes as space-separated list)
array$attributesOptional other attributes as array
Return values
stringHTML code for open div tag

◆ start_span()

static html_writer::start_span ( $class = '',
array $attributes = null )
static

Starts a tag.

(Shortcut function.)

Parameters
string$classOptional CSS class (or classes as space-separated list)
array$attributesOptional other attributes as array
Return values
stringHTML code for open span tag

◆ start_tag()

static html_writer::start_tag ( $tagname,
array $attributes = null )
static

Outputs an opening tag with attributes.

Parameters
string$tagnameThe name of tag ('a', 'img', 'span' etc.)
array$attributesThe tag attributes (array('src' => $url, 'class' => 'class1') etc.)
Return values
stringHTML fragment

◆ table()

static html_writer::table ( html_table $table)
static

Renders HTML table.

This method may modify the passed instance by adding some default properties if they are not set yet. If this is not what you want, you should make a full clone of your data before passing them to this method. In most cases this is not an issue at all so we do not clone by default for performance and memory consumption reasons.

Parameters
html_table$tabledata to be rendered
Return values
stringHTML code

◆ tag()

static html_writer::tag ( $tagname,
$contents,
array $attributes = null )
static

Outputs a tag with attributes and contents.

Parameters
string$tagnameThe name of tag ('a', 'img', 'span' etc.)
string$contentsWhat goes between the opening and closing tags
array$attributesThe tag attributes (array('src' => $url, 'class' => 'class1') etc.)
Return values
stringHTML fragment

The documentation for this class was generated from the following file: