Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/italystrap/html
HTML tag and attributes generator in PHP
https://github.com/italystrap/html
php php-library wordpress-php-library
Last synced: about 1 month ago
JSON representation
HTML tag and attributes generator in PHP
- Host: GitHub
- URL: https://github.com/italystrap/html
- Owner: ItalyStrap
- License: mit
- Created: 2019-12-09T08:34:52.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2020-03-06T10:49:53.000Z (almost 5 years ago)
- Last Synced: 2024-11-07T02:07:11.613Z (about 2 months ago)
- Topics: php, php-library, wordpress-php-library
- Language: PHP
- Homepage:
- Size: 248 KB
- Stars: 1
- Watchers: 2
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# ItalyStrap HTML API
[![Build Status](https://travis-ci.org/ItalyStrap/html.svg?branch=master)](https://travis-ci.org/ItalyStrap/html)
[![Latest Stable Version](https://img.shields.io/packagist/v/italystrap/html.svg)](https://packagist.org/packages/italystrap/html)
[![Total Downloads](https://img.shields.io/packagist/dt/italystrap/html.svg)](https://packagist.org/packages/italystrap/html)
[![Latest Unstable Version](https://img.shields.io/packagist/vpre/italystrap/html.svg)](https://packagist.org/packages/italystrap/html)
[![License](https://img.shields.io/packagist/l/italystrap/html.svg)](https://packagist.org/packages/italystrap/html)
![PHP from Packagist](https://img.shields.io/packagist/php-v/italystrap/html)PHP HTML handler the OOP way
## Table Of Contents
* [Installation](#installation)
* [Basic Usage](#basic-usage)
* [Advanced Usage](#advanced-usage)
* [Contributing](#contributing)
* [License](#license)## Installation
The best way to use this package is through Composer:
```CMD
composer require italystrap/html
```## Basic Usage
### Attributes Class
```php
use ItalyStrap\HTML\AttributesInterface;$sut = new AttributesInterface();
$sut->add( 'context', [
'class' => 'color-primary',
'id' => 'unique_id',
] );// ' class="color-primary" id="unique_id"'
echo $sut->render( 'context' );$sut->add( 'another_context', [
'class' => '', // This will be skipped because empty
'attr1' => null, // This will be skipped because null
'attr2' => false, // This will be skipped because false
'attr3' => 0, // This will be skipped because 0 is also false
'id' => 'unique_id',
] );
// ' id="unique_id"'
echo $sut->render( 'another_context' );
```Attributes can be also used with the `get_attr()` and `get_attr_e()` helpers functions under the same namespece.
```php
use function ItalyStrap\HTML\{get_attr, get_attr_e};// Return ' class="someClass"'
$attr = get_attr( 'context', ['class' => 'someClass'] );// Echo ' class="someClass"'
get_attr_e( 'context', ['class' => 'someClass'] );
```### `ItalyStrap\HTML\get_attr()`
Build list of attributes into a string and apply contextual filter on string:
```php
use function ItalyStrap\HTML\{get_attr, get_attr_e};$attr = [
'id' => 'unique_id',
'class' => 'some_class',
];$output = get_attr( $context, $attr, false );
// id="unique_id" class="some_class"
printf(
'Title',
$output
);
```or
```html
>Title
``````php
// Title
``````php
use function ItalyStrap\HTML\{open_tag, close_tag, open_tag_e, close_tag_e};\ItalyStrap\HTML\Tag::$is_debug = false; // If you don't want tu print debug comments
$open = \ItalyStrap\HTML\open_tag( 'test', 'div', [ 'class' => 'btn-primary' ] );
$this->assertStringContainsString( '', $open, '' );', $closed, '' );
$closed = \ItalyStrap\HTML\close_tag( 'test' );
$this->assertStringContainsString( '\ItalyStrap\HTML\Tag::$is_debug = false;
\ItalyStrap\HTML\open_tag_e( 'test', 'div', [ 'class' => 'btn-primary' ] );
echo 'Content';
\ItalyStrap\HTML\close_tag_e( 'test' );$this->expectOutputString( '
Content' );
```### Tag Class
```php
use ItalyStrap\HTML\{Tag,AttributesInterface};Tag::$is_debug = true; // This will print comment around the output for debugging, you can see it with ctrl + u key in the browser
$sut = new Tag( new AttributesInterface() );//
Some content inside HTML div tags
echo $sut->open( 'some_context', 'div', [ 'class' => 'someClass' ] );
echo 'Some content inside HTML div tags';
echo $sut->close( 'some_context' );//
echo $sut->void( 'some_other_context', 'input', [ 'type' => 'text' ] );
```### Filters
```php
use ItalyStrap\HTML\{Tag,AttributesInterface};$context = 'some_context';
\add_filter("italystrap_{$context}_tag", function( string $tag, string $context, Tag $obj) {
// Do some staff with $tag
$new_tag = 'span';
return $new_tag;
}, 10, 3);$sut = new Tag( new AttributesInterface() );
echo $sut->open( 'some_context', 'div', [ 'class' => 'someClass' ] );
echo 'Some content inside HTML div tags';
echo $sut->close( 'some_context' );// Some content inside HTML div tags
```## Advanced Usage
See in [tests](tests) folder for more advance usage.
## Contributing
All feedback / bug reports / pull requests are welcome.
## License
Copyright (c) 2019 Enea Overclokk, ItalyStrap
This code is licensed under the [MIT](LICENSE).
## Notes
* Maintained under the [Semantic Versioning Guide](http://semver.org)
## Credits
> TODO