ibrahim / twig-view
基于Twig模板组件构建的Slim Framework 3视图助手
2.1.1
2016-03-13 20:58 UTC
Requires
- php: >=5.5.0
- psr/http-message: ^1.0
- twig/twig: ^1.18
Requires (Dev)
- phpunit/phpunit: ^4.8.0
This package is not auto-updated.
Last update: 2024-09-23 11:58:13 UTC
README
这是一个基于Twig模板组件构建的Slim Framework视图助手。您可以使用此组件在Slim Framework应用程序中创建和渲染模板。
安装
通过Composer
$ composer require slim/twig-view
需要Slim Framework 3和PHP 5.5.0或更高版本。
使用方法
// Create Slim app $app = new \Slim\App(); // Fetch DI Container $container = $app->getContainer(); // Register Twig View helper $container['view'] = function ($c) { $view = new \Slim\Views\Twig('path/to/templates', [ 'cache' => 'path/to/cache' ]); // 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) { return $this->view->render($response, 'profile.html', [ 'name' => $args['name'] ]); })->setName('profile'); // Run app $app->run();
自定义模板函数
此组件向您的Twig模板公开一个自定义的path_for()
函数。您可以使用此函数生成任何命名路由的完整URL。以下是一个示例Twig模板
{% extends "layout.html" %}
{% block body %}
<h1>User List</h1>
<ul>
<li><a href="{{ path_for('profile', { 'name': 'josh' }) }}">Josh</a></li>
</ul>
{% endblock %}
测试
phpunit
贡献
有关详细信息,请参阅CONTRIBUTING
安全性
如果您发现任何与安全性相关的问题,请通过security@slimframework.com发送电子邮件,而不是使用问题跟踪器。
鸣谢
许可证
MIT许可证(MIT)。有关更多信息,请参阅许可证文件