非常感謝這裏的答案,但他們並沒有真正幫助我。下一次,我嘗試添加更多代碼示例;)
我的目標是從行動渲染成模板,但使用不同的參數和渲染這些不同的內容在父視圖中的諧音。
實例:假設一個名爲「view.html.twig」,包括模板2倍「myPartial.html.twig」模板,但第一次與PARAM和第二時間而不PARAM。基於這個參數,應該返回不同的內容。
我的問題是現在,爲什麼顯然只有第一個動作在Symfony的呈現爲我的兩個部分觀點有相同的內容。
原來這就是我現在做的事:
1控制器,2個動作,都調用了同一個動作3獲取數據和值返回給調用動作。然後這兩個動作都使用從第三個動作渲染的值調用渲染方法。
這就是它現在看起來:
<div class="tab-content clearfix">
<div class="tab-pane" id="1b">
{% render controller('MyBundle:MyController:list1') %}
</div>
<div class="tab-pane" id="2b">
{% render controller('MyBundle:MyController:list2', {'type' : 1}) %}
</div>
但我想實現是做這樣的事情(沒有工作,因爲那時兩個標籤會顯示同樣的內容):
<div class="tab-content clearfix">
<div class="tab-pane" id="1b">
{% render controller('MyBundle:MyController:list') %}
</div>
<div class="tab-pane" id="2b">
{% render controller('MyBundle:MyController:list', {'type' : 1}) %}
</div>
,我覺得困惑,因爲在這兩個時代 「渲染」 我被調用,所以我希望控制器在兩個時間都被調用,因此控制器中的局部視圖也被渲染兩次。但顯然這並非如此:(控制器本身看起來是這樣的:
public function list1Action()
{
$twigVars = //do something to build the twig vars without param;
return $this->render('@MyBundle/Test/partial_list.html.twig', array(
'vars' => $twigVars,
));
}
public function list2Action($param)
{
$twigVars = //do something to build the twig vars with param;
return $this->render('@MyBundle/Test/partial_list.html.twig', array(
'vars' => $twigVars,
));
}
而我想要的是這樣的:
public function listAction($param = '')
{
if ($param) {
$twigVars = //do something to return the twig vars with param;
} else {
$twigVars = //do something else to return twig vars without param;
}
return $this->render('@MyBundle/Test/partial_list.html.twig', array(
'vars' => $twigVars,
));
}
來源
2017-02-19 15:06:18
Eve
請更新與相關的代碼問題(控制器/ views) – DarkBee
答案之一是否回答你的問題? –