dongnaota6386 2016-07-28 08:58
浏览 107
已采纳

PHP Twig错误:类__TwigTemplate_XYZ的对象无法转换为字符串

Hey and good morning :).

I'll start with the error message and explain afterwards what I've tried and did:

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 full error message as Image:

The PHP Error Message

The call stack as Text:

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

In the AnweisungEingeben.php I have the following code fragment:

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

I already tried:

  • If the paths are correct (via scandir("path"))
  • If Twig gets loaded
  • Minimal Code like

    $loader = new Twig_Loader_Array(array('index.html' => 'Hello World!'));
    $twig = new Twig_Environment($loader);
    echo $twig->render('index.html');
    

Unfortunately everything results in the same.

The strange thing is, that the exact same code is working flawlessly in a minimal project.

I know it's very chaotic, but the project is existing and growing now for about ~20 years and this twig approach is my try to start(!) separating the logic from the html code.

  • 写回答

1条回答 默认 最新

  • dpnhp20440 2016-07-28 10:26
    关注

    You're mixing methods up, you are trying to pass a Twig_Template to the Twig_Environment's render method, while this method actually expect the path to the template as it's first parameter

    So either do :

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

    Or

    echo $twig->render('Anweisung_Base.phtml');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里