class html_writer_support

internal  
 

Utility functions for generating HTML output.

Methods

static string
tag(string $tagname, string $contents, array|null $attributes = null)

Generates a full HTML tag.

static string
start_tag(string $tagname, array|null $attributes = null)

Generates an opening HTML tag.

static string
end_tag(string $tagname)

Generates a closing HTML tag.

static string
empty_tag(string $tagname, array|null $attributes = null)

Generates an empty (self-closing) HTML tag.

static string
nonempty_tag(string $tagname, mixed $contents, array|null $attributes = null)

Generates a non-empty HTML tag, even if contents are empty.

static string
attribute(string $name, mixed $value)

Generates a single HTML attribute.

static string
attributes(array|null $attributes = null)

Generates multiple HTML attributes.

static string
img(moodle_url|string $src, string $alt, array|null $attributes = null)

Generates an HTML img tag.

static string
random_id(string $base = 'random')

Generates a random unique ID.

static string
link(moodle_url|string $url, string $text, array|null $attributes = null)

Generates an HTML anchor (link) tag.

static string
checkbox(string $name, string $value, bool $checked = true, string $label = '', array|null $attributes = null, array|null $labelattributes = null)

Generates an HTML checkbox input.

static string
select_yes_no(string $name, bool $selected = true, array|null $attributes = null)

Generates a yes/no select box.

static string
select(array $options, string $name, array|string $selected = '', array $nothing = ['' => 'choosedots'], array|null $attributes = null, array $disabled = [])

Generates an HTML select box.

static string
select_time(string $type, string $name, int $currenttime = 0, int $step = 5, array|null $attributes = null, float|int $timezone = 99)

Generates date/time selection boxes.

static string
alist(array $items, array|null $attributes = null, string $tag = 'ul')

Generates an HTML list (ul/ol).

static string
input_hidden_params(moodle_url $url, array|null $exclude = null)

Generates hidden input tags for URL parameters.

static string
script(string $jscode, null|moodle_url|string $url = null)

Generates an HTML script tag.

static string
table(html_table $table)

Generates an HTML table.

static string
label(string $text, string|null $for, bool $colonize = true, array $attributes = [])

Generates an HTML label tag.

static string
div(string $content, string $class = '', array|null $attributes = null)

Generates an HTML div tag.

static string
start_div(string $class = '', array|null $attributes = null)

Generates an opening HTML div tag.

static string
end_div()

Generates a closing HTML div tag.

static string
span(string $content, string $class = '', array|null $attributes = null)

Generates an HTML span tag.

static string
start_span(string $class = '', array|null $attributes = null)

Generates an opening HTML span tag.

static string
end_span()

Generates a closing HTML span tag.

static string
html_to_text(string $html)

Converts HTML content to plain text.

Details

at line 40
static string tag(string $tagname, string $contents, array|null $attributes = null)

Generates a full HTML tag.

Parameters

string $tagname

Tag name

string $contents

The tag contents

array|null $attributes

Optional tag attributes

Return Value

string

the generated HTML tag

at line 53
static string start_tag(string $tagname, array|null $attributes = null)

Generates an opening HTML tag.

Parameters

string $tagname

Tag name

array|null $attributes

Optional tag attributes

Return Value

string

the generated opening tag

at line 65
static string end_tag(string $tagname)

Generates a closing HTML tag.

Parameters

string $tagname

Tag name

Return Value

string

the generated closing tag

at line 78
static string empty_tag(string $tagname, array|null $attributes = null)

Generates an empty (self-closing) HTML tag.

Parameters

string $tagname

Tag name

array|null $attributes

Optional tag attributes

Return Value

string

the generated empty tag

at line 92
static string nonempty_tag(string $tagname, mixed $contents, array|null $attributes = null)

Generates a non-empty HTML tag, even if contents are empty.

Parameters

string $tagname

Tag name

mixed $contents

The tag contents

array|null $attributes

Optional tag attributes

Return Value

string

the generated HTML tag

at line 105
static string attribute(string $name, mixed $value)

Generates a single HTML attribute.

Parameters

string $name

Attribute name

mixed $value

Attribute value

Return Value

string

the generated HTML attribute

at line 117
static string attributes(array|null $attributes = null)

Generates multiple HTML attributes.

Parameters

array|null $attributes

Map of attribute names to values

Return Value

string

the generated HTML attributes

at line 131
static string img(moodle_url|string $src, string $alt, array|null $attributes = null)

Generates an HTML img tag.

Parameters

moodle_url|string $src

The image source URL

string $alt

The image alt text

array|null $attributes

