PHPackages                             rinart73/document-helper - 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. [Templating &amp; Views](/categories/templating)
4. /
5. rinart73/document-helper

ActiveLibrary[Templating &amp; Views](/categories/templating)

rinart73/document-helper
========================

CodeIgniter 4 library for that allows for easier HTML generation, particularly when it comes to head tags, scripts, styles and images

v1.0.0-beta(3y ago)03[1 PRs](https://github.com/rinart73/document-helper/pulls)MITPHPPHP &gt;=7.4.0

Since Feb 9Pushed 2y ago1 watchersCompare

[ Source](https://github.com/rinart73/document-helper)[ Packagist](https://packagist.org/packages/rinart73/document-helper)[ Docs](https://github.com/rinart73/document-helper)[ RSS](/packages/rinart73-document-helper/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (1)Dependencies (5)Versions (4)Used By (0)

Document Helper
===============

[](#document-helper)

[![](https://github.com/rinart73/document-helper/workflows/PHPUnit/badge.svg)](https://github.com/rinart73/document-helper/actions/workflows/phpunit.yml)[![](https://github.com/rinart73/document-helper/workflows/PHPStan/badge.svg)](https://github.com/rinart73/document-helper/actions/workflows/phpstan.yml)[![Coverage Status](https://camo.githubusercontent.com/051287d8892bcb2b6cecb90bf3266f14b16b3bae6339021c25a6676c97a0167f/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f72696e61727437332f646f63756d656e742d68656c7065722f62616467652e7376673f6272616e63683d646576656c6f70)](https://coveralls.io/github/rinart73/document-helper?branch=develop)

Document Helper is a CodeIgniter 4 library for easier HTML generation, particularly when it comes to meta-tags, styles, scripts and images.

It's heavilly inspired by OpenCart and Wordpress.

Features
--------

[](#features)

- Add document `title`, `meta`, `link` tags in Controllers; `html` and `body` classes in Controllers and Views.
- Register scripts and styles that you might need. When you request them, their dependencies are added and sorted **automatically**.
- Generate image variants (resized versions for `srcset`, alternative image types such as WebP) and render `img`/`picture` at the same time.

Getting Started
---------------

[](#getting-started)

### Prerequisites

[](#prerequisites)

- A [CodeIgniter 4.2.7+](https://github.com/codeigniter4/CodeIgniter4/) based project
- [Composer](https://getcomposer.org/) for package management
- PHP 7.4+

### Installation

[](#installation)

Installation is done through Composer.

```
composer require rinart73/document-helper
```

### Suggested setup

[](#suggested-setup)

Add the `document` helper and the `Rinart73\DocumentHelper\Document` class into your `Controllers/BaseController.php`:

```
namespace App\Controllers;

use Rinart73\DocumentHelper\Document;

abstract class BaseController extends Controller
{
    protected $helpers = ['document'];
    protected Document $document;

    public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger)
    {
        parent::initController($request, $response, $logger);

        $this->document = service('document');
        $this->registerDocumentLibraries();
        $this->initDocumentDefaults();
    }

    // Register styles and scripts that you **might** need
    protected function registerDocumentLibraries()
    {
        $this->document->registerScript('jquery', 'https://cdn.jsdelivr.net/npm/jquery@3.6.3/dist/jquery.min.js', [], '3.6.3');
    }

    // Set default Document parameters for your pages
    protected function initDocumentDefaults()
    {
        $this->document
            ->setHtmlAttributes(['lang' => 'en-US'])
            ->setMeta('charset', 'utf-8')
            ->setMeta('viewport', 'width=device-width, initial-scale=1')
            ->setMeta('robots', 'index, follow')
            ->setTitleSuffix('| MyWebSite')
            ->addScripts('jquery');
    }
```

Then add helper functions into your layouts: `Views/layouts/layout-default.php`

```

>

>
