2017 © Pedro PelĂĄez
 

library twig-view

Slim Framework 3 view helper built on top of the Twig 2 templating component

image

slim/twig-view

Slim Framework 3 view helper built on top of the Twig 2 templating component

  • Monday, May 7, 2018
  • by akrabat
  • Repository
  • 34 Watchers
  • 189 Stars
  • 614,539 Installations
  • PHP
  • 186 Dependents
  • 7 Suggesters
  • 54 Forks
  • 1 Open issues
  • 12 Versions
  • 12 % Grown

The README.md

Slim Framework Twig View

Latest Version on Packagist Software License Build Status Coverage Status Total Downloads, (*1)

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., (*2)

Install

Via Composer, (*3)

composer require slim/twig-view

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

Usage

With DI Container

use DI\Container;
use Slim\Factory\AppFactory;
use Slim\Views\Twig;
use Slim\Views\TwigMiddleware;

require __DIR__ . '/../vendor/autoload.php';

// Create Container
$container = new Container();

// Set view in Container
$container->set(Twig::class, function() {
    return Twig::create(__DIR__ . '/../templates', ['cache' => 'path/to/cache']);
});

// Create App from container
$app = AppFactory::createFromContainer($container);

// Add Twig-View Middleware
$app->add(TwigMiddleware::create($app, $container->get(Twig::class)));

// Add other middleware
$app->addRoutingMiddleware();
$app->addErrorMiddleware(true, true, true);

// Render from template file templates/profile.html.twig
$app->get('/hello/{name}', function ($request, $response, $args) {
    $viewData = [
        'name' => $args['name'],
    ];

    $twig = $this->get(Twig::class);

    return $twig->render($response, 'profile.html.twig', $viewData);
})->setName('profile');

// Render from string
$app->get('/hi/{name}', function ($request, $response, $args) {
    $viewData = [
        'name' => $args['name'],
    ];

    $twig = $this->get(Twig::class);
    $str = $twig->fetchFromString('<p>Hi, my name is {{ name }}.</p>', $viewData);
    $response->getBody()->write($str);

    return $response;
});

// Run app
$app->run();

Without DI container

use Slim\Factory\AppFactory;
use Slim\Views\Twig;
use Slim\Views\TwigMiddleware;

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.twig', [
        'name' => $args['name']
    ]);
})->setName('profile');

// Render from string
$app->get('/hi/{name}', function ($request, $response, $args) {
    $view = Twig::fromRequest($request);
    $str = $view->fetchFromString(
        '

Hi, my name is {{ name }}., (*5)

', [ 'name' => $args['name'] ] ); $response->getBody()->write($str); return $response; }); // Run app $app->run();

Custom template functions

TwigExtension provides these functions to your Twig templates:, (*6)

  • 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.: https://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:, (*7)

<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>

Tests

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

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

Contributing

Please see CONTRIBUTING for details., (*9)

Security

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

Credits

License

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

The Versions

07/05 2018

dev-master

9999999-dev http://slimframework.com

Slim Framework 3 view helper built on top of the Twig 2 templating component

  Sources   Download

MIT

The Requires

 

The Development Requires

framework twig template slim view

07/05 2018

2.4.0

2.4.0.0 http://slimframework.com

Slim Framework 3 view helper built on top of the Twig 2 templating component

  Sources   Download

MIT

The Requires

 

The Development Requires

framework twig template slim view

20/09 2017

2.3.0

2.3.0.0 http://slimframework.com

Slim Framework 3 view helper built on top of the Twig 2 templating component

  Sources   Download

MIT

The Requires

 

The Development Requires

framework twig template slim view

25/01 2017

2.2.0

2.2.0.0 http://slimframework.com

Slim Framework 3 view helper built on top of the Twig 2 templating component

  Sources   Download

MIT

The Requires

 

The Development Requires

framework twig template slim view

13/03 2016

2.1.1

2.1.1.0 http://slimframework.com

Slim Framework 3 view helper built on top of the Twig templating component

  Sources   Download

MIT

The Requires

 

The Development Requires

framework twig template slim view

07/03 2016

2.1.0

2.1.0.0 http://slimframework.com

Slim Framework 3 view helper built on top of the Twig templating component

  Sources   Download

MIT

The Requires

 

The Development Requires

framework twig template slim view

17/08 2015

2.0

2.0.0.0 http://slimframework.com

Slim Framework view helper built on top of the Twig templating component

  Sources   Download

MIT

The Requires

 

The Development Requires

framework twig template slim view

11/07 2015

1.2.0

1.2.0.0 http://slimframework.com

Slim Framework view helper built on top of the Twig templating component

  Sources   Download

MIT

The Requires

 

The Development Requires

framework twig template slim view

28/06 2015

1.1.1

1.1.1.0 http://slimframework.com

Slim Framework view helper built on top of the Twig templating component

  Sources   Download

MIT

The Requires

 

The Development Requires

framework twig template slim view

29/05 2015

1.1.0

1.1.0.0 http://slimframework.com

Slim Framework view helper built on top of the Twig templating component

  Sources   Download

MIT

The Requires

 

The Development Requires

framework twig template slim view

27/05 2015

1.1

1.1.0.0 http://slimframework.com

Slim Framework view helper built on top of the Twig templating component

  Sources   Download

MIT

The Requires

 

The Development Requires

framework twig template slim view

14/03 2015

1.0

1.0.0.0 http://slimframework.com

Slim Framework view helper built on top of the Twig templating component

  Sources   Download

MIT

The Requires

 

The Development Requires

framework twig template slim view