rinart73/document-helper
Composer 安装命令:
composer require rinart73/document-helper
包简介
CodeIgniter 4 library for that allows for easier HTML generation, particularly when it comes to head tags, scripts, styles and images
README 文档
README
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
- Add document
title,meta,linktags in Controllers;htmlandbodyclasses 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 renderimg/pictureat the same time.
Getting Started
Prerequisites
- A CodeIgniter 4.2.7+ based project
- Composer for package management
- PHP 7.4+
Installation
Installation is done through Composer.
composer require rinart73/document-helper
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
<!doctype html> <html <?= document_html() ?>> <head> <?= document_head() ?> </head> <body <?= document_body() ?>> <?= $this->renderSection('content') ?> <?= document_footer() ?> </body> </html>
Then you will be able to use the features that the library prodives in your Controllers and Views:
Controllers/Articles.php
namespace App\Controllers; class Articles extends BaseController { public function index() { $this->document ->addBodyClasses('archive', 'archive--articles') ->setTitle('My articles') ->setMeta('description', 'Articles archive description'); } }
Overview
Refer to examples for more showcases.
Document tags
$document = service('document'); $document ->setHtmlAttributes(['lang' => 'en-US']) ->setBodyAttributes(['class' => 'page-article']) ->setTitle('My article') ->setTitleSuffix('| WebSite') ->setMeta('charset', 'utf-8') ->setMeta('viewport', 'width=device-width, initial-scale=1') ->setMeta('description', 'My article description') ->setMeta('robots', 'index, follow') ->addLink('canonical', 'https://example.com/articles/my-article/') ->addLink('alternate', 'https://example.com/articles/my-article/', ['hreflang' => 'en']) ->addLink('alternate', 'https://example.ru/articles/translated-article/', ['hreflang' => 'ru']);
Scripts and styles
$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
$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 include original file), * Default class attribute will be overridden but loading=lazy will be kept. */ $images->renderTag('uploads/my.jpg', ['1:1', 'top-right', 1024, 768], ['class' => 'img-square']);
Helpers
Helper functions are typically meant to be used inside views, although they can be used anywhere else.
Controllers/YourController.php
class YourController extends BaseController { public function index() { helper('document'); } }
Views/layout-default.php
<!doctype html> <html <?= document_html() ?>> <head> <?= document_head() ?> </head> <body <?= document_body() ?>> <?= $this->renderSection('content') ?> <?= document_footer() ?> </body> </html>
Views/view-auth.php
<?= $this->extend('layout-default') ?> <?php document_add_html_classes('h-100'); document_add_body_classes('bg-light', 'h-100', 'page-auth'); document_add_libraries('bootstrap', 'app-auth'); ?> <?= $this->section('content') ?> <main class="container-xl h-100"> <div class="row h-100 align-items-center justify-content-center"> <div class="col"> <?= document_image('uploads/my.jpg', ['1:1', 'top-right', 1024, 768], ['class' => 'img-square']) ?> </div> </div> </main> <?= $this->endSection() ?>
License
This project is licensed under the MIT License - see the LICENSE file for details.
rinart73/document-helper 适用场景与选型建议
rinart73/document-helper 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3 次下载、GitHub Stars 达 0, 最近一次更新时间为 2023 年 02 月 09 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「codeigniter」 「codeigniter4」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 rinart73/document-helper 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 rinart73/document-helper 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 rinart73/document-helper 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
The CodeIgniter Redis package
JSON Web Token for codeigniter4 authentication.
ORM Database mapping for Codeigniter 4
An Example of CodeIgniter 4 Page module
统计信息
- 总下载量: 3
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 17
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-02-09