In this repo i store all my websites, each in a different branch
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

93 regels
2.5 KiB

  1. # Slim Framework Twig View
  2. [![Build Status](https://travis-ci.org/slimphp/Twig-View.svg?branch=master)](https://travis-ci.org/slimphp/Twig-View)
  3. 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. It works with Twig 1.18+ (PHP5.5+) and with Twig 2 (PHP7).
  4. ## Install
  5. Via [Composer](https://getcomposer.org/)
  6. ```bash
  7. $ composer require slim/twig-view
  8. ```
  9. Requires Slim Framework 3 and PHP 5.5.0 or newer.
  10. ## Usage
  11. ```php
  12. // Create Slim app
  13. $app = new \Slim\App();
  14. // Fetch DI Container
  15. $container = $app->getContainer();
  16. // Register Twig View helper
  17. $container['view'] = function ($c) {
  18. $view = new \Slim\Views\Twig('path/to/templates', [
  19. 'cache' => 'path/to/cache'
  20. ]);
  21. // Instantiate and add Slim specific extension
  22. $basePath = rtrim(str_ireplace('index.php', '', $c['request']->getUri()->getBasePath()), '/');
  23. $view->addExtension(new \Slim\Views\TwigExtension($c['router'], $basePath));
  24. return $view;
  25. };
  26. // Define named route
  27. $app->get('/hello/{name}', function ($request, $response, $args) {
  28. return $this->view->render($response, 'profile.html', [
  29. 'name' => $args['name']
  30. ]);
  31. })->setName('profile');
  32. // Render from string
  33. $app->get('/hi/{name}', function ($request, $response, $args) {
  34. $str = $this->view->fetchFromString('<p>Hi, my name is {{ name }}.</p>', [
  35. 'name' => $args['name']
  36. ]);
  37. $response->getBody()->write($str);
  38. return $response;
  39. });
  40. // Run app
  41. $app->run();
  42. ```
  43. ## Custom template functions
  44. This component exposes a custom `path_for()` function to your Twig templates. You can use this function to generate complete URLs to any Slim application named route. This is an example Twig template:
  45. {% extends "layout.html" %}
  46. {% block body %}
  47. <h1>User List</h1>
  48. <ul>
  49. <li><a href="{{ path_for('profile', { 'name': 'josh' }) }}" {% if is_current_path('profle', { 'name': 'josh' }) %}class="active"{% endif %}>Josh</a></li>
  50. <li><a href="{{ path_for('profile', { 'name': 'andrew' }) }}">Andrew</a></li>
  51. </ul>
  52. {% endblock %}
  53. ## Testing
  54. ```bash
  55. phpunit
  56. ```
  57. ## Contributing
  58. Please see [CONTRIBUTING](CONTRIBUTING.md) for details.
  59. ## Security
  60. If you discover any security related issues, please email security@slimframework.com instead of using the issue tracker.
  61. ## Credits
  62. - [Josh Lockhart](https://github.com/codeguy)
  63. ## License
  64. The MIT License (MIT). Please see [License File](LICENSE.md) for more information.