2017 © Pedro Peláez
 

library pop-view

Pop View Component for Pop PHP Framework

image

popphp/pop-view

Pop View Component for Pop PHP Framework

  • Monday, January 29, 2018
  • by nicksagona
  • Repository
  • 1 Watchers
  • 2 Stars
  • 1,808 Installations
  • PHP
  • 5 Dependents
  • 1 Suggesters
  • 0 Forks
  • 0 Open issues
  • 16 Versions
  • 3 % Grown

The README.md

pop-view

Build Status Coverage Status, (*1)

Join the chat at https://discord.gg/TZjgT74U7E, (*2)

Overview

pop-view is the view template component that can be used as the "V" in an MVC stack or independently as well. It supports using both PHP-file based templates and stream templates. Within the stream templates, there is basic support for logic and iteration for dynamic control over the view template., (*3)

pop-view is a component of the Pop PHP Framework., (*4)

Top, (*5)

Install

Install pop-view using Composer., (*6)

composer require popphp/pop-view

Or, require it in your composer.json file, (*7)

"require": {
    "popphp/pop-view" : "^4.0.3"
}

Top, (*8)

Quickstart

Consider a phtml template file like this:, (*9)

<html>
<body>
    <h1><?=$title; ?></h1>
</body>
</html>

You can set up a view object and populate data like this:, (*10)

use Pop\View\View;
use Pop\View\Template\File;

$view        = new View(new File('hello.phtml'));
$view->title = 'Hello World!';

echo $view;

which will produce:, (*11)

<html>
<body>
    <h1>Hello World!</h1>
</body>
</html>

Top, (*12)

File Template

A file template simply uses PHP variables to deliver the data and content to template to be rendered. With a file template, you have full access to the PHP environment to write any additional code or helper scripts. However, in using this, you must make sure to adhere to the best practices and standards regarding the security of the application., (*13)

hello.phtml
<!DOCTYPE html>
<html>
<head>
    <title><?=$title; ?></title>
</head>
<body>
    <h1><?=$title; ?></h1>
    <p><?=$content; ?></p>
</body>
</html>

You can set up the view object like this:, (*14)

use Pop\View\View;
use Pop\View\Template\File;

$view = new View(new File('hello.phtml'));
$view->title   = 'Hello World!';
$view->content = 'This is a test!';

echo $view;

Top, (*15)

Stream Template

A stream template uses a formatted string placeholder to deliver the data and content to template to be rendered:, (*16)

hello.html
<!DOCTYPE html>
<html>
<head>
    <title>[{title}]</title>
</head>
<body>
    <h1>[{title}]</h1>
    <p>[{content}]</p>
</body>

</html>

You can set up the view object in a similar way and it will render the exact same as the file template example., (*17)

use Pop\View\View;
use Pop\View\Template\Stream;

$view = new View(new Stream('hello.html'));
$view->title   = 'Hello World!';
$view->content = 'This is a test!';

echo $view;

Top, (*18)

Includes

Stream templates support includes to allow you to include other templates within them., (*19)

header.html

<!DOCTYPE html>
<html>

<head>
    <title>[{title}]</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>

<body>
    <header>This is the header</header>
footer.html

    <footer>This is the footer</footer>
</body>

</html>
index.html
<!-- index.html //-->
{{@include header.html}}
    <h1>[{title}]</h1>
    <p>[{content}]</p>
{{@include footer.html}}

You can set up the view object like before:, (*20)

use Pop\View\View;
use Pop\View\Template\Stream;

$view = new View(new Stream('index.html'));
$view->title   = 'Hello World!';
$view->content = 'This is a test!';

echo $view;

Top, (*21)

Inheritance

Stream templates support inheritance to allow you to extend other templates., (*22)

parent.html

<!DOCTYPE html>
<html>

<head>
{{header}}
    <title>[{title}]</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
{{/header}}
</head>

<body>
    <h1>[{title}]</h1>
    [{content}]
</body>

</html>
child.html

{{@extends parent.html}}

