开发者

How to change view-parameter within controller?

开发者 https://www.devze.com 2023-03-21 15:56 出处:网络
This is the code that gets executed (as displayed in \"code behind this page\" section): Controller Code

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 r开发者_如何学Ceturn 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?


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'));
}
0

精彩评论

暂无评论...
验证码 换一张
取 消