Slim Framework Twig View

This is a Slim Framework view helper built on top of the Twig templating component. You can use this component to create and render templates in your Slim Framework application.

Install

Via Composer

$ composer require slim/twig-view

Requires Slim Framework 4, Twig 3 and PHP 7.4 or newer.

Usage

use DIContainer; use SlimFactoryAppFactory; use SlimViewsTwig; use SlimViewsTwigMiddleware; require __DIR__ . '/vendor/autoload.php'; // Create Container $container = new Container(); AppFactory::setContainer($container); // Set view in Container $container->set('view', function() { return Twig::create('path/to/templates', ['cache' => 'path/to/cache']); }); // Create App $app = AppFactory::create(); // Add Twig-View Middleware $app->add(TwigMiddleware::createFromContainer($app)); // Define named route $app->get('/hello/{name}', function ($request, $response, $args) { return $this->get('view')->render($response, 'profile.html', [ 'name' => $args['name'] ]); })->setName('profile'); // Render from string $app->get('/hi/{name}', function ($request, $response, $args) { $str = $this->get('view')->fetchFromString( '<p>Hi, my name is {{ name }}.</p>', [ 'name' => $args['name'] ] ); $response->getBody()->write($str); return $response; }); // Run app $app->run();

Without container

use SlimFactoryAppFactory; use SlimViewsTwig; use SlimViewsTwigMiddleware; require __DIR__ . '/vendor/autoload.php'; // Create App $app = AppFactory::create(); // Create Twig $twig = Twig::create('path/to/templates', ['cache' => 'path/to/cache']); // Add Twig-View Middleware $app->add(TwigMiddleware::create($app, $twig)); // Define named route $app->get('/hello/{name}', function ($request, $response, $args) { $view = Twig::fromRequest($request); return $view->render($response, 'profile.html', [ 'name' => $args['name'] ]); })->setName('profile'); // Render from string $app->get('/hi/{name}', function ($request, $response, $args) { $view = Twig::fromRequest($request); $str = $view->fetchFromString( '<p>Hi, my name is {{ name }}.</p>', [ 'name' => $args['name'] ] ); $response->getBody()->write($str); return $response; }); // Run app $app->run();

Custom template functions

TwigExtension provides these functions to your Twig templates:

url_for() - returns the URL for a given route. e.g.: /hello/world full_url_for() - returns the URL for a given route. e.g.: http://www.example.com/hello/world is_current_url() - returns true is the provided route name and parameters are valid for the current path. current_url() - returns the current path, with or without the query string. get_uri() - returns the UriInterface object from the incoming ServerRequestInterface object base_path() - returns the base path.

You can use url_for to generate complete URLs to any Slim application named route and use is_current_url to determine if you need to mark a link as active as shown in this example Twig template:

{% extends "layout.html" %} {% block body %} <h1>User List</h1> <ul> <li><a href="{{ url_for('profile', { 'name': 'josh' }) }}" {% if is_current_url('profile', { 'name': 'josh' }) %}class="active"{% endif %}>Josh</a></li> <li><a href="{{ url_for('profile', { 'name': 'andrew' }) }}">Andrew</a></li> </ul> {% endblock %}

Tests

To execute the test suite, you'll need to clone the repository and install the dependencies.

$ git clone https://github.com/slimphp/Twig-View $ composer install $ composer test

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email security@slimframework.com instead of using the issue tracker.

Credits

Josh Lockhart Pierre Bérubé

License

The MIT License (MIT). Please see License File for more information.

版权声明:

1、该文章(资料)来源于互联网公开信息,我方只是对该内容做点评,所分享的下载地址为原作者公开地址。
2、网站不提供资料下载,如需下载请到原作者页面进行下载。
3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考学习用!
4、如文档内容存在违规,或者侵犯商业秘密、侵犯著作权等,请点击“违规举报”。