Skip to content

Commit bf6ab4d

Browse files
committed
initial commit
1 parent 7dca0ff commit bf6ab4d

21 files changed

Lines changed: 4348 additions & 2 deletions

.github/codecov.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
codecov:
2+
require_ci_to_pass: yes
3+
4+
coverage:
5+
range: "80...100"
6+
7+
comment: false

.github/dependabot.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
version: 2
2+
updates:
3+
- package-ecosystem: composer
4+
directory: "/"
5+
schedule:
6+
interval: monthly
7+
open-pull-requests-limit: 10
8+
- package-ecosystem: github-actions
9+
directory: "/"
10+
schedule:
11+
interval: monthly
12+
open-pull-requests-limit: 10

.github/workflows/ci.yml

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
name: CI
2+
3+
on:
4+
push:
5+
branches:
6+
- 'main'
7+
pull_request:
8+
branches:
9+
- '*'
10+
workflow_dispatch:
11+
12+
permissions:
13+
contents: read # to fetch code (actions/checkout)
14+
15+
jobs:
16+
testsuite-linux:
17+
runs-on: ubuntu-22.04
18+
strategy:
19+
fail-fast: false
20+
matrix:
21+
php-version: ['8.1', '8.2']
22+
dependencies: ['highest']
23+
include:
24+
- php-version: '8.1'
25+
dependencies: 'lowest'
26+
- php-version: '8.2'
27+
dependencies: 'highest'
28+
29+
steps:
30+
- uses: actions/checkout@v4
31+
32+
- name: Setup PHP
33+
uses: shivammathur/setup-php@v2
34+
with:
35+
php-version: ${{ matrix.php-version }}
36+
extensions: mbstring, intl
37+
ini-values: zend.assertions=1
38+
coverage: pcov
39+
40+
- name: Composer install
41+
uses: ramsey/composer-install@v2
42+
with:
43+
dependency-versions: ${{ matrix.dependencies }}
44+
composer-options: ${{ matrix.composer-options }}
45+
46+
- name: Run PHPUnit
47+
run: |
48+
if [[ ${{ matrix.php-version }} == '8.2' ]]; then
49+
vendor/bin/phpunit --coverage-clover=coverage.xml
50+
else
51+
vendor/bin/phpunit
52+
fi
53+
54+
- name: Code Coverage Report
55+
if: success() && matrix.php-version == '8.2'
56+
uses: codecov/codecov-action@v3
57+
58+
cs-stan:
59+
name: Coding Standard & Static Analysis
60+
runs-on: ubuntu-22.04
61+
62+
steps:
63+
- uses: actions/checkout@v4
64+
65+
- name: Setup PHP
66+
uses: shivammathur/setup-php@v2
67+
with:
68+
php-version: '8.2'
69+
extensions: mbstring, intl
70+
coverage: none
71+
tools: cs2pr
72+
73+
- name: Composer Install
74+
run: composer stan-setup
75+
76+
- name: Run phpcs
77+
run: vendor/bin/phpcs --report=checkstyle src/ tests/ | cs2pr
78+
79+
- name: Run psalm
80+
run: vendor/bin/psalm.phar --output-format=github
81+
82+
- name: Run phpstan
83+
if: always()
84+
run: vendor/bin/phpstan.phar analyse --error-format=github

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/vendor
2+
/coverage
3+
/.phpunit.cache

README.md

