xiaocms進(jìn)階教程:為表單建立list模板(自定義controller和模板)

通過看源碼我們能夠發(fā)現(xiàn)xiaocms的入口都是index.php 不通過index.php進(jìn)入的頁面是不會(huì)執(zhí)行模板語言的。而index.php?c=這個(gè)地址中c參數(shù)即為controller的位置。
首先我們先來看一下系統(tǒng)的控制器,進(jìn)入core>controller>index.php 這里就是系統(tǒng)默認(rèn)自帶的action和controller分配的地方了。我們進(jìn)入indexAction函數(shù)

    public function indexAction() {
    if($this->get('catdir') || $this->get('catid')){//如果url中包含catid,那么就是欄目頁面。
        $catid  = (int)$this->get('catid');
        if(!empty($catid)) {
        $category    = $this->category_cache[$catid];
        }
        else if ($this->get('catdir')) {
        $category_dir = get_cache('category_dir');
        $catid = $category_dir[$this->get('catdir')];
        $category = $this->category_cache[$catid];
        }
        if (empty($category)) {
            header('HTTP/1.1 404 Not Found');
            $this->show_message('當(dāng)前欄目不存在');
        }
        $category['page']   = (int)$this->get('page') ? (int)$this->get('page') : 1;
        if($category['islook'] && !$this->member_info) $this->show_message('當(dāng)前欄目游客不允許查看');
        $category['cat'] = $category;
        $this->view->assign($category);
        $this->view->assign($this->listSeo($category, $category['page'] ));
        if ($category['typeid'] == 1) //根據(jù)typeid來判斷欄目具體是那種類型的,然后分配不同的模板。
            $this->view->display($category['listtpl']);//這個(gè)方法很重要,對(duì)于我們自定義controller和使用自己的模板來說就靠這個(gè)方法了。
//如果我們?cè)赿isplay參數(shù)中填入自己的模板,那么系統(tǒng)就會(huì)調(diào)用我們自己的模板了。
        else if ($category['typeid'] == 2) 
            $this->view->display($category['pagetpl']);
        else if  ($category['typeid'] == 3) {
            header('Location: ' . $category['http']);
        }
    }
    else if ($this->get('id')){//如果url中包含的是id信息,那么就是詳情頁。
        $id    = (int)$this->get('id');
        $content  = $this->db->setTableName('content')->find($id);
        if (empty($content)) {
            header('HTTP/1.1 404 Not Found');
            $this->show_message('不存在此內(nèi)容!');
        }
        if (empty($content['status'])) $this->show_message('此內(nèi)容正在審核中不能查看!');
        $category   = $this->category_cache[$content['catid']];
        if($category['islook'] && !$this->member_info) $this->show_message('當(dāng)前欄目游客不允許查看');
        $content_add = $this->db->setTableName($category['tablename'])->find($id);
        $content_add = $this->handle_fields($this->content_model[$content['modelid']]['fields'], $content_add);
        $content  = $content_add ? array_merge($content,$content_add) : $content;
        $content['page']   = (int)$this->get('page') ? (int)$this->get('page') : 1;
        if (strpos($content_add['content'], '[XiaoCms-page]') !== false) {//分頁
            $pdata  = array_filter ( explode('[XiaoCms-page]', $content_add['content']) );
            $pagenumber = count($pdata);
            $content['content'] = $pdata[$content['page']-1];
            $pageurl = $this->view->get_show_url($content, 1);
            $pagelist = xiaocms::load_class('pager');
            $pagelist = $pagelist->total($pagenumber)->url($pageurl)->num(1)->hide()->page($content['page'])->output();
            $this->view->assign('pagelist', $pagelist);
        }

        $content['cat'] = $category;//上一篇,下一篇
        $prev_page = $this->db->setTableName('content')->order('id DESC')->getOne(array('id<?', 'modelid=' .$content['modelid'], 'status!=0'), $id);
        if ($prev_page) $prev_page['url'] =  $this->view->get_show_url($prev_page);
        $next_page = $this->db->setTableName('content')->order('id ASC')->getOne(array('id>?', 'modelid=' .$content['modelid'] , 'status!=0'), $id);
        if ($next_page) $next_page['url'] =  $this->view->get_show_url($next_page);
        $this->view->assign($content);
        $this->view->assign($this->showSeo($content, $content['page']));
        $this->view->assign(array(
            'prev_page' => $prev_page,
            'next_page' => $next_page,
        ));
        $this->view->display($category['showtpl']);
    }
    else {//如果url中沒有這些信息,就是主頁。
        $this->view->assign(array(
            'index'           => 1,
            'site_title'       => $this->site_config['site_title'],
            'site_keywords'    => $this->site_config['site_keywords'], 
            'site_description' => $this->site_config['site_description'],
        ));
        $this->view->display('index.html');
    }
    }
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容