{{header}}
{{parent}}
    <style>
        body { margin: 0; padding: 0; color: #bbb;}
    </style>
{{/header}}

You can set up the view object like before:, (*23)

use Pop\View\View;
use Pop\View\Template\Stream;

$view = new View(new Stream('child.html'));
$view->title   = 'Hello World!';
$view->content = 'This is a test!';

echo $view;

Top, (*24)

Iteration

Iteration is possible in stream templates when working with arrays and array-like objects., (*25)


<!DOCTYPE html>
<html>

<head>
    <title>[{title}]</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>

<body>

[{items}]


[{key}]: [{value}]
[{/items}] </body> </html>
use Pop\View\View;
use Pop\View\Template\Stream;

$data = [
    'items' => [
        'hello' => 'world',
        'foo'   => 'bar',
        'baz'   => 123
    ]
];

$view = new View(new Stream('index.html'), $data);

echo $view;

Top, (*26)

Conditionals

Conditional logic is possible within a stream template as well., (*27)


<!DOCTYPE html>
<html>

<head>
    <title>[{title}]</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>

<body>

[{if(foo)}]


The variable 'foo' is set to [{foo}]., (*28)

[{else}]

The variable 'foo' is not set., (*29)

[{/if}] </body> </html>
use Pop\View\View;
use Pop\View\Template\Stream;

$data = ['foo' => 'bar'];

$view = new View(new Stream('index.html'), $data);

echo $view;

Top, (*30)

The Versions

29/01 2018

dev-master

9999999-dev http://www.popphp.org/

Pop View Component for Pop PHP Framework

  Sources   Download

BSD-3-Clause New BSD

The Requires

  • php >=5.6.0

 

The Development Requires

template php view pop pop php view template

29/01 2018

dev-v3-dev

dev-v3-dev http://www.popphp.org/

Pop View Component for Pop PHP Framework

  Sources   Download

BSD-3-Clause New BSD

The Requires

  • php >=5.6.0

 

The Development Requires

template php view pop pop php view template

29/01 2018

3.0.2

3.0.2.0 http://www.popphp.org/

Pop View Component for Pop PHP Framework

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.6.0

 

The Development Requires

template php view pop pop php view template

29/01 2018

v2.x-dev

2.9999999.9999999.9999999-dev http://www.popphp.org/

Pop View Component for Pop PHP Framework

  Sources   Download

BSD-3-Clause New BSD

The Requires

  • php >=5.4.0

 

The Development Requires

template php view pop pop php view template

10/01 2018

3.0.1

3.0.1.0 http://www.popphp.org/

Pop View Component for Pop PHP Framework

  Sources   Download

New BSD

The Requires

  • php >=5.6.0

 

The Development Requires

template php view pop pop php view template

22/02 2017

3.0.0

3.0.0.0 http://www.popphp.org/

Pop View Component for Pop PHP Framework

  Sources   Download

New BSD

The Requires

  • php >=5.6.0

 

The Development Requires

template php view pop pop php view template

03/10 2016

2.1.1

2.1.1.0 http://www.popphp.org/

Pop View Component for Pop PHP Framework

  Sources   Download

New BSD

The Requires

  • php >=5.4.0

 

The Development Requires

template php view pop pop php view template

01/07 2016

2.1.0

2.1.0.0 http://www.popphp.org/

Pop View Component for Pop PHP Framework

  Sources   Download

New BSD

The Requires

  • php >=5.4.0

 

The Development Requires

template php view pop pop php view template

10/05 2016

2.0.0p7

2.0.0.0-patch7 http://www.popphp.org/

Pop View Component for Pop PHP Framework

  Sources   Download

New BSD

The Requires

  • php >=5.4.0

 

The Development Requires

template php view pop pop php view template

07/04 2016

2.0.0p6

2.0.0.0-patch6 http://www.popphp.org/

Pop View Component for Pop PHP Framework

  Sources   Download

New BSD

The Requires

  • php >=5.4.0

 

The Development Requires

template php view pop pop php view template

19/02 2016

2.0.0p5

2.0.0.0-patch5 http://www.popphp.org/

Pop View Component for Pop PHP Framework

  Sources   Download

New BSD

The Requires

  • php >=5.4.0

 

The Development Requires

template php view pop pop php view template

19/02 2016

2.0.0p4

2.0.0.0-patch4 http://www.popphp.org/

Pop View Component for Pop PHP Framework

  Sources   Download

New BSD

The Requires

  • php >=5.4.0

 

The Development Requires

template php view pop pop php view template

14/12 2015

2.0.0p3

2.0.0.0-patch3 http://www.popphp.org/

Pop View Component for Pop PHP Framework

  Sources   Download

New BSD

The Requires

  • php >=5.4.0

 

The Development Requires

template php view pop pop php view template

20/08 2015

2.0.0p2

2.0.0.0-patch2 http://www.popphp.org/

Pop View Component for Pop PHP Framework

  Sources   Download

New BSD

The Requires

  • php >=5.4.0

 

The Development Requires

template php view pop pop php view template

09/08 2015

2.0.0p1

2.0.0.0-patch1 http://www.popphp.org/

Pop View Component for Pop PHP Framework

  Sources   Download

New BSD

The Requires

  • php >=5.4.0

 

The Development Requires

template php view pop pop php view template

16/07 2015

2.0.0

2.0.0.0 http://www.popphp.org/

Pop View Component for Pop PHP Framework

  Sources   Download

New BSD

The Requires

  • php >=5.4.0

 

The Development Requires

template php view pop pop php view template