All Projects → yiisoft → html

yiisoft / html

Licence: BSD-3-Clause license
Handy library to generate HTML

Programming Languages

PHP
23972 projects - #3 most used programming language

Projects that are alternatives of or similar to html

db-oracle
Oracle Database support for Yii
Stars: ✭ 21 (-50%)
Mutual labels:  yii3
yii-console
Yii console components
Stars: ✭ 48 (+14.29%)
Mutual labels:  yii3
view-twig
Yii View Twig Renderer
Stars: ✭ 24 (-42.86%)
Mutual labels:  yii3
yii-gii
Yii code generator extension
Stars: ✭ 27 (-35.71%)
Mutual labels:  yii3
strings
String helper methods and an inflector
Stars: ✭ 31 (-26.19%)
Mutual labels:  yii3
app-api
API application project template
Stars: ✭ 44 (+4.76%)
Mutual labels:  yii3
auth-jwt
www.yiiframework.com/
Stars: ✭ 28 (-33.33%)
Mutual labels:  yii3
data
Data providers
Stars: ✭ 31 (-26.19%)
Mutual labels:  yii3
var-dumper
Helper for dumping variable for debug purposes
Stars: ✭ 13 (-69.05%)
Mutual labels:  yii3
router
Router is a request matcher and URL generator
Stars: ✭ 38 (-9.52%)
Mutual labels:  yii3
yii-masked-input
Yii Framework Masked input widget Extension
Stars: ✭ 38 (-9.52%)
Mutual labels:  yii3
demo
Yii 3 demo application
Stars: ✭ 259 (+516.67%)
Mutual labels:  yii3
session
A session service, PSR-15 session middleware, and a flash message service which helps use one-time messages.
Stars: ✭ 14 (-66.67%)
Mutual labels:  yii3
files
Useful methods to manage files and directories
Stars: ✭ 27 (-35.71%)
Mutual labels:  yii3
yii-debug
Yii debug panel extension
Stars: ✭ 23 (-45.24%)
Mutual labels:  yii3
cache
PSR-16 compatible cache library
Stars: ✭ 30 (-28.57%)
Mutual labels:  yii3
db-sqlite
SQLite support for Yii
Stars: ✭ 15 (-64.29%)
Mutual labels:  yii3
data-response
www.yiiframework.com/
Stars: ✭ 12 (-71.43%)
Mutual labels:  yii3
db-redis
Yii DBAL Redis connection
Stars: ✭ 14 (-66.67%)
Mutual labels:  yii3
mutex
Mutex lock implementation
Stars: ✭ 28 (-33.33%)
Mutual labels:  yii3

Yii HTML


Latest Stable Version Total Downloads Build Status Scrutinizer Code Quality Code Coverage Mutation testing badge static analysis psalm-level type-coverage

The package provides various tools to help with dynamic server-side generation of HTML:

  • Tag classes A, Address, Article, Aside, Audio, B, Body, Br, Button, Caption, Col, Colgroup, Datalist, Div, Em, Fieldset, Footer, Form, H1, H2, H3, H4, H5, H6, Header, Hgroup, I, Img, Input (and specialized Checkbox, Radio, Range, File), Label, Legend, Li, Link, Meta, Nav, Noscript, Ol, Optgroup, Option, P, Picture, Script, Section, Select, Small, Source, Span, Strong, Style, Table, Tbody, Td, Textarea, Tfoot, Th, Thead, Title, Tr, Track, Ul, Video.
  • CustomTag class that helps to generate custom tag with any attributes.
  • HTML widgets ButtonGroup, CheckboxList and RadioList.
  • All tags content is automatically HTML-encoded. There is NoEncode class designed to wrap content that should not be encoded.
  • Html helper that has static methods to generate HTML, create tags and HTML widget objects.

Note that for simple static-HTML cases, it is preferred to use HTML directly.

Requirements

  • PHP 8.0 or higher.

Installation

composer require yiisoft/html

General usage

<?php

use Yiisoft\Html\Html;
use Yiisoft\Html\Tag\Meta;

?>

<?= Meta::pragmaDirective('X-UA-Compatible', 'IE=edge') ?>
<?= Meta::data('viewport', 'width=device-width, initial-scale=1') ?>

<?= Html::cssFile(
    'https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css',
    [
        'integrity' => 'sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T',
        'crossorigin' => 'anonymous'
    ]
) ?>
<?= Html::cssFile('/css/site.css', ['rel' => 'stylesheet']) ?>

<?= Html::openTag('footer', ['class' => 'footer']) ?>
    <?= Html::openTag('div', ['class' => 'container flex-fill']) ?>
        <?= Html::p('', ['class' => 'float-left']) ?>
        <?= Html::p()
            ->replaceClass('float-right')
            ->content(
                'Powered by ',
                Html::a(
                    'Yii Framework',
                    'https://www.yiiframework.com/',
                    ['rel' => 'external']
                )
            ) ?>
    <?= Html::closeTag('div') ?>
<?= Html::closeTag('footer') ?>

Tag objects usage

Tag classes allow working with a tag as an object and then get an HTML code by using render() method or type casting to string. For example, the following code:

echo \Yiisoft\Html\Tag\Div::tag()
    ->content(
        \Yiisoft\Html\Tag\A::tag()
            ->mailto('[email protected]')
            ->content('contact us')
            ->render()
    )
    ->encode(false)
    ->id('ContactEmail')
    ->replaceClass('red');

... will generate the following HTML:

<div id="ContactEmail" class="red"><a href="mailto:[email protected]">contact us</a></div>

Generating custom tags

To generate custom tags, use the CustomTag class. For example, the following code:

