0

我的帖子有正文,在这些文本中我输入了 [gallery=2] 之类的短代码,现在我可以在我的文本中找到 2 个参数(gallery 和 2)我想在我看来动态调用一个函数(gallery),它是像这样调用一个元素(图库 id = 2):

应用程序/视图/帖子/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'));  
}
4

1 回答 1

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
}
于 2012-05-14T21:08:52.520 回答