gigablah / silex-view
Silex 的引擎无关视图组件
Requires
- silex/silex: ~1.0
Requires (Dev)
- aura/view: ~1.2
- mustache/mustache: ~2.4
- phpunit/phpunit: ~3.7
- smarty/smarty: ~3.1
- twig/twig: ~1.0
This package is auto-updated.
Last update: 2024-08-24 03:48:38 UTC
README
ViewServiceProvider 为您的 Silex 应用程序提供引擎无关的模板功能。
安装
使用 Composer 通过将其添加到您的 composer.json
中来安装 gigablah/silex-view 库。您还需要一个渲染引擎,例如 Mustache。
{ "require": { "silex/silex": "~1.0", "mustache/mustache": "~2.4", "gigablah/silex-view": "~0.0.1" } }
用法
只需注册服务提供程序,并可选地传递一些默认值。
$app->register(new Gigablah\Silex\View\ViewServiceProvider(), array( 'view.globals' => array('foo' => 'bar'), 'view.default_engine' => 'mustache' ));
提供程序注册了 ArrayToViewListener
,它拦截控制器输出并将其包装在 View
对象中。为了使其正常工作,您必须从控制器函数返回一个数据数组。
视图
通常您不需要自己实例化任何视图实体;监听器会转换您的控制器输出。如果您想手动进行,语法如下
$view = $app['view']->create($template = '/path/to/template', $context = array('foo' => 'bar'));
可以通过调用 render()
函数或转换为字符串来渲染视图
$output = $view->render(); $output = (string) $view;
再次强调,您通常不需要手动渲染视图,因为它们将由 Response
对象处理。
视图上下文
视图实体只是一个 ArrayObject
的实例,因此您可以使用常规数组表示法设置上下文,并使用如 with()
这样的便利函数。
$view['foo'] = 'bar'; $view->with(array('foo' => 'bar'));
要插入全局上下文,请使用 share()
$view->share(array('foo' => 'bar'));
您可以通过覆盖 view.globals
来初始化全局上下文。
解析模板
监听器如何知道使用哪个模板?默认情况下,它从请求实体中读取 _route
属性并将其转换为小写,然后根据 view.default_engine
的值添加扩展。以下是一些示例
$app->get('/foobar', function () {}); // get_foobar.mustache $app->get('/', function () {}); // get_.mustache $app->match('/', function () {}); // _.mustache
由于您可能希望模板名称更具有描述性,因此可以使用命名路由
$app->match('/', function () {})->bind('home'); // home.mustache
您还可以在请求中设置 _template
属性,或者将其作为控制器输出的部分
$app->get('/foo', function (Symfony\Component\HttpFoundation\Request $request) { $request->attributes->set('_template', 'foo.html'); }); $app->get('/bar', function () { return array('_template' => 'bar.html'); });
如果您需要生成模板路径的自定义逻辑,您可以创建自己的类,该类实现 TemplateResolverInterface
并覆盖 view.template_resolver
。
引擎
此库不处理任何实际的视图渲染;该任务委托给您选择的模板库。目前提供了以下适配器
有一个特殊的 DelegatingEngine
,它充当多个不同引擎的注册表,根据模板文件扩展名选择合适的引擎。由于 Aura.View、Plates 和原始 PHP 都使用相同的默认文件扩展名 (.php),您需要手动配置扩展映射,如下所示
$app->register(new Gigablah\Silex\View\ViewServiceProvider(), array( 'view.default_engine' => 'php', 'view.engines' => array( 'php' => 'view.engine.plates' ) ));
组合视图
视图可以嵌套在其他视图内部
$view->nest($app['view']->create('foobar.html'), 'section');
对于单个视图,它相当于
$view['section'] = $app['view']->create('foobar.html');
然而,区别在于在相同位置嵌套多个视图。这样做会将子视图并排放置,而不是覆盖
$view->nest($app['view']->create('foobar.html'), 'section'); $view->nest($app['view']->create('foobar.html'), 'section'); // foobar.html is now repeated twice
更重要的是,您可以混合和匹配不同的引擎
$mustacheView = $app['view']->create('foo.mustache'); $smartyView = $app['view']->create('bar.tpl')->nest($mustacheView, 'section');
嵌套视图将继承其父视图的上下文。
异常处理
所有渲染异常都被捕获并存储在共享的 ExceptionBag
中。
要访问最后抛出的异常,或返回所有异常
$exception = $app['view']->getExceptionBag()->pop(); $exceptions = $app['view']->getExceptionBag()->all();
更多示例
您可以在演示应用程序中查看各种使用场景的代码示例。
许可证
本软件采用MIT许可证发布。有关详细信息,请参阅LICENSE文件。