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 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度