IPL: How to use ipl-html

by | Feb 22, 2023

In my previous blogpost I briefly explained the IPL and the tasks that these individual libs can perform. Today I want to explain how our ipl-html lib works and how to use it.

This library helps you write HTML in a very simple and safe way. You don’t need text escaping and view scripts.

Usually we always have to create a view script for each controller action as follows:

The Controller:
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<?php
namespace Icinga\Module\Test\Controllers;
use ipl\Web\Compat\CompatController;
class TestController extends CompatController
{
public function indexAction(): void
{
$this->view->header = 'Hello Icinga Users';
$this->view->description = 'Enjoy the blogpost';
}
}
<?php namespace Icinga\Module\Test\Controllers; use ipl\Web\Compat\CompatController; class TestController extends CompatController { public function indexAction(): void { $this->view->header = 'Hello Icinga Users'; $this->view->description = 'Enjoy the blogpost'; } }
<?php

namespace Icinga\Module\Test\Controllers;

use ipl\Web\Compat\CompatController;

class TestController extends CompatController
{
    public function indexAction(): void
    {
       $this->view->header = 'Hello Icinga Users';
       $this->view->description = 'Enjoy the blogpost';
    }
}

 

The view script for the index action in path application/views/scripts/test/index.phtml:
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<div class="controls">
<?= $this->tabs ?>
</div>
<div class="content">
<div class="widget">
<h1 class="header"><?= $this->escape($this->header) ?></h1>
<p class="description"><?= $this->escape($this->description) ?></p>
</div>
</div>
<div class="controls"> <?= $this->tabs ?> </div> <div class="content"> <div class="widget"> <h1 class="header"><?= $this->escape($this->header) ?></h1> <p class="description"><?= $this->escape($this->description) ?></p> </div> </div>
<div class="controls">
        <?= $this->tabs ?>
</div>
<div class="content">
    <div class="widget">
        <h1 class="header"><?= $this->escape($this->header) ?></h1>
        <p class="description"><?= $this->escape($this->description) ?></p>
    </div>
</div>
Result:

 

 

To achieve the same result with ipl-html, do the following:

The Controller:
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<?php
namespace Icinga\Module\Test\Controllers;
use Icinga\Module\Test\Widget;
use ipl\Web\Compat\CompatController;
class TestController extends CompatController
{
public function indexAction(): void
{
$this->addContent(new Widget());
}
}
<?php namespace Icinga\Module\Test\Controllers; use Icinga\Module\Test\Widget; use ipl\Web\Compat\CompatController; class TestController extends CompatController { public function indexAction(): void { $this->addContent(new Widget()); } }
<?php

namespace Icinga\Module\Test\Controllers;

use Icinga\Module\Test\Widget;
use ipl\Web\Compat\CompatController;

class TestController extends CompatController
{
    public function indexAction(): void
    {
       $this->addContent(new Widget());
    }
}
The class used in the controller above:
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<?php
namespace Icinga\Module\Test;
use ipl\Html\BaseHtmlElement;
use ipl\Html\Html;
use ipl\Html\HtmlString;
class Widget extends BaseHtmlElement
{
protected $tag = 'div';
protected $defaultAttributes = ['class' => 'widget'];
protected function assemble()
{
$this->add([
Html::tag('h1', ['class' => 'header'], 'Hello Icinga Users'),
// you can also do the following:
HtmlString::create('<p class="description">Enjoy the blogpost</p>')
]);
}
}
<?php namespace Icinga\Module\Test; use ipl\Html\BaseHtmlElement; use ipl\Html\Html; use ipl\Html\HtmlString; class Widget extends BaseHtmlElement { protected $tag = 'div'; protected $defaultAttributes = ['class' => 'widget']; protected function assemble() { $this->add([ Html::tag('h1', ['class' => 'header'], 'Hello Icinga Users'), // you can also do the following: HtmlString::create('<p class="description">Enjoy the blogpost</p>') ]); } }
<?php

namespace Icinga\Module\Test;

use ipl\Html\BaseHtmlElement;
use ipl\Html\Html;
use ipl\Html\HtmlString;

class Widget extends BaseHtmlElement
{
    protected $tag = 'div';

    protected $defaultAttributes = ['class' => 'widget'];

    protected function assemble()
    {
        $this->add([
            Html::tag('h1', ['class' => 'header'], 'Hello Icinga Users'),
            // you can also do the following:
            HtmlString::create('<p class="description">Enjoy the blogpost</p>')
        ]);
    }
}

 

This will give you exactly the same result as above. That is quite simple, isn’t it?
We at icinga use this library for all our new developments. The new module icingadb-web uses it the most at the moment.

Using ipl-html you can easily create tables, forms and any HTML elements.

You can find more examples and interesting features in our doc.

 

 

 

 

 

You May Also Like…

Subscribe to our Newsletter

A monthly digest of the latest Icinga news, releases, articles and community topics.