M
M
materlelik2014-08-27 22:36:13
PHP
materlelik, 2014-08-27 22:36:13

How to make a redirect in Twig template engine?

Hello, I have a Twig templating engine on my site. The page displays a link from the database in the form {{post.link}}. How to make a redirect happen? In bare php, the script looks like this:

<?php
   header("Location: http://vk.com");
?>

How to do the same in Twig?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey, 2014-08-27
Protko @Fesor

Another plus of Twig is that if a developer wants to do something where there is no place for it, then he will have problems with it.
You shouldn't do this in templates. Generally. If you really want it, you can:

<script>
window.location = {{post.link | json_encode()}};
</script>

Why exactly? Because otherwise you would have to mess with the output buffer, and this is clearly not the place where this should be done. If you have at least one byte of the request summer leaked to stdout, then the headers can no longer be changed.
but on good it should be done in the controller.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question