HTML Elements
The Nette\Utils\Html class is a helper for generating HTML code that prevents Cross Site Scripting (XSS) vulnerability.
It works in such a way that its objects represent HTML elements, we set their parameters and let them render:
Installation:
All examples assume the following class alias is defined:
Creating an HTML Element
The element is created using the method Html::el()
:
In addition to the name, you can enter other attributes in the HTML syntax:
Or pass them as an associative array to the second parameter:
To change and return an element name:
HTML Attributes
You can set and get individual HTML attributes in three ways, it's up to you who you like more. The first is through the properties:
The second way is to call methods that, in contrast to setting properties, we can chain together:
And the third way is most talkative:
In bulk, attributes can be set with addAttributes(array $attrs)
and deleted with
removeAttributes(array $attrNames)
.
The value of an attribute does not have to be only a string, logical values for logical attributes can also be used:
An attribute can also be an array of tokens, which are listed separated by spaces, which is suitable for CSS classes, for example:
An alternative is an associative array, where the values say whether the key should be listed:
CSS styles can be written in the form of associative arrays:
We have now used properties, but the same can be done using the methods:
Or even in the most talkative way:
One last thing: the method href()
can make it easier to compose query parameters in a URL:
Data Attributes
Data attributes have special support. Because their names contain hyphens, access via properties and methods is not so elegant,
so there is a method data()
:
If the value of the data attribute is an array, it is automatically serialized to JSON:
Element Content
The inner content of the element is set by the setHtml()
or setText()
methods. Use the first one only
if you know that you are reliably passing a secure HTML string in the parameter.
Conversely, the inner content is obtained by methods getHtml()
or getText()
. The second one removes
tags from the HTML output and converts the HTML entities to characters.
Child Nodes
The inner content of an element can also be an array of children. Each of them can be either a string or another
Html
element. They are inserted using addHtml()
or addText()
:
Another way to create and insert a new Html
node:
You can work with nodes as if they were array items. So access the individual ones using square brackets, count them with
count()
and iterate over them:
A new node can be inserted at a specific position using insert(?int $index, $child, bool $replace = false)
. If
$replace = false
, it inserts the element at the $index
position and moves the others. If
$index = null
, it will append an element at the end.
All nodes are returned by method getChildren()
and removed by method removeChildren()
.
Creating a Document Fragment
If you want to work with an array of nodes and are not interested in the wrapping element, you can create a so-called
document fragment by passing null
instead of the element name:
Methods fromHtml()
and fromText()
offer a faster way to create a fragment:
Generating HTML Output
The easiest way to generate an HTML element is to use echo
or cast an object to (string)
. You can
also prints opening or closing tags and attributes separately:
An important feature is the automatic protection against Cross Site Scripting (XSS). All attribute values
or content inserted using setText()
or addText()
is reliably escaped:
Conversion HTML ↔ Text
You can use the static method htmlToText()
to convert HTML to text:
HtmlStringable
The Nette\Utils\Html
object implements the Nette\HtmlStringable
interface, which, for example, Latte
or forms use to distinguish objects that have a method __toString()
that returns HTML code. So double escaping does
not occur if, for example, we print the object in the template using {$el}
.