danjam/slim-mustache-view

Simple Slim 3 framework view renderer for mustache templates using Mustache.php

1.1.2 2016-12-19 10:45 UTC

This package is auto-updated.

Last update: 2024-10-29 00:42:32 UTC


README

Build Status Latest Stable Version Coverage Status SensioLabsInsight

Simple Slim 3 framework view renderer for mustache templates using Mustache.php

Install

Via Composer

$ composer require danjam/slim-mustache-view

Usage

// create Slim 3 app
$app = new \Slim\App();

// get the container
$container = $app->getContainer();

// register Mustache view
$container['view'] = function ($container) {
    $view = new \Slim\Views\Mustache();
    
    return $view;
};

// define the route
$app->get('/hello/{name}', function ($request, $response, $args) {
    return $this->view->render($response, 'Hello, {{name}}', [
        'name' => $args['name']
    ]);
});

// run the app
$app->run();

The constructor takes an optional array of Mustache.php options. See the Mustache.php documentation for details.

// register Mustache view
$container['view'] = function () {
    $view = new \Slim\Views\Mustache([
        'cache' => './cache/mustache',
        'loader' => new Mustache_Loader_FilesystemLoader('./views'),
        'partials_loader' => new Mustache_Loader_FilesystemLoader('./views/partials')
    ]);

    return $view;
};

You can also capture raw template contents if needed. This can be useful for rendering inline templates, for example when also using mustache.js

$this->view->getRawTemplate('some-template.html');

Testing

phpunit

Credits

License

The MIT License (MIT). Please see License File for more information.