PHPackages                             arekx/html-processor - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. arekx/html-processor

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

arekx/html-processor
====================

HTML array processor

1.0.0(7y ago)051MITPHPPHP &gt;=7.1

Since Feb 23Pushed 7y ago1 watchersCompare

[ Source](https://github.com/ArekX/html-processor)[ Packagist](https://packagist.org/packages/arekx/html-processor)[ RSS](/packages/arekx-html-processor/feed)WikiDiscussions master Synced 3d ago

READMEChangelog (1)Dependencies (2)Versions (2)Used By (0)

html-processor
==============

[](#html-processor)

PHP html processor using arrays

Why
===

[](#why)

Combining html tags is hard. Standard string concatenation just doesn't work, it's hard to sanitize the input and it just looks ugly. This approach uses arrays to fully create a structure which nicely translates to HTML. But what about template engines like Blade, Mustache, etc?

This was not supposed to replace them, even though it fully could. This was intended so that it can be used in programmatic places where you need to create tags dynamically like recursive menu generators, but do not want to use template engines. Examples for these are widgets which output html or in other places of the code where you would need to output programmatically.

So this:

```
[
    ['@!DOCTYPE', 'html' => true],
    ['html', 'lang' => 'en-US', [
        ['head', [
            ['title', 'This is some title']
        ]],
        ['body', [
            ['h1', 'This is some page title'],
            ['button', 'This is a button'],
            ['a', 'href' => '#link', 'this is a link']
        ]]
    ]]
]
```

Translates nicely into:

```
>

        This is some title

        This is some page title
        This is a button
        this is a link

```

Usage
=====

[](#usage)

```
$renderer = new ArekX\HtmlProcessor\Html();

echo $renderer->render(['div']); // Renders:
echo $renderer->render(['div', 'class' => 'class']); // Renders:
echo $renderer->render(['div', 'class' => 'class', 'Inner content']); // Renders: Inner content
echo $renderer->render(['div', ['text', 'value']]); // textvalue
echo $renderer->render(['div', ['text', 'value', ['another-element']]]); // textvalue
```

Self closing tags
-----------------

[](#self-closing-tags)

```
echo $renderer->render(['@input', 'type' => 'text']); // Renders:
echo $renderer->render(['/br']); // Renders:
```

Conditional rendering
---------------------

[](#conditional-rendering)

You can conditionally render elements or text in arrays. If something is not to be rendered you can pass `false`and that part of the rendering will be skipped.

```
$condition = false;
echo $renderer->render([['p', [
    $condition ? ['strong', 'Item wrapped in strong'] : false,
    'test'
]]]); // Renders: test
```

### Using callbacks

[](#using-callbacks)

You can also use callbacks in elements to handle conditional rendering. `render($template, $config = [])` function accepts additional optional parameter for configuration which will be passed to all callbacks used in the template array.

```
echo $renderer->render([['p', [
    function ($config) {
        return $config['wrapped'] ? ['strong', 'Item wrapped in strong'] : false;
    },
    'test'
]]], ['wrapped' => true]); // Renders: Item wrapped in strongtest
```

Attributes conditional rendering
--------------------------------

[](#attributes-conditional-rendering)

If you pass `true` to an attribute you will just render that attribute without a value:

**NOTE:** All attribute values rendered are encoded using `htmlspecialchars`.

```
echo $renderer->render(['div', 'attribute' => true]); // Renders:
```

If you pass `false` as a value to any attribute that attribute wont be rendered:

```
echo $renderer->render(['div', 'class' => false, 'data-test' => 'test']); // Renders:
```

Value for attribute can also be a callback:

```
echo $renderer->render(['div', 'class' => function($config) {
    return $config['class'];
}, 'data-test' => 'test'], ['class' => ['class1', 'class2']]); // Renders:
```

Special attributes
------------------

[](#special-attributes)

If you look closely `class="class1 class2"` in above example, it is rendered using `'class' => ['class1', 'class2']`. There are special renderers for few fields like `class` and `style` which allows non-string values which can be parsed.

`class` attribute allows an array which will be joined with all `false` values removed. It also allows for a class value to be a callable.

`style` attribute allows for `['property' => 'value']` which will render to `property: value`. Value can also be a callable which will get executed to return property value. If a property value is `false` it will be skipped.

### Json attributes

[](#json-attributes)

You can encode arrays as json in attributes:

```
$array = [
    'key1' => 'value1',
    'key2' => 'value2'
];
echo $renderer->render(['div', 'data-prop' => ['json' => $array]]);
// Renders:
```

Testing
=======

[](#testing)

- To run tests run `composer test`.
- To generate coverage report run `composer coverage`

###  Health Score

24

—

LowBetter than 32% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity5

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity55

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

2638d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/3c16806a6fc4805b54005c72ea7aa0b6bc33f889a4a657a3071953f6b4e4a23c?d=identicon)[ArekXV](/maintainers/ArekXV)

---

Top Contributors

[![ArekX](https://avatars.githubusercontent.com/u/4344776?v=4)](https://github.com/ArekX "ArekX (6 commits)")

---

Tags

arraysconditionalformattinghtmlphpprocessorrendereringtemplate

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/arekx-html-processor/health.svg)

```
[![Health](https://phpackages.com/badges/arekx-html-processor/health.svg)](https://phpackages.com/packages/arekx-html-processor)
```

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
