2

我是 Symfony 1.4 新手,我正在加入一个需要创建新仪表板的项目。

我在analyse/actions/actions.class.php中创建了以下控制器

public function executeTestDashboard(sfWebRequest $request){

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

}

analyse/templates/_testDashboard.php视图,它是 home/templates/indexSuccess.php 中包含的部分视图:

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

它不起作用,$foo 既不是“FooBar”也不是“ThisFooBar”,而是“null”。我应该如何进行,以使其发挥作用?(甚至检查我的 executeTestDashboard 控制器是否已处理?)

4

2 回答 2

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));
}
于 2013-05-14T12:29:09.877 回答
1

您应该阅读有关Symfony 1.4 的partials内容。components如果您在模板中包含部分使用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));
于 2013-05-14T12:27:19.627 回答