peacock/view

0.0.4 2016-12-14 14:41 UTC

This package is auto-updated.

Last update: 2024-09-15 07:10:40 UTC


README

Build Status Total Downloads Latest Stable Version Latest Unstable Version License

Peacock是一个视图/布局引擎,旨在简化视图的使用和操作,而无需深入HTML。

安装

composer install peacock/view

用法

创建一个视图文件: say_hello.php

<p>Hello there, <?= $username ?>.</p>

获取一个ViewFactory实例。

$viewFactory = ViewFactory::getInstance();
$viewFactory->setViewsDirectory(VIEWS_PATH);

ViewFactory获取一个View

$viewData = ['username' => 'Tom'];
$view = $viewFactory->view('say_hello', $viewData);

渲染View

$view->render();
// <p>Hello there, Tom.</p>

使用布局

创建一个布局文件: layout.php

<html>
    <head>
    <title>Example</title>
    </head>
    <body>
        <h1>Example</h1>
        {RENDER_SECTION: content}
    </body>
</html>

创建一个视图文件: say_hello.php

<p>Hello there, <?= $username ?>.</p>

ViewFactory获取一个Layout

$viewData = ['username' => 'Tom'];
$layout = $viewFactory->layout('layout', $viewData);

向布局添加一个子视图,指定视图内容应添加到content部分,然后渲染布局。

$childView = $layout->childView('say_hello', 'content');
$layout->render();

这将产生以下输出。

<html>
    <head>
    <title>Example</title>
    </head>
    <body>
        <h1>Example</h1>
        <p>Hello there, Tom.</p>
    </body>
</html>

多个子视图和布局

您可以多次添加相同的视图/布局。

$childView = $layout->childView('say_hello', 'content');
$childView = $layout->childView('say_hello', 'content');
$childView = $layout->childView('say_hello', 'content');
$layout->render();

这将产生以下输出。

<html>
    <head>
    <title>Example</title>
    </head>
    <body>
        <h1>Example</h1>
        <p>Hello there, Tom.</p>
        <p>Hello there, Tom.</p>
        <p>Hello there, Tom.</p>
    </body>
</html>

视图数据

您还可以直接将数据传递给子视图/布局。

$childView = $layout->childView('say_hello', 'content', ['username' => 'Frank']);
$childView = $layout->childView('say_hello', 'content', ['username' => 'Amelia']);
$childView = $layout->childView('say_hello', 'content', ['username' => 'Steve']);
$layout->render();

这将产生以下输出。

<html>
    <head>
    <title>Example</title>
    </head>
    <body>
        <h1>Example</h1>
        <p>Hello there, Frank.</p>
        <p>Hello there, Amelia.</p>
        <p>Hello there, Steve.</p>
    </body>
</html>

子布局

如果您希望视图添加到多个部分,则Layout是正确的方法。

假设我们有一个博客文章,它将在主体中显示内容,但也会将作者姓名添加到页脚。

layout.php

<html>
<head>
    <title>{RENDER_SECTION:title} - My Blog</title>
</head>
<body>
    <div>
        {RENDER_SECTION:main_content}
    </div>
    <footer>
        {RENDER_SECTION:footer}
    </footer>
</body>
</html>

blog_post

{SECTION:title}<?= $post->title; ?>{END_SECTION}

{SECTION:main_content}
<h1><?= $post->title; ?></h1>
<?= $post->content; ?>
{END_SECTION}

{SECTION:footer}Written by <?= $post->author; ?>{END_SECTION}

实现

$post = new stdClass();
$post->title = 'How to use Peacock';
$post->author = 'Tom Wright';
$post->content = 'You should check out the GitHub README!';
$postData = ['post' => $post];

$layout = $viewFactory->layout('layout');
$layout->childLayout('blog_post', $postData);
$layout->render();

输出

<html>
<head>
    <title>How to use Peacock - My Blog</title>
</head>
<body>
<div>
    
<h1>How to use Peacock</h1>
You should check out the GitHub README!
</div>
<footer>
    Written by Tom Wright
</footer>
</body>
</html>