2017 © Pedro Peláez
 

library php-view

Render PHP view scripts into a PSR-7 Response object.

image

slim/php-view

Render PHP view scripts into a PSR-7 Response object.

  • Wednesday, July 5, 2017
  • by akrabat
  • Repository
  • 12 Watchers
  • 119 Stars
  • 810,304 Installations
  • PHP
  • 62 Dependents
  • 2 Suggesters
  • 38 Forks
  • 5 Open issues
  • 15 Versions
  • 13 % Grown

The README.md

Build Status, (*1)

PHP Renderer

This is a renderer for rendering PHP view scripts into a PSR-7 Response object. It works well with Slim Framework 3., (*2)

Cross-site scripting (XSS) risks

Note that PHP-View has no built-in mitigation from XSS attacks. It is the developer's responsibility to use htmlspecialchars() or a component like zend-escaper. Alternatively, consider Twig-View., (*3)

Installation

Install with Composer:, (*4)

composer require slim/php-view

Usage with Slim 3

use Slim\Views\PhpRenderer;

include "vendor/autoload.php";

$app = new Slim\App();
$container = $app->getContainer();
$container['renderer'] = new PhpRenderer("./templates");

$app->get('/hello/{name}', function ($request, $response, $args) {
    return $this->renderer->render($response, "hello.php", $args);
});

$app->run();

Usage with any PSR-7 Project

//Construct the View
$phpView = new PhpRenderer("./path/to/templates");

//Render a Template
$response = $phpView->render(new Response(), "hello.php", $yourData);

Template Variables

You can now add variables to your renderer that will be available to all templates you render., (*5)

// via the constructor
$templateVariables = [
    "title" => "Title"
];
$phpView = new PhpRenderer("./path/to/templates", $templateVariables);

// or setter
$phpView->setAttributes($templateVariables);

// or individually
$phpView->addAttribute($key, $value);

Data passed in via ->render() takes precedence over attributes., (*6)

$templateVariables = [
    "title" => "Title"
];
$phpView = new PhpRenderer("./path/to/templates", $templateVariables);

//...

$phpView->render($response, $template, [
    "title" => "My Title"
]);
// In the view above, the $title will be "My Title" and not "Title"

Sub-templates

Inside your templates you may use $this to refer to the PhpRenderer object to render sub-templates., (*7)

Rendering in Layouts

You can now render view in another views called layouts, this allows you to compose modular view templates and help keep your views DRY., (*8)

Create your layout ./path/to/templates/layout.php., (*9)

<html><head><title><?=$title?></title></head><body><?=$content?></body></html>

Create your view template ./path/to/templates/hello.php., (*10)

Hello <?=$name?>!

Rendering in your code., (*11)

$phpView = new PhpRenderer("./path/to/templates", ["title" => "My App"]);
$phpView->setLayout("layout.php");

//...

$phpview->render($response, "hello.php", ["title" => "Hello - My App", "name" => "John"]);

Response will be, (*12)

<html><head><title>Hello - My App</title></head><body>Hello John!</body></html>

Please note, the $content is special variable used inside layouts to render the wrapped view and should not be set in your view paramaters., (*13)

Exceptions

\RuntimeException - if template does not exist, (*14)

\InvalidArgumentException - if $data contains 'template', (*15)

The Versions

05/07 2017

dev-master

9999999-dev

Render PHP view scripts into a PSR-7 Response object.

  Sources   Download

MIT

The Requires

 

The Development Requires

framework template php renderer slim view phtml

11/10 2016

2.2.0

2.2.0.0

Render PHP view scripts into a PSR-7 Response object.

  Sources   Download

MIT

The Requires

 

The Development Requires

framework template php renderer slim view phtml

04/03 2016

2.1.0

2.1.0.0

Render PHP view scripts into a PSR-7 Response object.

  Sources   Download

MIT

The Requires

 

The Development Requires

framework template php renderer slim view phtml

08/12 2015

2.0.6

2.0.6.0

Render PHP view scripts into a PSR-7 Response object.

  Sources   Download

MIT

The Requires

 

The Development Requires

framework template php renderer slim view phtml

13/11 2015

2.0.5

2.0.5.0

Render PHP view scripts into a PSR-7 Response object.

  Sources   Download

MIT

The Requires

 

The Development Requires

framework template php renderer slim view phtml

11/11 2015

2.0.4

2.0.4.0

Render PHP view scripts into a PSR-7 Response object.

  Sources   Download

MIT

The Requires

 

framework template php renderer slim view phtml

10/11 2015

2.0.3

2.0.3.0

Render PHP view scripts into a PSR-7 Response object.

  Sources   Download

MIT

The Requires

 

framework template php renderer slim view phtml

16/10 2015

2.0.2

2.0.2.0

Render PHP view scripts into a PSR-7 Response object.

  Sources   Download

MIT

The Requires

 

framework template php renderer slim view phtml

01/10 2015

2.0.1

2.0.1.0

  Sources   Download

MIT

The Requires

 

01/10 2015

2.0.0

2.0.0.0

  Sources   Download

MIT

The Requires

 

01/10 2015

2.0.0-RC1

2.0.0.0-RC1

  Sources   Download

MIT

The Requires

 

29/09 2015

1.0.3

1.0.3.0

  Sources   Download

MIT

The Requires

 

29/09 2015

1.0.2

1.0.2.0

  Sources   Download

MIT

The Requires

 

29/09 2015

1.0.1

1.0.1.0

  Sources   Download

MIT

The Requires

 

29/09 2015

1

1.0.0.0

  Sources   Download

MIT

The Requires