2017 © Pedro Peláez
 

library view

image

peacock/view

  • Wednesday, December 14, 2016
  • by TomWright
  • Repository
  • 1 Watchers
  • 0 Stars
  • 365 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 5 Versions
  • 0 % Grown

The README.md

Peacock

Build Status Total Downloads Latest Stable Version Latest Unstable Version License, (*1)

Peacock is a View/Layout engine designed to make it easy to use and manipulate views without having to dive into the HTML., (*2)

Installation

composer install peacock/view

Usage

Create a view file: say_hello.php., (*3)

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

Get an instance of the ViewFactory., (*4)

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

Get a View out of the ViewFactory., (*5)

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

Render the View., (*6)

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

Using Layouts

Create a layout file: layout.php., (*7)

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

Create a view file: say_hello.php., (*8)

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

Get a Layout out of the ViewFactory., (*9)

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

Add a child view to the layout specifying that the view content should be added to the content section, and then render the layout., (*10)

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

That will give the following output., (*11)

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

Multiple Child Views & Layouts

You can add the same View/Layout multiple times., (*12)

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

That will give the following output., (*13)

<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>

View Data

You can also pass data directly to a child view/layout., (*14)

$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();

That will give the following output., (*15)

<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>

Child Layouts

If you want a view to add content to multiple sections then a Layout is the way to go., (*16)

Let's say we have a blog post that will show content in the main body, but also add the author's name to the footer., (*17)

layout.php, (*18)

<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, (*19)

{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}

Implementation:, (*20)

$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();

Output:, (*21)

<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>

The Versions

14/12 2016

dev-master

9999999-dev

  Sources   Download

proprietary

The Requires

 

by John Saunders

14/12 2016

0.0.4

0.0.4.0

  Sources   Download

proprietary

The Requires

 

by John Saunders

13/12 2016

0.0.3

0.0.3.0

  Sources   Download

proprietary

The Requires

 

by John Saunders

28/07 2016

0.0.2

0.0.2.0

  Sources   Download

proprietary

The Requires

 

by John Saunders

16/06 2016

0.0.1

0.0.1.0

  Sources   Download

proprietary

The Requires

 

by John Saunders