Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Port for Slim 4 Usage #105

merged 9 commits into from Apr 28, 2019


None yet
1 participant
Copy link

commented Apr 28, 2019

This is a port of Twig-View for usage with the new Slim 4 interfaces which requires a major release.

The Twig object's API remains the same. The TwigExtension interface has changed to reflect the new naming convention from Slim 4's RouteParser:

The new public interface for TwigExtension:

  • TwigExtension::urlFor() - Formerly pathFor()
  • TwigExtension::fullUrlFor() - Stays the same
  • TwigExtension::isCurrentUrl() - Formerly isCurrentPath()
  • TwigExtension::getCurrentUrl() - Formerly currentPath()
  • TwigExtension::getBasePath()
  • TwigExtension::setBasePath()
  • TwigExtension::getUri() - Formerly baseUrl()
  • TwigExtension:setUri()

Example usage with PHP-DI

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

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

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

// Create App
$app = new AppFactory::create();

// Add Twig-View Middleware
$basePath = '/base-path';
$routeParser = $app->getRouteCollector()->getRouteParser();
$twig = new Twig('path/to/templates', ['cache' => 'path/to/cache']);
$twigMiddleware = new TwigMiddleware($twig, $container, $routeParser, $basePath);

// Define named route
$app->get('/hello/{name}', function ($request, $response, $args) {
    return $this->view->render($response, 'profile.html', [
        'name' => $args['name']

// Render from string
$app->get('/hi/{name}', function ($request, $response, $args) {
    $str = $this->view->fetchFromString('<p>Hi, my name is {{ name }}.</p>', [
        'name' => $args['name']
    return $response;

// Run app

@l0gicgate l0gicgate changed the base branch from master to 3.x Apr 28, 2019

@l0gicgate l0gicgate added this to the 3.0.0-alpha milestone Apr 28, 2019

@l0gicgate l0gicgate merged commit fed1bec into slimphp:3.x Apr 28, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.