echo \Yiisoft\Html\Tag\CustomTag::name('b')
    ->content('text')
    ->attribute('title', 'Important');

... will generate the following HTML:

<b title="Important">text</b>

Encoding tags content

By default, stringable objects that implement \Yiisoft\Html\NoEncodeStringableInterface are not encoded, everything else is encoded.

To change this behavior use encode() method passing one of the following values:

  • null: default behavior;
  • true: any content is encoded;
  • false: nothing is encoded.

Note: all bundled tags and widgets implement \Yiisoft\Html\NoEncodeStringableInterface interface and are not encoded by default when passed as content. Their own content is encoded.

Examples:

// <b>&lt;i&gt;hello&lt;/i&gt;</b>
echo Html::b('<i>hello</i>');

// <b><i>hello</i></b>
echo Html::b('<i>hello</i>')->encode(false);

// <b><i>hello</i></b>
echo Html::b(Html::i('hello'));

// <b>&lt;i&gt;hello&lt;/i&gt;</b>
echo Html::b(Html::i('hello'))->encode(true);

In order to mark a string as "do not encode" you can use \Yiisoft\Html\NoEncode class:

// <b><i>hello</i></b>
echo Html::b(NoEncode::string('<i>hello</i>'));

HTML widgets usage

There are multiple widgets that do not directly represent any HTML tag, but a set of tags. These help to express complex HTML in simple PHP.

ButtonGroup

Represents a group of buttons.

echo \Yiisoft\Html\Widget\ButtonGroup::create()
	->buttons(
	    \Yiisoft\Html\Html::resetButton('Reset Data'),
	    \Yiisoft\Html\Html::resetButton('Send'),
	)
	->containerAttributes(['class' => 'actions'])
	->buttonAttributes(['form' => 'CreatePost']);

Result will be:

<div class="actions">
<button type="reset" form="CreatePost">Reset Data</button>
<button type="reset" class="primary" form="CreatePost">Send</button>
</div>

CheckboxList

Represents a list of checkboxes.

echo \Yiisoft\Html\Widget\CheckboxList\CheckboxList::create('count')
	->items([1 => 'One', 2 => 'Two', 5 => 'Five'])
	->uncheckValue(0)
	->value(2, 5)
	->containerAttributes(['id' => 'main']);

Result will be:

<input type="hidden" name="count" value="0">
<div id="main">
<label><input type="checkbox" name="count[]" value="1"> One</label>
<label><input type="checkbox" name="count[]" value="2" checked> Two</label>
<label><input type="checkbox" name="count[]" value="5" checked> Five</label>
</div>

RadioList

Represents a list of radio buttons.

echo \Yiisoft\Html\Widget\RadioList\RadioList::create('count')
    ->items([1 => 'One', 2 => 'Two', 5 => 'Five'])
    ->uncheckValue(0)
    ->value(2)
    ->containerAttributes(['id' => 'main'])
    ->render();

Result will be:

<input type="hidden" name="test" value="0">
<div id="main">
<label><input type="radio" name="test" value="1"> One</label>
<label><input type="radio" name="test" value="2" checked> Two</label>
<label><input type="radio" name="test" value="5"> Five</label>
</div>

Html helper usage

Html helper methods are static so usage is:

echo \Yiisoft\Html\Html::a('Yii Framework', 'https://www.yiiframework.com/');

Overall the helper has the following method groups.

Creating tag objects

Custom tags

  • tag
  • normalTag
  • voidTag

Base tags

  • b
  • div
  • em
  • i
  • meta
  • p
  • br
  • script
  • noscript
  • span
  • strong
  • small
  • style
  • title

Media tags

  • img
  • picture
  • audio
  • video
  • track
  • source

Heading tags

  • h1
  • h2
  • h3
  • h4
  • h5
  • h6

Section tags

  • body
  • article
  • section
  • nav
  • aside
  • hgroup
  • header
  • footer
  • address

List tags

  • ul
  • ol
  • li

Hyperlink tags

  • a
  • mailto

Link tags

  • link
  • cssFile
  • javaScriptFile

Form tags

  • button
  • buttonInput
  • checkbox
  • file
  • datalist
  • fieldset
  • fileInput
  • form
  • hiddenInput
  • input
  • label
  • legend
  • optgroup
  • option
  • passwordInput
  • radio
  • resetButton
  • resetInput
  • select
  • submitButton
  • submitInput
  • textInput
  • textarea

Table tags

  • table
  • caption
  • colgroup
  • col
  • thead
  • tbody
  • tfoot
  • tr
  • th
  • td

Generating tag parts

  • openTag
  • closeTag
  • renderTagAttributes

Creating HTML widget objects

  • radioList
  • checkboxList

Working with tag attributes

  • generateId
  • getArrayableName
  • getNonArrayableName
  • normalizeRegexpPattern

Encode and escape special characters

  • encode
  • encodeAttribute
  • encodeUnquotedAttribute
  • escapeJavaScriptStringValue

Working with CSS styles and classes

  • addCssStyle
  • removeCssStyle
  • addCssClass
  • removeCssClass
  • cssStyleFromArray
  • cssStyleToArray

Testing

Unit testing

The package is tested with PHPUnit. To run tests:

./vendor/bin/phpunit

Mutation testing

The package tests are checked with Infection mutation framework with Infection Static Analysis Plugin. To run it:

./vendor/bin/roave-infection-static-analysis-plugin

Static analysis

The code is statically analyzed with Psalm. To run static analysis:

./vendor/bin/psalm

License

The Yii HTML is free software. It is released under the terms of the BSD License. Please see LICENSE for more information.

Maintained by Yii Software.

Support the project

Open Collective

Follow updates

Official website Twitter Telegram Facebook Slack

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].