1. Go to this page and download the library: Download rinart73/document-helper library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
rinart73 / document-helper example snippets
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/[email protected]/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');
}
<!doctype html>
<html <?= document_html()
namespace App\Controllers;
class Articles extends BaseController
{
public function index()
{
$this->document
->addBodyClasses('archive', 'archive--articles')
->setTitle('My articles')
->setMeta('description', 'Articles archive description');
}
}
$document = service('document');
$document
->registerStyle('library', 'assets/css/library.css', [], '1.1')
->registerScript('library', 'assets/js/library.js', [], '1.1');
$document->registerScript('core', 'assets/js/core.js', [], '1.1.2');
$document->registerScript('app-common', 'assets/js/app-common.js', ['core', 'library'], '1.2');
/**
* Will add `library` and `core` styles and scripts before `app-common`.
* By default scripts automatically request styles with the same handle but the feature can be turned off.
*/
$this->document->addScripts('app-common');
// add script tag with serialized data before the `app-common` script
$document->localizeScript('app-common', 'appCommonData', [
'baseUrl' => base_url(),
'errorGeneric' => lang('Common.errorGeneric')
]);
// add inline script with custom attributes in the script tag
$document
->registerInlineScript('my-inline', 'console.log("Hello world");', [], ['data-test' => '42'])
->addScripts('my-inline');
$images = service('documentImages');
// generate img tag with width and height
$images->renderTag('uploads/my.jpg');
// img tag without width and height because it's an external resource
$images->renderTag('https://via.placeholder.com/640x360');
// set image defaults and enable WebP
$images->setAlternateTypes(IMAGETYPE_WEBP)
->setSrcsetWidths(1536, 1024, 768)
->setImgAttributes(['class' => 'img-fluid', 'loading' => 'lazy']);
/**
* Will generate:
* 1. Proportionally scaled JPEG versons with 1536px, 1024px and 768px widths
* 2. WebP versions of the full size image and resized images
* Will render a picture tag with sources and img inside
* By default images are generated on-demand but this behaviour can be altered.
*/
$images->renderTag('uploads/my.jpg');
/**
* Will generate:
* 1. 1024x1024 and 768x768 versions of the image. If image needs to be cut,
* data in the top right corner will be prioritised.
* 2. WebP versions of the resized images
* Will render a picture tag with sources and img inside (won't
class YourController extends BaseController
{
public function index()
{
helper('document');
}
}
<!doctype html>
<html <?= document_html()
<?= $this->extend('layout-default')
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.