Optional tag attributes

Return Value

string

the generated img tag

at line 143
static string random_id(string $base = 'random')

Generates a random unique ID.

Parameters

string $base

the ID base prefix

Return Value

string

the generated ID

Generates an HTML anchor (link) tag.

Parameters

moodle_url|string $url

The link destination

string $text

The link text

array|null $attributes

Optional tag attributes

Return Value

string

the generated anchor tag

at line 174
static string checkbox(string $name, string $value, bool $checked = true, string $label = '', array|null $attributes = null, array|null $labelattributes = null)

Generates an HTML checkbox input.

Parameters

string $name

The input name

string $value

The input value

bool $checked

Whether the checkbox is checked

string $label

The checkbox label text

array|null $attributes

Optional tag attributes

array|null $labelattributes

Optional label tag attributes

Return Value

string

the generated checkbox HTML

at line 194
static string select_yes_no(string $name, bool $selected = true, array|null $attributes = null)

Generates a yes/no select box.

Parameters

string $name

The input name

bool $selected

Whether 'yes' is selected

array|null $attributes

Optional tag attributes

Return Value

string

the generated select HTML

at line 211
static string select(array $options, string $name, array|string $selected = '', array $nothing = ['' => 'choosedots'], array|null $attributes = null, array $disabled = [])

Generates an HTML select box.

Parameters

array $options

Map of option values to labels

string $name

The input name

array|string $selected

Selected value(s)

array $nothing

Value for the 'nothing selected' option

array|null $attributes

Optional tag attributes

array $disabled

Disabled option values

Return Value

string

the generated select HTML

at line 234
static string select_time(string $type, string $name, int $currenttime = 0, int $step = 5, array|null $attributes = null, float|int $timezone = 99)

Generates date/time selection boxes.

Parameters

string $type

The selection type (e.g., 'days', 'months', 'years').

string $name

The input name

int $currenttime

The current timestamp

int $step

The increment step

array|null $attributes

Optional tag attributes

float|int $timezone

The timezone offset

Return Value

string

the generated select HTML

at line 254
static string alist(array $items, array|null $attributes = null, string $tag = 'ul')

Generates an HTML list (ul/ol).

Parameters

array $items

List of items

array|null $attributes

Optional tag attributes

string $tag

The list tag (ul or ol)

Return Value

string

the generated list HTML

at line 267
static string input_hidden_params(moodle_url $url, array|null $exclude = null)

Generates hidden input tags for URL parameters.

Parameters

moodle_url $url

The URL containing parameters

array|null $exclude

Parameter names to exclude

Return Value

string

the generated hidden input tags

at line 280
static string script(string $jscode, null|moodle_url|string $url = null)

Generates an HTML script tag.

Parameters

string $jscode

the JavaScript code

null|moodle_url|string $url

the script source URL

Return Value

string

the generated script tag

at line 292
static string table(html_table $table)

Generates an HTML table.

Parameters

html_table $table

the table object

Return Value

string

the generated table HTML

at line 307
static string label(string $text, string|null $for, bool $colonize = true, array $attributes = [])

Generates an HTML label tag.

Parameters

string $text

Label text

string|null $for

ID of the associated input

bool $colonize

whether to add a colon after the text

array $attributes

Optional tag attributes

Return Value

string

the generated label tag

at line 321
static string div(string $content, string $class = '', array|null $attributes = null)

Generates an HTML div tag.

Parameters

string $content

The div contents

string $class

The CSS class

array|null $attributes

Optional tag attributes

Return Value

string

the generated div tag

at line 334
static string start_div(string $class = '', array|null $attributes = null)

Generates an opening HTML div tag.

Parameters

string $class

The CSS class

array|null $attributes

Optional tag attributes

Return Value

string

the generated opening div tag

at line 344
static string end_div()

Generates a closing HTML div tag.

Return Value

string

the generated closing div tag

at line 358
static string span(string $content, string $class = '', array|null $attributes = null)

Generates an HTML span tag.

Parameters

string $content

The span contents

string $class

The CSS class

array|null $attributes

Optional tag attributes

Return Value

string

the generated span tag

at line 371
static string start_span(string $class = '', array|null $attributes = null)

Generates an opening HTML span tag.

Parameters

string $class

The CSS class

array|null $attributes

Optional tag attributes

Return Value

string

the generated opening span tag

at line 381
static string end_span()

Generates a closing HTML span tag.

Return Value

string

the generated closing span tag

at line 393
static string html_to_text(string $html)

Converts HTML content to plain text.

Parameters

string $html

the HTML content

Return Value

string

the converted plain text