clippings / mustache-provider
Pimple 和 Silex 的 Mustache 服务提供者。
This package is auto-updated.
Last update: 2024-09-19 18:07:08 UTC
README
为 Mustache 集成提供对 Pimple 依赖注入容器或 Silex 应用微框架的支持。
安装
将 clippings/mustache-provider
添加到您项目的 Composer 依赖项中
composer require clippings/mustache-provider
配置
-
mustache.path
(可选):包含 Mustache 模板文件的目录路径。 -
mustache.partials_path
(可选):包含 Mustache 部分模板文件的目录路径。如果未指定,则默认为mustache.path
。 -
mustache.partials
(可选):模板名称到模板内容的关联数组。如果您想内联定义部分,请使用此选项。 -
mustache.helpers
(可选):Mustache 辅助函数的关联数组。有关更多信息,请参阅 Mustache.php 辅助函数文档。 -
mustache.options
(可选):Mustache 选项的关联数组。有关更多信息,请参阅 Mustache.php 文档。
服务
-
mustache.loader
(可选):Mustache 模板加载器实例。此加载器将使用您提供的mustache.path
选项。您还可以用自己的东西替换此加载器。 -
mustache.partials_loader
(可选):用于加载部分的 Mustache 模板加载器。默认情况下,它将加载来自您的mustache.partials_path
或mustache.partials
配置选项的模板。您还可以用您选择的另一个加载器替换部分加载器。
注册
<?php $app->register(new Mustache\MustacheServiceProvider, array( 'mustache.path' => __DIR__.'/../views', 'mustache.options' => array( 'cache' => __DIR__.'/../tmp/cache/mustache', ), ));
使用方法
Mustache 提供者提供了一个 mustache
服务
<?php $app->get('/hello/{name}', function ($name) use($app) { return $app['mustache']->render('hello', array( 'name' => ucfirst($name), )); });
这将渲染应用程序的 views
目录中的 hello.mustache
文件。
模板加载
默认情况下,Mustache 服务提供者附带了一个 Filesystem 模板加载器。要开始,只需设置一个 mustache.path
选项,告诉它模板在哪里即可。但您可以将该加载器替换为任何其他 Mustache Loader。
要获得更多精彩内容,请查看 Inline 模板加载器
<?php // ... $app->register(new MustacheServiceProvider, array( 'mustache.loader' => new Mustache_Loader_InlineLoader(__FILE__, __COMPILER_HALT_OFFSET__) )); $app->get('/{name}', function($name) use ($app) { return $app['mustache']->render('hello', compact('name')); }) ->value('name', 'world'); // ... __halt_compiler(); @@ hello Hello, {{ name }}!
特性
Mustache\MustacheApplicationTrait
为您的应用程序添加了一个 render
辅助函数
<?php use Silex\Application; class MyApplication extends Application { use \Mustache\MustacheApplicationTrait; } $app = new MyApplication;
现在您可以调用 render
<?php return $app->render('hello', array('name' => 'Justin'));
或 BYO 响应
<?php $response = new Response; $response->setTtl(10); return $app->render('hello', array('name' => 'Justin'), $response);
它还提供了一个 renderTemplate
辅助函数,它返回一个渲染后的字符串而不是一个 Response
对象。
自定义
您可以通过扩展 mustache
服务来在使用它之前修改 Mustache
<?php $app['mustache'] = $app->factory($app->extend('mustache', function ($mustache, $app) { $mustache->addHelper('app', $app); $mustache->setLogger($app['monolog']); return $mustache; }));