A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://github.com/slimphp/Twig-View below:

slimphp/Twig-View: Slim Framework view helper built on top of the Twig templating component

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.

Via Composer

composer require slim/twig-view

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

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();
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(
        '<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:

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:

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

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

Please see CONTRIBUTING for details.

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

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


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4