webman icon indicating copy to clipboard operation
webman copied to clipboard

Handling Twig extensions

Open vilords opened this issue 3 years ago • 4 comments

Would be great if we could add extensions somehow in config without modifying the actual /support/view/Twig.php file

public static function render($template, $vars, $app = null)
{
    static $views = [], $view_suffix;
    $view_suffix = $view_suffix ? : \config('view.view_suffix', 'html');
    $app = $app === null ? \request()->app : $app;
    if (!isset($views[$app])) {
        $view_path = $app === '' ? \app_path(). '/view/' : \app_path(). "/$app/view/";
        $views[$app] = new Environment(new FilesystemLoader($view_path), \config('view.options', []));

        $views[$app]->addExtension(new \Jasny\Twig\DateExtension());
        $views[$app]->addExtension(new \Jasny\Twig\PcreExtension());
        $views[$app]->addExtension(new \Jasny\Twig\TextExtension());
        $views[$app]->addExtension(new \Jasny\Twig\ArrayExtension());
    }
    $vars = \array_merge(static::$_vars, $vars);
    $content = $views[$app]->render("$template.$view_suffix", $vars);
    static::$_vars = [];
    return $content;
}

vilords avatar Dec 03 '21 11:12 vilords

Creating your own view class is a good way.

Create app/support/Twig.php

<?php
namespace app\support;

class Twig extends \support\view\Twig
{
    public static function render($template, $vars, $app = null)
    {
        static $views = [], $view_suffix;
        $view_suffix = $view_suffix ? : \config('view.view_suffix', 'html');
        $app = $app === null ? \request()->app : $app;
        if (!isset($views[$app])) {
            $view_path = $app === '' ? \app_path(). '/view/' : \app_path(). "/$app/view/";
            $views[$app] = new Environment(new FilesystemLoader($view_path), \config('view.options', []));
    
            $views[$app]->addExtension(new \Jasny\Twig\DateExtension());
            $views[$app]->addExtension(new \Jasny\Twig\PcreExtension());
            $views[$app]->addExtension(new \Jasny\Twig\TextExtension());
            $views[$app]->addExtension(new \Jasny\Twig\ArrayExtension());
        }
        $vars = \array_merge(static::$_vars, $vars);
        $content = $views[$app]->render("$template.$view_suffix", $vars);
        static::$_vars = [];
        return $content;
    }
}

Modify config/view.php

return [
    'handler' => app\support\Twig::class
];

Restart webman then you can use your Twig extensions.

walkor avatar Dec 05 '21 03:12 walkor

Great :) Thank you

On Sun, Dec 5, 2021, 04:23 walkor @.***> wrote:

Creating your own view class is a good way.

Create app/support/Twig.php

app : $app; if (!isset($views[$app])) { $view_path = $app === '' ? \app_path(). '/view/' : \app_path(). "/$app/view/"; $views[$app] = new Environment(new FilesystemLoader($view_path), \config('view.options', [])); $views[$app]->addExtension(new \Jasny\Twig\DateExtension()); $views[$app]->addExtension(new \Jasny\Twig\PcreExtension()); $views[$app]->addExtension(new \Jasny\Twig\TextExtension()); $views[$app]->addExtension(new \Jasny\Twig\ArrayExtension()); } $vars = \array_merge(static::$_vars, $vars); $content = $views[$app]->render("$template.$view_suffix", $vars); static::$_vars = []; return $content; } } *Modify config/view.php* return [ 'handler' => app\support\Twig::class ]; *Restart webman then you can use your Twig extensions.* — You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub , or unsubscribe . Triage notifications on the go with GitHub Mobile for iOS or Android .

vilords avatar Dec 05 '21 09:12 vilords

Hi,

Just updated my webman, but this new way the extensions do not get recognized:

(/config/view.php)

use support\view\Raw; use support\view\Twig; use support\view\Blade; use support\view\ThinkPHP;

return [ 'handler' => Twig::class, 'options' => [ 'cache' => runtime_path() . '/views' ], 'extension' => function (Twig\Environment $twig) { $twig->addExtension(new \Jasny\Twig\DateExtension()); $twig->addExtension(new \Jasny\Twig\PcreExtension()); $twig->addExtension(new \Jasny\Twig\TextExtension()); $twig->addExtension(new \Jasny\Twig\ArrayExtension()); } ];

Even if I use addFilter or addFunction, nothing is recognized in the html template.

Any clue?

vilords avatar May 02 '24 23:05 vilords

ok, now it seems to be working: 'extension' => function (\Twig\Environment $twig) {

Not working with Twig\Environment but working with \Twig\Environment

vilords avatar May 03 '24 08:05 vilords