pyrech/layout

此包已被弃用且不再维护。未建议替代包。

提供包装器以渲染带有 doctype、meta、样式、脚本等 HTML 布局。

dev-master 2013-12-20 11:11 UTC

This package is not auto-updated.

Last update: 2022-02-01 12:24:41 UTC


README

不要自己处理 HTML doctype、meta、title、style、script 等,使用 Pyrech\Layout

Layout 是一个 PHP 类,用于包装并渲染 HTML 布局。您可以自定义 doctype、meta、样式表、脚本等。

此类可以使用两种方式。您可以在渲染方法中指定您的设置,或者按照您想要的顺序在 head 部分中添加每个元素。

[推荐] 第一种方式(按需添加每个元素)

$content = '<h1>Hello World</h1>';

$layout = \Pyrech\Layout::getInstance(); // Or new \Pyrech\Layout();

$layout->setDoctype(\Pyrech\Layout::DOCTYPE_HTML5)
       ->addMeta('charset', 'utf-8')
       ->addTitle('My wonderful title')
       ->addMeta('description', 'Description of your page')
       ->addMeta('robot', 'index')
       ->addMeta('http-equiv:refresh', '60') // If the key attribute is not 'name', prefix the value by the attribute then ':''
       ->addIcon('/favicon.png', 'png')
       ->addIcon('/favicon.ico', 'ico')
       ->addStyle('/my-stylesheet.css') // Default media is 'all'
       ->addStyle('/print.css', 'print')
       ->addScript('/my-javascript.js', \Pyrech\Layout::SCRIPT_DEFER)
       ->addScript('alert("Hello World!");', \Pyrech\Layout::SCRIPT_INTERNAL)
       ->addBodyClass(array('some-class', 'another-class')); // Array of classes or a string with several classes

echo $layout->render($content);

第二种方式(通过渲染方法)

$content = '<h1>Hello World</h1>';

$layout = \Pyrech\Layout::getInstance(); // Or new \Pyrech\Layout();

$opts = array('doctype' => \Pyrech\Layout::DOCTYPE_HTML5,
              'meta'    => array('charset'            => 'utf-8',
                                 'description'        => 'Description of your page',
                                 'robot'              => 'index',
                                 'http-equiv:refresh' => '60'), // If the attribute is not name, prefix the value by the attribute then ':'
              'title'   => 'My wonderful title',
              'icon'    => array('/favicon.png' => 'png',
                                 '/favicon.ico' => 'ico'),
              'styles'  => array('/my-stylesheet.css', // Default media is 'all'
                                 '/print.css' => 'print'),
              'scripts' => array('/my-javascript.js', // Defer can be setted for one script with the \Pyrech\Layout::SCRIPT_DEFER option
                                 'alert("Hello World!");' => \Pyrech\Layout::SCRIPT_INTERNAL),
              'defer'   => true, // Defer can be setted for all scripts
              'class'   => array('some-class', 'another-class')); // Array of classes or a string with several classes
              
echo $layout->render($content, $opts);

自定义元素

如果您想在 head 部分插入自定义元素,必须使用第一种方式(见上文)并调用 addElement 方法。

$layout->addElement('<!--Your html comment-->');

Pyrech\Layout 可以在任何 PHP 框架中实现,也可以在简单结构中使用。通过 composer 可用: pyrech/layout