https://github.com/php-forge/html-helper
Awesome HTML Helper Code Generator for PHP.
https://github.com/php-forge/html-helper
Last synced: 11 months ago
JSON representation
Awesome HTML Helper Code Generator for PHP.
- Host: GitHub
- URL: https://github.com/php-forge/html-helper
- Owner: php-forge
- License: mit
- Created: 2024-02-28T14:01:40.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2024-03-03T11:28:26.000Z (almost 2 years ago)
- Last Synced: 2025-01-17T16:15:03.070Z (about 1 year ago)
- Language: PHP
- Homepage:
- Size: 43.9 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
- Code of conduct: .github/CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
Awesome HTML Helpers Code Generator for PHP.
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 php-forge/html-helper:^0.1
```
or add
```json
"php-forge/html-helper": "^0.1"
```
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 Template
The `Template::class` helper can be used to render a template.
The method accepts two parameters:
- `template:` (string): The template to render.
- `tokenValues:` (array): The token values to replace in the template.
```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