2012-05-14 20 views
0

我的帖子有正文,在這個文本我把短代碼像[gallery = 2],現在我可以找到2個參數在我的文本(畫廊和2)我想動態調用在我看來,一個函數(畫廊),並且調用元素(圖庫ID = 2)所示:Cakephp短代碼動態調用函數和元素

應用程序/視圖/職位/ index.ctp

findshortcode($posts[Post][Body]); // this function find short code and call his name like gallery(2) 

//my problem is : 

function gallery($id = null){ 
    $this->element('gallery', array('galleryid' => $id), array('plugin' => 'gallerys')); 
} 

回答

0

你有什麼有沒有做非常有意義,你所描述的做法並不是Cakish或MVC的做事方式。但我想我會回答你的問題,並會向你解釋你想要做的方式。

你需要做的是什麼?是不是所有的邏輯從控制器內......然後把全部完成變量顯示視圖...

class PostsController extends AppController { 

var $shortCodeTypes = array('gallery'); 

function view($id){ 
    $post = $this->Post->find(null, $id); 
    $post = $this->_insertShortCodes($post); 
    $this->set(compact('post')); 
} 

function _insertShortCodes($post){ 

    foreach($this->shortCodeTypes as $shortCodeType){ 
     // Do logic to find shortcodes and insert data into post body 
    } 
    return $post 
} 
+0

thaks了很多,我想你的回答是真實的,我在MVC方式的編程邏輯中犯了錯誤。 – ali786