Raffael
Raffael

Reputation: 20045

How to change view-parameter within controller?

This is the code that gets executed (as displayed in "code behind this page" section):

Controller Code

    /**
     * @Route("/hello/{name}", name="_demo_hello")
     * @Template()
     */
    public function helloAction($name)
    {
        $name = "whatever";
        $this->render('AcmeDemoBundle:Demo:hello.html.twig',
          array('name' => '123'));
        //return array('name' => 'abc');
    }

Template Code

{% extends "AcmeDemoBundle::layout.html.twig" %}

{% block title "Hello " ~ name %}

{% block content %}
    <h1>xHello {{ name }}!</h1>
{% endblock %}

The output is

xHello Raffael!

The URL: http://192.168.177.128/Symfony/web/app_dev.php/demo/hello/Raffael


And here is my problem:

When I uncomment the return within controller then "Raffael" is replaced with "abc" as expected.

But according to the Quicktour it is possible to determine the values of variables within the template via the render-Method.

To render a template in Symfony, use the render method from within a controller and pass it any variables needed in the template:

$this->render('AcmeDemoBundle:Demo:hello.html.twig', array( 'name' => $name, ));

What's wrong?

Upvotes: 1

Views: 92

Answers (1)

Raffael
Raffael

Reputation: 20045

Further down the quick tour implicitely reveales that you have to return the output of render:

public function helloAction($name)
{
  return $this->render('AcmeDemoBundle:Demo:hello.html.twig', 
    array('name' => '123'));
}

Upvotes: 1

Related Questions