Home > Back-end >  Unknown "asset" function in Twig PHP
Unknown "asset" function in Twig PHP

Time:05-28

I'm having problems importing CSS stylesheet in HTML through TWIG template. The asset() function doesn't seem to be recognised by the compiler. The css file is in app/views/styles and login.html is in app/views. This error occur:

Unknown "asset" function.

index.php

<?php

declare(strict_types=1);

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

use Slim\Factory\AppFactory;
use Slim\Views\Twig;
use Slim\Views\TwigMiddleware;

$app = Appfactory::create();
//twig

$percorso= __DIR__ . '/../app/views';
$twig = Twig::create($percorso, ['cache' => false]);
$app->add(TwigMiddleware::create($app, $twig));


//middleware
require '../app/middleware.php';
$app->addErrorMiddleware(true, true, false);

//rotte
require '../app/routers/routes.php';

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

login.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">  
    <link rel="stylesheet" href="{{asset('styles/styleguide.css')}}">

</head>
<body>
      <div >
        <div >
          <div >
            <h1 >GREPPINOTES</h1>
            <img src="assets\[email protected]" >
          </div>
          <h1 >La serietà dello studio, il piacere dello studio</h1>
            
          <div >
           <button type="button" >ACCEDI AL SITO</button>
          </div>
          
        </div>
       
      </div>
  
</body>
</html>

composer.json

{
    "require": {
        "slim/slim":"4.*",
        "slim/psr7": "^1.5",
        "selective/basepath": "^2.1",
        "slim/twig-view": "3.3",
        "symfony/asset": "^5.4"
     

    },
    "autoload": {
        "psr-4": {
            "App\\": "app/", 
            "App\\Controller\\": "app/controllers/",
            "App\\Auth\\": "app/middleware/"
            

        }
    }
   
}

I'm using twig-view 3.3 and symfony 5.4.

I tried to import the style by just adding it in the HTML with <style> block and it worked, but I wanted to use a different file just for the style.

CodePudding user response:

Run composer require symfony/webpack-encore-bundle then Run npm install

CodePudding user response:

Run composer require symfony/webpack-encore-bundle then Run npm install then npm run dev

  • Related