2010-09-03 66 views
2

你好我有以下proble:Symfony的從動作變量傳遞,以查看

我有操作類在Symfony的,action.class.php的視圖的,在那裏,我有這樣的代碼:

foreach ($images as $i => $img) 
{ 
    if (strstr($request->getFileType($img), 'image')) 
    { 
    $enter = true; 
    $name = Util::processImages($request, $img, 'app_uploads_temp_dir'); 

    if ($name != '') 
    { 
     $userimages[$i] = $name; 
     $this->uploads[$i] = true; 
     $this->message = 'Image loaded'; 
    } 
    } 
} 

在我看來,我想渲染根據情況上傳字段或消息:

​​

但$這個 - >未設置消息,也不是$消息或從動作傳遞的任何變量,可以有人電話我爲什麼會發生這種情況,我怎麼解決它?

+0

你沒有注意到的是它不是你粘貼代碼的視圖。這是佈局。如果在你的動作中使用'$ this-> message =「Test」;'在你的視圖中可以使用'$ message',但是除非你使用插槽,否則只能在你的佈局中訪問使用'$ this-> getRequest() - > setParameter('message','Test');'的請求變量。無論哪種方式:這是一個解決方案的解決方案:) – phidah 2010-09-04 16:16:52

+0

非常感謝你,我有點新鮮的symfony,但它的一個偉大的框架堅韌...再次感謝您的建議 – 2010-09-05 00:27:12

回答

2

好了各位,我發現自己的答案,對不起張貼的答案,我會離開它,以防有人有相同的問題:

的整體思路是用插槽,代碼將是像這樣:

foreach ($images as $i => $img) 
{ 
    if (strstr($request->getFileType($img), 'image')) 
    { 
    $enter = true; 
    $name = Util::processImages($request, $img, 'app_uploads_temp_dir'); 

    if ($name != '') 
    { 
     $userimages[$i] = $name; 
     $uploads[$i] = true; 
     $message = 'Image loaded'; 
    } 
    } 
} 

$this->getResponse()->setSlot("message", $message); 

,並在接下來的代碼,使用變量:

​​

感謝反正!

  • 如果管理員認爲,這個問題應該被刪除,請告訴我,我會做
+0

忘了添加源: http:///www.symfonydeveloper.com/2009/03/05/passing-variables-from-an-action-or-module-template-to-a-layout-template/ 這是我在哪裏找到信息 – 2010-09-03 06:56:28

2

如果設置$這個 - >在你的行動消息時,它可在$消息你的看法。

但是在你的情況下,你需要在foreach()中設置$ this-> message,所以你需要爲每個循環重新定義$ this->消息。這可能解釋了爲什麼$ message沒有返回您所期望的值。

這裏使用一個插槽不是正確的用例,因爲在動作中設置的所有$ this-> vars已經直接傳遞給視圖/模板。

+0

我試過設置foreach之外的變量並沒有工作:( – 2010-09-03 12:00:49