PHP Twig Error: Object of class __TwigTemplate_XYZ could not be converted to string

PHP Twig Error: Object of class __TwigTemplate_XYZ could not be converted to string

嘿,早上好:)。

我将从错误消息开始,然后解释我尝试过和做过的事情:

Catchable fatal error: Object of class __TwigTemplate_[64chars] could not be converted to string in /someFolder/lib/Twig/Loader/Filesystem.php on line 139

完整的错误信息如图:

The PHP Error Message

作为文本的调用堆栈:

Call Stack
#   Time    Memory  Function    Location
1   0.0001  123400  {main}( )   ../handleOOP.php:0
2   0.0107  1269656 OOPHandler::runPage( )  ../handleOOP.php:7
3   0.0107  1269768 AnweisungEingeben->stepkontrolle( ) ../OOPHandler.php:62
4   0.0108  1271536 AnweisungEingeben->ausgabe( )   ../AnweisungEingeben.php:77
5   0.0383  1669484 Twig_Environment->render( ) ../AnweisungEingeben.php:442
6   0.0383  1669484 Twig_Environment->loadTemplate( )   ../Environment.php:347
7   0.0383  1669512 Twig_Environment->getTemplateClass( )   ../Environment.php:378
8   0.0384  1669512 Twig_Loader_Filesystem->getCacheKey( )  ../Environment.php:312

AnweisungEingeben.php中我有以下代码片段:

include_once "../lib/Twig/Autoloader.php";
Twig_Autoloader::register();
$template_dirs = array(
        $root.'templates/web/anweisungen',
        [...]
);
$twig_loader = new Twig_Loader_Filesystem($template_dirs);
$twig = new Twig_Environment($twig_loader, array("debug" => true));
$template = $twig->loadTemplate('Anweisung_Base.phtml');
$wech = $twig->render($template); // line 442 from AnweisungEingeben.php

我已经试过了:

不幸的是,结果都是一样的。

奇怪的是,完全相同的代码在一个最小的项目中却能完美运行。

我知道它非常混乱,但该项目已经存在并发展了大约 20 年,这种分支方法是我尝试开始(!)将逻辑与 html 代码分离的尝试。

你正在混合方法,你试图将 Twig_Template 传递给 Twig_Environment 的渲染方法,而这个方法实际上期望模板的路径是第一个 parameter

所以要么做:

$template = $twig->loadTemplate('Anweisung_Base.phtml');
echo $template->render();

echo $twig->render('Anweisung_Base.phtml');