geggleto/slim-renderer 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

geggleto/slim-renderer

最新稳定版本:3.1.0

Composer 安装命令:

composer require geggleto/slim-renderer

包简介

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

README 文档

README

Build Status

PHP Renderer

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

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 laminas-escaper. Alternatively, consider Twig-View.

Installation

Install with Composer:

composer require slim/php-view

Usage with Slim 4

use Slim\Views\PhpRenderer;

include "vendor/autoload.php";

$app = Slim\AppFactory::create();

$app->get('/hello/{name}', function ($request, $response, $args) {
    $renderer = new PhpRenderer('path/to/templates');
    return $renderer->render($response, "hello.php", $args);
});

$app->run();

Note that you could place the PhpRenderer instantiation within your DI Container.

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.

// 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($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. If using a layout the fetch() method can be used instead of render() to avoid appling the layout to the sub-template.

<?=$this->fetch('./path/to/partial.phtml', ["name" => "John"])?>

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.

Create your layout path/to/templates/layout.php.

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

Create your view template path/to/templates/hello.php.

Hello <?=$name?>!

Rendering in your code.

$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

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

Exceptions

\RuntimeException - if template does not exist

\InvalidArgumentException - if $data contains 'template'

统计信息

  • 总下载量: 24
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 220
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 220
  • Watchers: 15
  • Forks: 59
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-09-29