Simple library for html rendering

2.0.1 2017-09-22 11:17 UTC


Build Status Version License Downloads Contao Community Alliance coding standard

This library is a PHP 7.1 helper library for creating HTML output.


This extension can be installed using Composer

composer require netzmacht/html:~2.0

Basic Usage

Define the attributes in the View:


$attributes = new Netzmacht\Html\Attributes();
    ->setAttribute('data-target', '#some');

This library uses the magic __toString to converts the helper objects to string. Outputting is really simple now:

<div <?= $attributes; ?>><span class="label">Label</span> This is a paragraph.</div>

Of course you can change the attributes before generating

<div <?= $attributes->setId('second')->removeClass('a_class')->addClass('new_class'); ?>>the content</div>


Of course you can create the whole element as well. The library knows about standalone html elements which can't have any children and nodes which have children. Notice that the css classes are passed as array.

$factory   = new Netzmacht\Html\Factory\ElementFactory();
$paragraph = $factory->create('p', array('id' => 'a_id', 'class' => array('description'));
$label     = $factory->create('span');

    ->addChild('Label this');

    ->addChild('This is a paragraph.')
    ->addChild(' '); // add space between both elements
    ->addChild($label, Netzmacht\Html\Element\Node::POSITION_FIRST); // add at first position    

Now you can output the whole element:

    <?= $paragraph; ?>


This library provides a Symfony Bundle and Contao Manager Plugin as integration.



// Register your bundle in the AppKernel
public function getBundles()
    return [
        // ...
        new Netzmacht\Html\Infrastructure\SymfonyBundle\NetzmachtHtmlBundle()

// Use the factory
$factory = $container->get('netzmacht.html.factory');
$element = $factory->create('p');