maps82 / smarty-view
Slim 框架视图助手,基于 Smarty 模板引擎构建
0.4
2015-10-17 16:25 UTC
Requires
- php: >=5.5.0
- psr/http-message: ^1.0
- smarty/smarty: ~3.1.27
This package is auto-updated.
Last update: 2024-09-13 02:13:28 UTC
README
这是一个基于 Smarty 模板组件构建的 Slim 框架视图助手。您可以使用此组件在 Slim 框架应用程序中创建和渲染模板。
安装
通过 Composer
$ composer require maps82/smarty-view
需要 Slim 框架 3。
使用方法
// Create Slim app $app = new \Slim\App(); // Fetch DI Container $container = $app->getContainer(); // Register Smarty View helper $container['view'] = function ($c) { $view = new \Slim\Views\Smarty('path/to/templates', [ 'compile' => 'path/to/compile', 'config' => 'path/to/config', // optional 'cache' => 'path/to/cache', // optional - omit this to disable caching 'debug' => false ]); // Instantiate and add Slim specific extension $view->addExtension(new Slim\Views\SmartyExtension( $c['router'], $c['request']->getUri() )); return $view; }; // Define named route $app->get('/hello/{name}', function ($request, $response, $args) { return $this->view->render($response, 'profile.tpl', [ 'name' => $args['name'] ]); })->setName('profile'); // Run app $app->run();
自定义模板函数
此组件向您的 Smarty 模板公开了一个自定义的 path_for
函数。您可以使用此函数生成任何 Slim 应用程序命名路由的完整 URL。以下是一个示例 Smarty 模板
{extends "layout.tpl"}
{block name="body"}
<h1>User List</h1>
<ul>
<li><a href="{path_for name="profile" options=["name" => "josh"]}">Josh</a></li>
</ul>
{/block}
致谢
此组件在官方 slim/twig-view 上有很强的依赖
许可证
MIT 许可证(MIT)。请参阅 许可证文件 获取更多信息。