Lines changed: 170 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,170 @@
1-
# custom-html-elements
2-
Allows you to create custom HTML elements to render more complex template parts
1+
# Custom HTML Elements
2+
Allows you to create custom HTML elements to render more complex template parts with a simpler HTML representation
3+
4+
## Requirements
5+
6+
* PHP 8.1+
7+
8+
## Installation
9+
10+
```shell
11+
composer require lordsimal/custom-html-elements
12+
```
13+
14+
## Usage
15+
16+
This is an example representation of a custom HTML element you want to use:
17+
18+
```html
19+
<c-youtube src="RLdsCL4RDf8"/>
20+
```
21+
22+
So this would appear in a HTML output like this:
23+
24+
```html
25+
<!DOCTYPE html>
26+
<html lang="en">
27+
<head>
28+
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
29+
<title>Example Page</title>
30+
<meta name="author" content="Kevin Pfeifer">
31+
</head>
32+
<body>
33+
<c-youtube src="RLdsCL4RDf8"/>
34+
</body>
35+
</html>
36+
```
37+
38+
To render this custom HTML element you need to do this:
39+
40+
```php
41+
$htmlOutput = ''; // This variable represents what is shown above
42+
$me = new \LordSimal\CustomHtmlElements\TagEngine([]
43+
'parse_on_shutdown' => true,
44+
'tag_directories' => [
45+
__DIR__.DIRECTORY_SEPARATOR.'Tags'.DIRECTORY_SEPARATOR,
46+
__DIR__.DIRECTORY_SEPARATOR.'OtherTagsFolder'.DIRECTORY_SEPARATOR,
47+
],
48+
'sniff_for_buried_tags' => true
49+
]);
50+
echo $me->parse($htmlOutput);
51+
```
52+
53+
The element logic can be placed in e.g. `Tags/Youtube.php` or `OtherTagsFolder/Youtube.php`:
54+
55+
```php
56+
<?php
57+
namespace App\Tags;
58+
59+
use LordSimal\CustomHtmlElements\CustomTag;
60+
61+
class Youtube extends CustomTag
62+
{
63+
public static string $tag = 'c-youtube';
64+
65+
public function render(): string
66+
{
67+
return <<< HTML
68+
<iframe width="560" height="315"
69+
src="https://www.youtube.com/embed/{$this->src}"
70+
allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"
71+
allowfullscreen>
72+
</iframe>
73+
HTML;
74+
}
75+
}
76+
```
77+
78+
As you can see the main 2 parts are the
79+
80+
```php
81+
public static string $tag = 'c-youtube';
82+
```
83+
84+
which defines what HTML tag is represented by this class and the `render()` method.
85+
86+
Inside the `render()` method you have access to all HTML attributes which you pass onto your element.
87+
88+
So e.g.
89+
90+
```html
91+
<c-button type="primary" text="Click me" url="/something/stupid" />
92+
```
93+
94+
would be accessible inside the `Button` class via
95+
96+
```php
97+
class Button extends CustomTag
98+
{
99+
public static string $tag = 'c-button';
100+
101+
public function render(): string
102+
{
103+
$classes = ['c-button'];
104+
if ($this->type == 'primary') {
105+
$classes[] = 'c-button--primary';
106+
}
107+
$classes = implode(' ', $classes);
108+
return <<< HTML
109+
<a href="$this->url" class="$classes">$this->text</a>
110+
HTML;
111+
}
112+
}
113+
```
114+
115+
## Accessing the inner content
116+
117+
You may want to create custom HTML elements like
118+
```html
119+
<c-github>Inner Content</c-github>
120+
```
121+
122+
To access the `Inner Content` text inside your class you simply have to call `$this->content` like so
123+
124+
```php
125+
class Github extends CustomTag
126+
{
127+
public static string $tag = 'c-github';
128+
129+
public function render(): string
130+
{
131+
return <<< HTML
132+
$this->content
133+
HTML;
134+
}
135+
}
136+
```
137+
138+
## Self closing elements
139+
140+
By default every custom HTML element can be used either way:
141+
142+
```html
143+
<c-youtube src="RLdsCL4RDf8"></c-youtube>
144+
```
145+
or
146+
```html
147+
<c-youtube src="RLdsCL4RDf8" />
148+
```
149+
150+
both are rendered the same way.
151+
152+
## Rendering nested custom HTML elements
153+
154+
By default this library doesn't render nested custom HTML elements. To enable this feature you have to add this config while creating the TagEngine
155+
156+
```php
157+
$tagEngine = new TagEngine([
158+
'tag_directories' => [
159+
__DIR__.DIRECTORY_SEPARATOR.'Tags'.DIRECTORY_SEPARATOR,
160+
],
161+
'sniff_for_nested_tags' => true
162+
]);
163+
```
164+
165+
## Acknowledgements
166+
167+
This library is inspired by the following packages
168+
169+
* https://github.com/buggedcom/PHP-Custom-Tags
170+
* https://github.com/SageITSolutions/MarkupEngine

composer.json

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
{
2+
"name": "lordsimal/custom-html-elements",
3+
"description": "Allows you to create custom HTML elements to render more complex template parts",
4+
"type": "library",
5+
"license": "MIT",
6+
"authors": [
7+
{
8+
"name": "Kevin Pfeifer",
9+
"email": "kevin.pfeifer@sunlime.at"
10+
}
11+
],
12+
"require": {
13+
"php": ">=8.1",
14+
"spatie/php-structure-discoverer": "^1.2"
15+
},
16+
"require-dev": {
17+
"cakephp/cakephp-codesniffer": "^5.0",
18+
"phpunit/phpunit": "^10.1"
19+
},
20+
"autoload": {
21+
"psr-4": {
22+
"LordSimal\\CustomHtmlElements\\": "src/"
23+
}
24+
},
25+
"autoload-dev": {
26+
"psr-4": {
27+
"LordSimal\\CustomHtmlElements\\Test\\": "tests/",
28+
"LordSimal\\CustomHtmlElements\\MyPlugin\\": "tests/plugins/MyPlugin/"
29+
}
30+
},
31+
"scripts": {
32+
"cs-check": "phpcs --colors -p",
33+
"cs-fix": "phpcbf --colors -p",
34+
"test": "phpunit --colors=always",
35+
"coverage": "php -d xdebug.mode=coverage vendor/bin/phpunit --coverage-html coverage",
36+
"stan-setup": "cp composer.json composer.backup && composer require --dev symfony/polyfill-php81 phpstan/phpstan:^1.10 psalm/phar:^5.15 && mv composer.backup composer.json",
37+
"phpstan": "vendor/bin/phpstan analyze",
38+
"psalm": "vendor/bin/psalm.phar --show-info=false",
39+
"stan": [
40+
"@phpstan",
41+
"@psalm"
42+
],
43+
"stan-baseline": "vendor/bin/phpstan --generate-baseline",
44+
"psalm-baseline": "vendor/bin/psalm.phar --set-baseline=psalm-baseline.xml"
45+
},
46+
"config": {
47+
"allow-plugins": {
48+
"dealerdirect/phpcodesniffer-composer-installer": true
49+
}
50+
}
51+
}

0 commit comments

Comments
 (0)