godotengine / godot-asset-library

PHP frontend for Godot Engine's asset library
https://godotengine.org/asset-library
MIT License
294 stars 98 forks source link

Migrate to Twig for templating #135

Closed Calinou closed 2 years ago

Calinou commented 6 years ago

As discussed with @bojidar-bg, we should eventually migrate from PHP-based templating to Twig. This would lead to more readable and secure templates.

nervaccio commented 5 years ago

Hi, I've done some experiment to migrate to twig. It's an early draft, you can check this branch twig-test I hope could be a starting point to figure out what is the best way to approrch this task.

I've rewrite only assets.phtml template, and tried to touch as less as possible the "backend".

What you'll find

src/dependencies.php rows 6-20 based on the Slim documentation here

// Twig render
$container['twig'] = function ($c) {
    $view = new \Slim\Views\Twig(__DIR__ . '/../templates_twig/', [
        // 'cache' => 'path/to/cache',
        'debug' => true,
    ]);
    $view->addExtension(new Twig_Extension_Debug());

    // Instantiate and add Slim specific extension
    $router = $c->get('router');
    $uri = \Slim\Http\Uri::createFromEnvironment(new \Slim\Http\Environment($_SERVER));
    $view->addExtension(new Slim\Views\TwigExtension($router, $uri));

    return $view;
};

Until we'll start to touch how we send data to the template I think is convenient to have both system in place. Next part you can see how to select the new one.

src/middleware.php rows 142-150

$pageConverted = [
    'GET /asset' => 'assets',
];

if (array_key_exists($queryUri, $pageConverted)) {
    $response = $this->twig->render($response, $pageConverted[$queryUri] . '.html.twig', $params);
}else {
    $response = $this->renderer->render($response, $template_names[$queryUri] . '.phtml', $params);
}

It's only my hack to switch only a one route without touch anything else.

templates_twig/ all files In first place i haven't test all the detail, just the search fuctionality, sorry. The _pagination.phtml is not complete migrated. So please be kind. In this folder you can see how i've converted a large part of assets page. How i've splitted the template, like macro(menu links), components(search form) is just an idea. If we start to move to Twig the logic present in _footer.phtml, for example, i think must be moved to the backend.

@bojidar-bg told me that @Calinou have done some work too, did you though a different approach?

This is a screenshot of the test. asset-library

Calinou commented 5 years ago

There's ongoing work to migrate to Laravel (which uses Blade templating, but it's still more convenient than bare PHP templates): https://github.com/godotengine/godot-asset-library/issues/82

nervaccio commented 5 years ago

Ok, so there is no need for this.

Calinou commented 2 years ago

This is superseded by the asset library rewrite, which will become the new official platform sometime in the future. I don't have an ETA for deploying it though.

Closing, as this current platform is now in maintenance mode.