【问题标题】:PHP Twig Error: Object of class __TwigTemplate_XYZ could not be converted to stringPHP Twig 错误:类 __TwigTemplate_XYZ 的对象无法转换为字符串
【发布时间】:2016-12-02 13:33:03
【问题描述】:

嘿,早上好 :)。

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

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

我已经试过了:

  • 如果路径正确(通过 scandir("path"))
  • 如果 Twig 被加载
  • 类似的最小代码

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

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

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

我知道这很混乱,但是该项目已经存在并且现在已经发展了大约 20 年,这种树枝方法是我尝试开始(!)将逻辑与 html 代码分开。

【问题讨论】:

    标签: php twig template-engine


    【解决方案1】:

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

    所以要么做:

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

    或者

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

    【讨论】:

      猜你喜欢
      • 2015-02-05
      • 1970-01-01
      • 2017-07-25
      • 2015-05-15
      • 1970-01-01
      • 2013-07-03
      • 2014-11-21
      • 2012-07-12
      • 1970-01-01
      相关资源
      最近更新 更多