8000 GitHub - ncou/Chiron-PhpRenderer: Simple php render for PSR7 response
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
This repository was archived by the owner on Aug 16, 2020. It is now read-only.

ncou/Chiron-PhpRenderer

Repository files navigation

Build Status Coverage Status CodeCov

Total Downloads Monthly Downloads

StyleCI PHP-Eye PHPStan

PHP Renderer

This is a renderer for rendering PHP view scripts into a PSR-7 Response object. It works well with Chiron Framework.

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.

Templates

You may use $this inside your php templates. $this will be the actual PhpRenderer object will allow you to render sub-templates

Installation

Install with Composer:

composer require chiron/php-renderer

Usage with Chiron

use Chiron\Views\PhpRenderer;

include "vendor/autoload.php";

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

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

$app->run();

Usage with any PSR-7 Project

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

//Render a Template
$text = $phpView->render("/path/to/template.php", $yourData);
$response = $response->write($text);

Template Variables

You can now add variables to your renderer that will be available to all templates you render.

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

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

//...

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

Exceptions

\RuntimeException - if template does not exist

About

Simple php render for PSR7 response

Resources

License

Code of conduct

Stars

Watchers

Forks

Sponsor this project

Packages

No packages published
0