使用缩写名称codeigniter加载视图

时间:2014-01-21 12:08:54

标签: php codeigniter

减少$ data变量中视图的输入。我希望只是视图能够获取common / file_name而不是全长并且对于我的if语句都是相同的

库或自定义库中是否有一个可以使这项工作的功能我只是不想将完整的视图名称只放在最后的段中foldername / file

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Home extends CI_Controller {


public function index(){
$data['column_left'] = $this->load->view('common/column_left');
$data['column_right'] = $this->load->view('common/column_right');
$data['content_top'] = $this->load->view('common/content_top');
$data['content_bottom'] = $this->load->view('common/content_bottom');
$data['footer'] = $this->load->view('common/footer');
$data['header'] = $this->load->view('common/header');

if(file_exists('template/common/home.tpl')) { // can't get it to pick up file in theme
     $this->load->view('template/common/home.tpl', $data);
} else {
     $this->load->view('default/template/common/home.tpl', $data); // theme
}
}
}

2 个答案:

答案 0 :(得分:0)

您可以使用自定义library。这是我以前回答的一般提示。您可以根据需要进行更改。您可以从controllerconfig传递主题。

创建一个名为template.php的新库文件,并编写一个名为load_template的函数。在该函数中,使用上面的代码。

public function load_template($view_file_name,$data_array=array()) {

$ci = &get_instatnce();

$ci->load->view("header");

$ci->load->view($view_file_name,$data_array);

$ci->> load->view("footer");

}

您必须在config文件夹中的自动加载文件中加载此库。 所以您不想加载所有控制器。

您可以使用此功能

$this->template->load_template("index");

如果您想将日期传递给查看文件,则可以通过$ data_array

发送

答案 1 :(得分:0)

是的!它是(一种如何使这项工作的方式),通过MY_Controller.php扩展你的控制器(在/ application / core /中)

它看起来应该是这样的

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class MY_Controller extends CI_Controller {

    protected $data = Array();

    public function __construct() {
        parent::__construct();
        $this->output->enable_profiler(FALSE);
    }

    public function _render() { //as I understand now you dont need parameter

        $template['column_left'] = $this->load->view('common/column_left', '', TRUE);
        $template['column_right'] = $this->load->view('common/column_right', '', TRUE);
        $template['content_top'] = $this->load->view('common/content_top', '', TRUE);
        $template['content_bottom'] = $this->load->view('common/content_bottom', '', TRUE);
        //$this->load->view($view_file, $this->data);
        $template['footer'] = $this->load->view('common/footer', '', TRUE);
        $template['header'] = $this->load->view('common/header', '', TRUE);

        if(file_exists('template/common/home.tpl')) { 

            $this->load->view('template/common/home.tpl', $template);

        } else {

            $this->load->view('default/template/common/home.tpl', $template);

        }

    }

}

现在改为使用$data['items'] = array();使用$this->data['items'] = array();

最后你的家庭控制器应该延长MY_Controller

class Home extends MY_Controller {

使用_render()很容易

每当您需要呈现页面时,只需执行$this->_render();

相关问题