lp_td2013
lp_td2013

Reputation: 23

Symfony 1.4 Passing variables from action to view

I am a Symfony 1.4 NEWBIE and I'm joining a project where I need to create a new dashboard.

I've created the following controller in analyse/actions/actions.class.php

public function executeTestDashboard(sfWebRequest $request){

    $foo = "FooBar";
    $this->foo = "ThisFooBar";
    return $this->renderPartial('analyse/testDashboard', array('foo' => $foo);

}

and analyse/templates/_testDashboard.php view, which is a partial included in home/templates/indexSuccess.php :

<div class="testDashboard">
        <h1>TEST</h1>
        <?php var_dump($foo);?>
</div>

It doesn't work, $foo is neither "FooBar" nor "ThisFooBar", but "null". How should I proceed, in order to make it work? (or even check if my executeTestDashboard controller is processed ?)

Upvotes: 2

Views: 5561

Answers (2)

antony
antony

Reputation: 2893

Here's a few examples that might explain it to you a bit better:

// $foo is passed in TestDashboardSuccess.php, which is the default view rendered.
public function executeTestDashboard(sfWebRequest $request)
{
    $this->foo = "ThisFooBar";
}

// Different template indexSuccess.php is rendered. $foo is passed to indexSuccess.php
public function executeTestDashboard(sfWebRequest $request)
{
    $this->foo = "ThisFooBar";
    $this->setTemplate('index');
}

// Will return/render a partial, $foo is passed to  _testDashboard.php. This 
// method is often used with ajax calls that just need to return a snippet of code
public function executeTestDashboard(sfWebRequest $request)
{
    $foo = 'ThisFooBar';

    return $this->renderPartial('analyse/testDashboard', array('foo' => $foo));
}

Upvotes: 2

Michal Trojanowski
Michal Trojanowski

Reputation: 12362

You should read about partials and components in Symfony 1.4. If you're including a partial in a template using include_partial() it is only the partial that is rendered and no controller code is executed.

If you need some more logic than simple rendering the partial you should use a component, which would look something like:

in analyse/actions/compononets.class.php

public function executeTestDashboard(){

    $this->foo = "FooBar: ".$this->getVar('someVar');
}

in analyse/templates/_testDashboard.php

<div class="myDashboard><?php echo $foo ?></div>

in any other template file, where you want your dashboard displayed:

include_component('analyse', 'testDashboard', array('someVar' => $someValue));

Upvotes: 1

Related Questions