I am getting this error: Notice: Undefined property: Twig_Environment::$render in
when trying to send a message from a PHP
-file to a twig
-file when is wrong login. I want to show the same page with the message from mu PHP
-file like that:
$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;
}
}
and in twig
-file:
{% if theMessage %}
{{ theMessage }}
{% endif %}
But it doesn't work and I get the error above. I am having big trouble to try to resolve it. I am open to any suggestions or hint.
The problem is that PHP is interpolating the $twig
variable inside your message. It's better to change this code:
echo ("<script>location.href = $twig->render('register.twig', array('theMessage' => 'Wrong password!'));</script>");
To this one:
echo sprintf("<script>location.href = %s;</script>", $twig->render('register.twig', array('theMessage' => 'Wrong password!')));