2013-05-14 22 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)); 
}