2013-05-14 45 views
2

我是Symfony 1.4 NEWBIE,我正在加入一个项目,我需要创建一个新的仪表板。Symfony 1.4将变量从行为传递到查看

我创建了一个以下控制器分析/操作/的actions.class.php

public function executeTestDashboard(sfWebRequest $request){ 

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

} 

分析/模板/ _testDashboard.php观点,这是部分列入家电/模板/ indexSuccess.php的:

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

它不工作,$ foo的既不是 “FooBar的”,也不是 “ThisFooBar”,但 “空”。我应该如何继续,才能使其发挥作用? (或者甚至检查我的executeTestDashboard控制器是否被处理?)

回答

1

您应该阅读关于Symfony 1.4中的partialscomponents。如果使用include_partial()在模板中包含部分内容,则只会渲染部分内容,并且不会执行控制器代码。

如果你需要一些较简单的渲染部分,你应该使用一个组件,它看起来会是更多的逻辑一样:

analyse/actions/compononets.class.php

public function executeTestDashboard(){ 

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

analyse/templates/_testDashboard.php

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

在任何其他模板文件,您希望显示仪表板的位置:

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

感谢,与组件的使用工作! – 2013-05-14 13:43:07

2

下面是可能解释给你一个好一点的几个例子:

// $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)); 
}