bnf/pug-view

Render PUG templates into a PSR-7 Response object.

2.0.0 2018-01-08 19:06 UTC

This package is auto-updated.

Last update: 2024-12-06 09:19:08 UTC


README

Build Status

PUG Renderer

This is a renderer for rendering PUG templates into a PSR-7 Response object. It works well with Slim Framework 3.

Installation

Install with Composer:

composer require bnf/pug-view

Usage with 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();

Usage with any PSR-7 Project

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

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