PHP code example of adbario / slim-twig-view

1. Go to this page and download the library: Download adbario/slim-twig-view library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

adbario / slim-twig-view example snippets


// Create Slim app
$app = new \Slim\App();

// Fetch DI Container
$container = $app->getContainer();

// Register Twig View helper
$container['view'] = function ($c) {
    $view = new \Adbar\Slim\Views\Twig('path/to/templates', [
        'cache' => 'path/to/cache',
        // Optional template file extension, defaults to 'twig'
        'file_extension' => 'html'
    ]);

    // 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) {
    // Render template file from path 'path/to/templates/user/profile.html'
    return $this->view->render($response, 'user.profile', [
        'name' => $args['name']
    ]);
})->setName('profile');

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