|
| 1 | +# HtmlTag |
| 2 | + |
| 3 | +The `HtmlTag` helper is used to **create the root of a HTML document**, the open |
| 4 | +and close tags for the `<html>` element. |
| 5 | + |
| 6 | +## Basic Usage |
| 7 | + |
| 8 | +```php |
| 9 | +<?= $this->htmlTag(['lang' => 'en'])->openTag() ?> |
| 10 | +<!-- Some HTML --> |
| 11 | +<?= $this->htmlTag()->closeTag() ?> |
| 12 | +``` |
| 13 | + |
| 14 | +Output: |
| 15 | + |
| 16 | +```html |
| 17 | +<html lang="en"> |
| 18 | +<!-- Some HTML --> |
| 19 | +</html> |
| 20 | +``` |
| 21 | + |
| 22 | +## Using Attributes |
| 23 | + |
| 24 | +### Set a single Attribute |
| 25 | + |
| 26 | +```php fct_label="Invoke Usage" |
| 27 | +$this->htmlTag(['lang' => 'en']); |
| 28 | + |
| 29 | +echo $this->htmlTag()->openTag(); // <html lang="en"> |
| 30 | +``` |
| 31 | + |
| 32 | +```php fct_label="Setter Usage" |
| 33 | +$this->htmlTag()->setAttribute('lang', 'en'); |
| 34 | + |
| 35 | +echo $this->htmlTag()->openTag(); // <html lang="en"> |
| 36 | +``` |
| 37 | + |
| 38 | +### Set multiple Attributes |
| 39 | + |
| 40 | +```php fct_label="Invoke Usage" |
| 41 | +$this->htmlTag(['lang' => 'en', 'id' => 'example']); |
| 42 | + |
| 43 | +echo $this->htmlTag()->openTag(); // <html lang="en" id="example"> |
| 44 | +``` |
| 45 | + |
| 46 | +```php fct_label="Setter Usage" |
| 47 | +$this->htmlTag()->setAttributes(['lang' => 'en', 'id' => 'example']); |
| 48 | + |
| 49 | +echo $this->htmlTag()->openTag(); // <html lang="en" id="example"> |
| 50 | +``` |
| 51 | + |
| 52 | +### Get current Value |
| 53 | + |
| 54 | +To get the current value, use the `getAttributes()` method. |
| 55 | + |
| 56 | +```php |
| 57 | +$this->htmlTag(['lang' => 'en', 'id' => 'example']); |
| 58 | + |
| 59 | +var_dump($this->htmlTag()->getAttributes()); // ['lang' => 'en', 'id' => 'example'] |
| 60 | +``` |
| 61 | + |
| 62 | +### Default Value |
| 63 | + |
| 64 | +The default value is an empty `array` that means no attributes are set. |
| 65 | + |
| 66 | +## Using Namespace |
| 67 | + |
| 68 | +The `HtmlTag` helper can automatically add the [XHTML namespace](http://www.w3.org/1999/xhtml/) |
| 69 | +for XHTML documents. To use this functionality, the [`Doctype` helper](doctype.md) |
| 70 | +is used. |
| 71 | + |
| 72 | +The namespace is added only if the document type is set to an XHTML type and use |
| 73 | +is enabled: |
| 74 | + |
| 75 | +```php |
| 76 | +// Set doctype to XHTML |
| 77 | +$this->doctype(Zend\View\Helper\Doctype::XHTML1_STRICT); |
| 78 | + |
| 79 | +// Add namespace to open tag |
| 80 | +$this->htmlTag()->setUseNamespaces(true); |
| 81 | + |
| 82 | +// Output |
| 83 | +echo $this->htmlTag()->openTag(); // <html xmlns="http://www.w3.org/1999/xhtml"> |
| 84 | +``` |
| 85 | + |
| 86 | +### Get current Value |
| 87 | + |
| 88 | +To get the current value, use the `getUseNamespaces()` method. |
| 89 | + |
| 90 | +```php |
| 91 | +$this->htmlTag()->setUseNamespaces(true); |
| 92 | + |
| 93 | +var_dump($this->htmlTag()->getUseNamespaces()); // true |
| 94 | +``` |
| 95 | + |
| 96 | +### Default Value |
| 97 | + |
| 98 | +The default value is `false` that means no namespace is added as attribute. |
0 commit comments