bnf/pug-view

将 PUG 模板渲染成 PSR-7 响应对象。

2.0.0 2018-01-08 19:06 UTC

This package is auto-updated.

Last update: 2024-09-06 08:47:15 UTC


README

Build Status

PUG 渲染器

这是一个将 PUG 模板渲染成 PSR-7 响应对象的渲染器。它与 Slim Framework 3 兼容良好。

安装

使用 Composer 安装

composer require bnf/pug-view

与 Slim 3 一起使用

use Bnf\PugView\PugRenderer;

include 'vendor/autoload.php';

$app = new Slim\App();
$container = $app->getContainer();

$settings = [
    'extension' => '.pug',
    'basedir' => 'templates/'
];

$container['view'] = function($c) {
	return new PugRenderer($settings);
};

/* PugRenderer is added as middleware to automatically inject the $response object. */
$app->add('view');

/* Add global template variables */
$app->add(function($request, $response, $next) {
    $this->view->set('title', 'default title');
    // Make the container accessible in the view, so that every object can be accessed in the template:
    // E.g: a(href=c.router.pathFor('named-route'))
    $this->view->set('c', $this);

    return $next($request, $response);
});

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

$app->run();

与任何 PSR-7 项目一起使用

//Construct the View
$settings = [
    'extension' => '.pug',
    'basedir' => 'templates/'
];
$phpView = new PugRenderer($settings);

//Render a Template
$response = $phpView->render('template', $yourData, new Response());