crell/htmlmodel
Composer 安装命令:
composer require crell/htmlmodel
包简介
Domain value objects for modeling HTML pages
关键字:
README 文档
README
HtmlModel is exactly what it sounds like. It is a series of value objects intended to model an HTML page. It does not attempt to model every element in HTML (that would be silly), but just the key aspects of it. In a sense, it seeks to provide an HTML equivalent of RESTful domain models such as HAL or Atom.
Inspired by PSR-7, all objects are immutable. They may be manipulated with with*() methods, which return new value object instances. Link handling uses the PSR-13 hyperlink specification.
This approach was inspired by, and evolved from, similar code that exited in Drupal 8 during development but was later removed.
Install
Via Composer
$ composer require crell/htmlmodel
Usage
If you've used a PSR-7 Request or Response object, HtmlModel should be quite similar. Most of the time you'll be interacting with either an HtmlFragment or an HtmlPage.
// Create an HtmlFragment object. $fragment = new HtmlFragment(); // Set the fragment body, which is simply an arbitrary HTML string. $fragment = $fragment->withContent('<aside>Immutable objects are easier than you think.</aside>'); // Populate its metadata; the metadata won't be rendered with this // fragment but can be transferred to an aggregating page, or a JSON // instruction in response to an Ajax call. $fragment = $fragment ->withHeadElement(new MetaRefreshElement(3, 'http://www.google/com')) ->withHeadElement(new LinkElement('canonical', 'http://www.example.com/')) ->withStyleLink(new StyleLinkElement('css.css')) ;
// Create an HtmlPage object. $html = new HtmlPage(); // Populate it with various elements and body contents. $html = $html ->withTitle('Test page') ->withHtmlAttribute('manifest', 'example.appcache') ->withBodyAttribute('foo', 'bar') ->withBase(new BaseElement('http://www.example.com/')) ->withHeadElement(new MetaRefreshElement(3, 'http://www.google.com')) ->withHeadElement(new LinkElement('canonical', 'http://www.example.com/')) ->withScript(new ScriptElement('header.js')) ->withScript(new ScriptElement('footer.js'), 'footer') ->withStyleLink(new StyleLinkElement('css.css')) ->withInlineStyle(new StyleElement('CSS here')) ->withContent('Body here') ; // Simply casting the page object to a string will produce the corresponding markup. $output = (string)$html;
The HtmlPage can even contain an HTTP status code. Although it won't get rendered it can be transferred to a Response object, allowing the page creator to specify the type of response through a straightforward domain object.
The cool part, though, is when you can aggregate multiple fragments into a page cleanly. That lets you build multiple portions of a page in parallel, even asynchronously, even caching some portions of the page but not others, and then fold them together into a single page.
// Create an HtmlFragment (or return one from some lower-level routine) $src = new HtmlFragment(); $src = $src ->withHeadElement(new MetaRefreshElement(3, 'http://www.google.com')) ->withHeadElement(new LinkElement('canonical', 'http://www.example.com/')) ->withScript(new ScriptElement('js.js')) ->withScript(new ScriptElement('footer.js'), 'footer') ->withScript($inline_script) ->withStyleLink(new StyleLinkElement('css.css')) ->withInlineStyle(new StyleElement('CSS here')) ->withContent('Body here') ; // Now make an HtmlPage. $dest = new HtmlPage(); // Now shuffle the metadata from the fragment to the page. $transferer = new AggregateMetadataTransferer([ StyleContainerInterface::class => new StyleTransferer(), ScriptContainerInterface::class => new ScriptTransferer(), StatusCodeContainerInterface::class => new StatusCodeTransferer(), HeadElementContainerInterface::class => new HeadElementTransferer(), ]); $html = $transferer->transfer($src, $dest); // Now take other fragments and transfer their metadata to the page, aggregating them together!
Change log
Please see CHANGELOG for more information what has changed recently.
Testing
$ composer test
Contributing
Please see CONTRIBUTING for details.
Security
If you discover any security related issues, please email the author instead of using the issue tracker.
Credits
License
The LGPL License, version 3 or, at your option, any later version. Please see License File for more information.
crell/htmlmodel 适用场景与选型建议
crell/htmlmodel 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 28 次下载、GitHub Stars 达 21, 最近一次更新时间为 2015 年 06 月 30 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「html」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 crell/htmlmodel 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 crell/htmlmodel 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 crell/htmlmodel 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Texy converts plain text in easy to read Texy syntax into structurally valid (X)HTML. It supports adding of images, links, nested lists, tables and has full support for CSS. Texy supports hyphenation of long words (which reflects language rules), clickable emails and URL (emails are obfuscated again
HTML and form generation
A modern HTML DOM parser for PHP using DOMDocument and Symfony CssSelector.
Reusable utilities library for Lacus Solutions' packages (type description, HTML escaping, random sequences)
Yii2 block extension
A rails-inspired form builder for PHP
统计信息
- 总下载量: 28
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 21
- 点击次数: 23
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2015-06-30