opis-colibri / html
1.0-beta.1
2020-04-18 11:44 UTC
Requires
- opis-colibri/core: ^1.0
Requires (Dev)
- phpunit/phpunit: ^7.0
This package is auto-updated.
Last update: 2021-01-06 12:26:23 UTC
README
这是一个帮助您以面向对象的方式处理HTML文档的模块。
收集器
视图
此模块提供了以下视图的实现: html.document
、html.collection
、html.script
、html.meta
、html.style
、html.link
和 html.attributes
。
路由
此模块提供了一个名为 htmldoc
的全局绑定,它将返回 Opis\Colibri\Modules\Html\Document
类的单例实例。
如何使用它
以下是对该模块的一些基本用法
use Opis\Colibri\Modules\Html\Document as HtmlDoc; $doc = new HtmlDoc(); $doc->title("My page's title") ->content("This is some content") ->htmlAttributes(['lang' => 'en']) ->bodyAttributes(['onload' => 'alert(1)']) ->bodyClasses(['foo', 'bar']); echo $doc;
输出将是
<!DOCTYPE html> <html lang="en"> <head> <title>My page's title</title> </head> <body onload="alert(1)" class="foo bar"> This is some content </body> </html>
如果您想为您的文档设置基本URL,只需使用 base
方法。
$doc->base('/foo');
<!DOCTYPE html> <html lang="en"> <head> <title>My page's title</title> <base href="/foo"> </head> <body onload="alert(1)" class="foo bar"> This is some content </body> </html>
您可以通过使用 Opis\Colibri\Modules\Html\MetaCollection
类提供的各种方法向文档中添加元标签。要访问元集合实例,只需在文档实例上调用 meta
方法。
$doc->meta() ->keywords(['foo', 'bar', 'baz']) ->description('This is a description for my page');
<!DOCTYPE html> <html lang="en"> <head> <title>My page's title</title> <base href="/foo"> <meta name="keywords" content="foo, bar, baz"> <meta name="description" content="This is a description for my page"> </head> <body onload="alert(1)" class="foo bar"> This is some content </body> </html>
使用 links
方法添加链接标签到您的文档,该方法返回一个 Opis\Colibri\Modules\Html\LinkCollection
实例。返回的实例提供了向您的页面添加链接标签的各种方法。
$doc->links() ->favicon('/assets/favicon.ico') ->custom('apple-touch-icon', function(Link $link){ $link->attributes([ 'rel' => 'apple-touch-icon', 'sizes' => '180x180', 'href' => '/assets/icon-180x180.png', ]); }) ->link('canonical', '/other-page');
<!DOCTYPE html> <html lang="en"> <head> <title>My page's title</title> <base href="/foo"> <meta name="keywords" content="foo, bar, baz"> <meta name="description" content="This is a description for my page"> <link rel="icon" href="/assets/favicon.ico"> <link rel="apple-touch-icon" sizes="180x180" href="/assets/icon-180x180.png"> <link rel="canonical" href="/other-page"> </head> <body onload="alert(1)" class="foo bar"> This is some content </body> </html>