2010-03-13 61 views
0

我想使這些套的觀點之一:Zend Framework:如何刪除控制器中呈現的視圖?

  1. 體佩$ ID1

OR

  1. 體佩$ id2

其設置exsists。

我不喜歡這樣寫道:

try { 
    $this->render("head"); 
    $this->render("body-$id1"); 
    $this->render("foot"); 
} catch (Exception $e) { 
    $this->render("head"); 
    $this->render("body-$id2"); 
    $this->render("foot"); 
} 

,但它會導致head觀點被渲染兩次,如果身體 - $ ID1不存在。

你有更好的解決方案嗎?

在另一種說法中,我可以在渲染之前檢查body-$id1的存在嗎?

回答

1

那麼,它會在「try」塊中運行任何有效的腳本,但是如果失敗,它將呈現「catch」塊中的所有內容。所以,你可能需要更多的東西一樣:

$this->render("head"); 
try { 
    $this->render("body-$id1"); 
} catch (Exception $e) { 
    $this->render("body-$id2"); 
} 
$this->render("foot"); 

我沒有看到檢查的API函數如果視圖存在,但你可以寫一個控制器助手,只是獲取路徑到您的視圖腳本,並使用file_exists到檢查該路徑中是否存在「body - {$ id1}」。

相關問題