Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/ui-awesome/html-helper

UI Awesome HTML Helper Code Generator for PHP.
https://github.com/ui-awesome/html-helper

List: html-helper

html-helper php ui-awesome

Last synced: about 2 months ago
JSON representation

UI Awesome HTML Helper Code Generator for PHP.

Awesome Lists containing this project

README

        





UI Awesome HTML Helpers Code Generator for PHP.





PHPUnit


Codecov


Infection


Psalm


Psalm Coverage


Style ci

HTML Helper is a PHP library that simplifies the creation of HTML elements. It provides a set of classes to generate
HTML attributes, encode content, sanitize HTML content, and more.

## Installation

The preferred way to install this extension is through [composer](https://getcomposer.org/download/).

Either run

```shell
composer require --prefer-dist ui-awesome/html-helper:^0.2
```

or add

```json
"ui-awesome/html-helper": "^0.2"
```

to the require section of your `composer.json` file.

## Usage

### Add CSS classes

The `CssClasses::class` helper can be used to add CSS classes to an HTML element.

The method accepts three parameters:

- `attributes:` (array): The HTML attributes of the element.
- `classes:` (array|string): The CSS classes to add.
- `overwrite:` (bool): Whether to overwrite the `class` attribute or not. For default, it is `false`.

```php
attributes, ['btn', 'btn-primary', 'btn-lg']);
```

Overwriting the `class` attribute:

```php
'btn'];

$classes = CssClasses::add($this->attributes, ['btn-primary', 'btn-lg'], true);
```

### Convert regular expression to pattern

The `Utils::class` helper can be used to normalize a regular expression.

The method accepts one parameter:

- `regexp:` (string): The pattern to normalize.
- `delimiter:` (string): The delimiter to use. For default, it is `null`.

```php
alert("Hello, World!")');
```

### Encode value

The `Encode::class` helper can be used to encode HTML value.

The method accepts tree parameters:

- `value:` (string): The value to encode.
- `doubleEncode:` (bool): Whether to double encode the value or not. For default, it is `true`.
- `charset:` (string): The charset to use. For default, it is `UTF-8`.

```php
alert("Hello, World!")');
```

### Get short class name

The `Utils::class` helper can be used to get the short class name.

The method accepts one parameter:

- `class:` (string): The class name to get the short name.
- `suffix:` (string): Whether to append the `::class` suffix to the class name.
For default, it is `true`. If it is `false`, the method will return the short name without the `::class` suffix.
- `lowercase:` (bool): Whether to convert the class name to lowercase or not.
For default, it is `false`.

```php
alert("Hello, World!")');
```

### Render HTML attributes

The `Attributes::class` helper can be used to `render` HTML attributes in a programmatic way.

```php
'btn btn-primary',
'id' => 'submit-button',
'disabled' => true,
]
);
```

### Render HMTL class attribute

The `CssClass::class` helper can be used to render the `class` attribute.

The method accepts one parameter:

- `class:` (string): The class to render.
- `baseClass:` (string): The base class to use.
- `inList:` (array): The list of classes to validate.

```php
'prefix',
'{{tag}}' => '

content
',
'{{suffix}}' => 'suffix',
];

$content = Template::render($template, $tokenValues);
```

> `\n` is a new line character, and it is used to separate the tokens in the template.

### Validate value in list

The `Validator::class` helper can be used to validate a value in a list.

The method accepts tree parameters:

- `value:` (mixed): The value to validate.
- `exceptionMessage:` (string): The exception message to throw if the value is not in the list.
- `list:` (...string): The list to validate the value.

```php