adbario / slim-twig-view
基于Twig 2模板组件构建的Slim Framework 3视图助手扩展
1.0.0
2017-07-20 19:16 UTC
Requires
- php: >=5.5
- slim/slim: ~3.0
- slim/twig-view: ^1.0|^2.0
This package is auto-updated.
Last update: 2024-09-11 14:57:09 UTC
README
这是Slim Framework Twig View的一个扩展,具有以下额外功能
- 自动添加文件扩展名到模板名称
- 使用点表示法表示模板文件路径
安装
composer require adbario/slim-twig-view
使用方法
模板文件的默认扩展名为'twig',在注册Slim容器上的Twig视图时可以通过可选设置进行更改。
// Create Slim app $app = new \Slim\App(); // Fetch DI Container $container = $app->getContainer(); // Register Twig View helper $container['view'] = function ($c) { $view = new \Adbar\Slim\Views\Twig('path/to/templates', [ 'cache' => 'path/to/cache', // Optional template file extension, defaults to 'twig' 'file_extension' => 'html' ]); // Instantiate and add Slim specific extension $basePath = rtrim(str_ireplace('index.php', '', $c['request']->getUri()->getBasePath()), '/'); $view->addExtension(new Slim\Views\TwigExtension($c['router'], $basePath)); return $view; }; // Define named route $app->get('/hello/{name}', function ($request, $response, $args) { // Render template file from path 'path/to/templates/user/profile.html' return $this->view->render($response, 'user.profile', [ 'name' => $args['name'] ]); })->setName('profile'); // Run app $app->run();
自定义模板函数
更多请参阅 Slim Framework Twig View