我收到此错误: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 %}
但它不起作用,我得到上面的错误。我试图解决它有很大的麻烦。我愿意接受任何建议或提示。
答案 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!')));