2017-02-13 58 views
-1

這可能是一個愚蠢的問題,但也許你仍然可以幫助我。樹枝:根據一個控制器動作渲染不同參數的視圖

我想要什麼:我有一個枝條模板,已經在它的標籤(使用引導選項卡板),現在我想用一個動作從一個控制器,但使用不同的參數來渲染選項卡的內容。

所以基本上,這個控制器動作應該採取一些參數「類型」,然後根據這種類型返回不同的結果。我們的目標是沒有2個控制器來渲染除了基於參數「類型提取不同信息之外幾乎相同的樹枝視圖。」 但是,我無法讓它工作,它或者是兩個選項卡都顯示相同的結果,因爲顯然該視圖只呈現一次,或者說,我在渲染的時間內得到的樹枝模板例外。 什麼是解決這個問題的正確方法?

非常感謝。

+1

請更新與相關的代碼問題(控制器/ views) – DarkBee

+0

答案之一是否回答你的問題? –

回答

2

有多種方式來實現這一目標。你可以在你的Twig模板中使用if/else語句,但是你也可以在你的控制器中設置模板,這取決於你最適合的模板。

方法1:自定義模板

默認情況下,Symfony的使用「foobar.html.twig在您的foobarAction()方法,但你可以在你的方法重寫此:

public function recentArticlesAction($max = 3) 
    { 
     // make a database call or other logic 
     // to get the "$max" most recent articles 
     $articles = ...; 

     return $this->render(
      'article/recent_list.html.twig', 
      array('articles' => $articles) 
     ); 
    } 

(警告:例如從How to Embed Controllers in a Template,但文章本身無關,同你的問題做)

你可以設置一個變量(例如$templateName),並改變它:

$templateName = 'recent_list.html.twig'; 
if ($admin) { 
    $templateName = 'another_template.html.twig'; 
} 

//or using parameters from you Request 
if ($type = $request->request->get('type')) { 
    $templateName = $type . '.html.twig'; 
} 

return $this->render(
    $templateName, 
    array('articles' => $articles) 
); 

方法2:用嫩枝

控制器:

public function foobarAction(Request $request) 
{ 
    return [ 
     'type' => $request->request->get('type'); 
    ]; 
} 

您嫩枝模板:

{% if type == 'foo' %} 
<h1>Foo!</h1> 
<p>Hi, welcome you the foo page!</p> 
{% elseif type == 'bar' %} 
<h1>Bar!</h1> 
<p>Hi, you've reached the Bar page.</p> 
{% else %} 
<h1>Error!</h1> 
<p>Type not found.</p> 
{% endif %} 
0

嘗試渲染手動每個視圖:

if ($type) { 
     return $this->render('home/template.html.twig', array('data' => $data)); 
    } 
    return $this->render('home/another.html.twig', array('another_data' => $another_data)); 
0

非常感謝這裏的答案,但他們並沒有真正幫助我。下一次,我嘗試添加更多代碼示例;)

我的目標是從行動渲染成模板,但使用不同的參數和渲染這些不同的內容在父視圖中的諧音。

實例:假設一個名爲「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, 
    )); 
} 
相關問題