简体   繁体   中英

Argument 1 passed to Twig\Environment::getTemplateClass() must be of the type string, null given

Been looking around but can't find anything to sort the issue. I made a custom PHP MVC (using help from various sources), use Twig's template engine and rendering works fine. I added an custom Error handler which works as well. I set a constant to show errors in dev but in prod just log them and render custom 404 and 500 views. When the twig render($template) function is called for this specific option it just returns the following errors:

Fatal error: Uncaught TypeError: Argument 1 passed to Twig\Environment::getTemplateClass() must be of the type string, null given, called in
C:\Users\duboi\OneDrive\Documents\Programming\www\fabrice\vendor\twig\twig\src\Template.php on line 321 and defined in
C:\Users\duboi\OneDrive\Documents\Programming\www\fabrice\vendor\twig\twig\src\Environment.php on line 259

There is no issue whatsoever when 404 and 500 http requests are not triggered. (existing routes/controllers/actions) I looked through Twig's classes from the error listed and noticed the argument causing issue is $name from public function \Twig\Environment::getTemplateClass($name, int $index = null): string I traced the functions calls and did a var_dump() in each one of them on the variable that ends up being the $name argument. All show \vendor\twig\twig\src\Environment.php:279:string '404.html' (length=8) which is a String and is NOT NULL. Why does the error say it is?

Error.php:

public static function exceptionHandler($exception) {
    $code = $exception->getCode();
    if ($code != 404) {
        $code = 500;
    }
        
    http_response_code($code);
        
    if (\App\Config::SHOW_ERRORS) {
  [....]
    } else {
        $log = ROOT . DS . 'logs' . DS . date('Y-m-d') . '.txt';
        ini_set('error_log', $log);
        
        $message = "Uncaught Exception: '" . get_class($exception) . "'";
        $message .= " with message '" . $exception->getMessage() . "'";
        $message .= "\nStack trace: " . $exception->getTraceAsString();
        $message .= "\nThrown in '" . $exception->getFile() . "' on line " . $exception->getLine();
        
        error_log($message);
        View::renderTemplate($code . '.html');
    }
}

View.php:

public static function renderTemplate($template, $args = []) {
    static $twig = null;
    
    if ($twig === null) {
        $loader = new \Twig\Loader\FilesystemLoader(ROOT . DS . 'App' . DS . 'Views');
        $twig = new \Twig\Environment($loader);
    }
    echo $twig->render($template, $args); // this is the point of my code when the error occurs
}

I had the exact same error message. For me, the problem was, That I forgot to put the baseTemplate in quotes, Which I was extending in the template that I wanted to render.

( I had {% extends firstTwigBaseTemplate.html %} instead of {% extends "firstTwigBaseTemplate.html" %}) )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM