为什么会出现此错误:未定义的属性:PHP中的Twig_Environment?

时间:2016-04-20 13:04:58

标签: php symfony twig

我收到此错误:Notice: Undefined property: Twig_Environment::$render in尝试从PHP - 文件向twig - 文件发送错误登录时发送错误。我想用来自mu PHP的消息显示相同的页面 - 文件类似:

$twig = new Twig_Environment($loader);
echo $twig->render('register.twig');

if (isset($_POST['reg'])) {

    if ($_POST['pwd1'] === $_POST['pwd2']) {

    } else {
        echo ("<script>location.href = $twig->render('register.twig', array('theMessage' => 'Wrong password!'));</script>");
        exit;
    }
}

并在twig - 文件:

{% if theMessage %}
  {{ theMessage }}
{% endif %} 

但它不起作用,我得到上面的错误。我试图解决它有很大的麻烦。我愿意接受任何建议或提示。

1 个答案:

答案 0 :(得分:1)

问题是PHP正在插入消息中的$twig变量。最好更改此代码:

echo ("<script>location.href = $twig->render('register.twig', array('theMessage' => 'Wrong password!'));</script>");

对此:

echo sprintf("<script>location.href = %s;</script>", $twig->render('register.twig', array('theMessage' => 'Wrong password!')));