danjam/slim-mustache-view

使用 Mustache.php 的简单 Slim 3 框架视图渲染器,用于 mustache 模板

1.1.2 2016-12-19 10:45 UTC

This package is auto-updated.

Last update: 2024-08-29 00:23:38 UTC


README

Build Status Latest Stable Version Coverage Status SensioLabsInsight

简单的 Slim 3 框架 视图渲染器,用于 mustache 模板,使用 Mustache.php

安装

通过 Composer

$ composer require danjam/slim-mustache-view

用法

// 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();

构造函数接受一个可选的 Mustache.php 选项数组。有关详细信息,请参阅 Mustache.php 文档

// 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;
};

如果需要,您还可以捕获原始模板内容。这对于渲染内联模板很有用,例如当同时使用 mustache.js

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

测试

phpunit

致谢

许可证

MIT 许可证 (MIT)。有关更多信息,请参阅 许可证文件