2011-08-26 82 views
0

我有一個工作正常的自定義動作助手。 如果用戶沒有登錄,它會生成一個動態登錄框,如果他是,它正在生成一個菜單。如何將視圖作爲對象加入動作助手

但是在這裏我有一個問題。 我想從一個叫做user_menu.phtml的小視圖中生成該菜單

如何將該視圖放入我的視圖幫助器中,並將其分配給對象?

好了,一些更新,遺憾的是愚蠢的,actualy我有動作助手:

對不起,如果我是不夠具體而寫我最初的問題。

所以我有一個動作助手在:library/Hlp/Action/Helper 如果用戶沒有在旅館裏閒逛,那麼這個幫助器就會呈現一個表單。

這裏是我的助手方法,做這項工作:

public function preDispatch() 
{ 
    $view = $this->getView(); 

    $identity = Zend_Auth::getInstance()->getIdentity(); 

    $session = new Zend_Session_Namespace('users_session'); 
    $user_id = $session->idd; 

    if(!empty($identity)) { 
     $userModel = new Application_Model_Vartotojai(); 
     $user_email = $userModel->geUserRowBy('id', $user_id); 
     $user_email = $user_email['email']; 

     $view->login_meniu = $identity.' - 
    [id:'.$user_id.']<br />['.$user_email.'] <br/> 
    <a href="/authentication/logout">Log OUt</a>'; 
    //here I would like to read view file to an object or some other variable 
    //if posible to an object si I would be able to inject some values 

    } else {   
     $form = new Application_Form_LoginForm(); 
     $view->login_meniu = $form; 
     $view->register_link = '<br /><a href="https://stackoverflow.com/users/register">Register</a>'; 
     //here I would like to read view file to an object or some other variable 
    //if posible to an object si I would be able to inject some values 
    } 

Additionaly這種形式我想添加一些鏈接或其他HTML內容,這將BR存儲在一個視圖文件。

回答

3

所有你需要做的是延長Zend_View_Helper_Abstract類。然後您將視圖對象存儲在公共屬性$view中。

通過使用對象,您可以用

return $this->view->partial('user_menu.phtml'); 

更新

使您的文件,因爲你已經更新了你的問題,我會更新我的答案離開原來的答案,因爲它仍然是有效的先前的問題。

你的情況,你已經有$view對象,做你問在註釋只需使用連接到視圖這樣的partial helper

$renderedScript = $view->partial('user_menu.phtml', 
    array('id' => $user_id, 'email' => $user_email['email'])); 

通過給數組或對象作爲部分調用的第二個參數,您可以在腳本文件中將它們用作模型。例如:

// content of user_menu.phtml 
<h1>Login info</h1> 
<p> 
    [id: <?=$this->user_id?>]<br /> 
    [<?=$this->email?>] <br/> 
    <a href="/authentication/logout">Log Out</a>' 
</p> 

P.我在視圖腳本中使用了short_tags + the equal sign (=) shorthand作爲回顯,如果您不使用它們,您應該用<?php echo $this->email ?>

+0

謝謝Fabio,我認爲您的回答接近真相,但我仍然無法正常工作。所以我必須初始化$ view_obj = new Zend_View_Helper_Abstract();然後如何指定我想使用的視圖文件? – madcatzx

+0

@madcatzx你應該在你的問題中發佈你的當前代碼來澄清你的需求。你已經有看法幫手了,是嗎?所以在你的類中擴展'Zend_View_Helper_Abstract',你就沒事了。我發佈了一些代碼,也許你可以看看它嗎? – Fabio

+0

@madcatzx我已經更新了我的答案以適應您的新請求。 – Fabio

-2

從視圖中,可以通過這個來輔助

myHelper($this,$otherVars) 

然後從助手,你可以調用其他幫助

myHelper($view, $otherVars){ 
    $view->otherHelper() 
}