Solution
You could do the following, replacing your last line return $this->render($tmpl);
:
$response = new Response();
$response->setContent($tmpl);
return $response;
Don't forget to put a use Symfony\Component\HttpFoundation\Response;
at the top of your controller though!
Theory
Alright, let's begin from where you are now. You are inside your controller, calling the render
method. This method is defined as follows:
/**
* Renders a view.
*
* @param string $view The view name
* @param array $parameters An array of parameters to pass to the view
* @param Response $response A response instance
*
* @return Response A Response instance
*/
public function render($view, array $parameters = array(), Response $response = null)
{
return $this->container->get('templating')->renderResponse($view, $parameters, $response);
}
The docblock tells you that it expects a string which is the view name, not an actual template. As you can see, it uses the templating
service and simply passed the parameters and return value back and forth.
Running php app/console container:debug
shows you a list of all registered services. You can see that templating
is actual an instance of Symfony\Bundle\TwigBundle\TwigEngine
. The method renderResponse
has the following implementation:
/**
* Renders a view and returns a Response.
*
* @param string $view The view name
* @param array $parameters An array of parameters to pass to the view
* @param Response $response A Response instance
*
* @return Response A Response instance
*/
public function renderResponse($view, array $parameters = array(), Response $response = null)
{
if (null === $response) {
$response = new Response();
}
$response->setContent($this->render($view, $parameters));
return $response;
}
You now know that when you call the render
method, a Response object is passed back which is essentially a plain Response
object on which setContent was executed, using a string representing the template.
I hope you don't mind I described it a bit more detailed. I did this to show you how to find a solution like this yourself.