自定义助手Codeigniter

时间:2014-01-19 14:07:56

标签: php codeigniter

你好目前我只是按照我想在数组中加载视图的方式制定出来,但这是打字的漫长道路。

我如何最好创建一个帮助文件/函数,让我仍然可以输入页眉和页脚等,然后选择视图,但是我在控制器中键入了全长。

目前我必须这样输入

以这种方式工作只是为了长期

$this->children = array(
   'header' => $this->load->view('theme/default/template/common/header.tpl'),
   'column_left' => $this->load->view('theme/default/template/common/column_left.tpl'),
   'column_right' =>  $this->load->view('theme/default/template/common/column_right.tpl'),
    'content_top' => $this->load->view('theme/default/template/common/content_top.tpl'),
    'content_bottom' => $this->load->view('theme/default/template/common/content_bottom.tpl'),
    'footer' => $this->load->view('theme/default/template/common/footer.tpl')

);

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

我想要一个我可以在控制器中调用的帮助文件,我只需要输入。如果我不必将完整的视图名称放在帮助器所在的位置,它就会变得更容易

public function index() {

    $this->load->helper('template'); "This is the name I have chosen"

    $this->children = array(
    'header',
    'column_left',
    'column_right',
    'content_top',
    'content_bottom',
    'footer'
    );

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

}

2 个答案:

答案 0 :(得分:0)

我认为这应该有效:

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

if ( ! function_exists('final_view'))
{
    //$data - CI custom $data (from controller to views)
    //$views - an array with your children views
    function final_view($data, $views = array())
    {
        $CI =& get_instance();
        //Include each views
        foreach($views as $view)
        {
            $CI->load->view('theme/default/template/common/'.$view.'.tpl');
        }

        //Include the main view tpl and the custom CI $data
        $CI->load->view('theme/default/template/common/home.tpl', $data);
    }   
}
编辑:正如tomexsans所说,我添加了CI实例。

答案 1 :(得分:0)

不建议使用帮助器,因为您需要core个函数,只需创建一个新核心并用它扩展每个控制器

然后你可以做一些事情(尚未测试),但它会是动态的,授予你始终使用代码上的header,column_left,column_right,content_top,content_bottom,footer组合

//param 1 is the config of the view
// param 2 is the data to be passed on the view
function final_view($view,$data)
{   

    //set all path views
    $header['with_stars'] = 'path to view';
    $header['without_bg'] = 'path to view';
    $header['big_title'] = 'path to view';

    $column_left['black'] = 'path to view';
    $column_left['blue'] = 'path to view';
    $column_left['red'] = 'path to view';


    $column_right['big_right'] = 'path to view';
    $column_right['small_right'] = 'path to view';

    $content_top['big_blue_top'] = 'path to view';
    $content_top['small_red_top'] = 'path to view';


    $content_bottom['no_title'] = 'path to view';
    $content_bottom['with_title'] = 'path to view';


    $footer['with_copyright'] = 'path to view';
    $footer['with_big_date'] = 'path to view';


    //combine in one array
    $glue = [
      'header' => $this->load->view($header[$view[0]]),
      'column_left' => $this->load->view($column_left[$view[1]]),
      'column_right' => $this->load->view($column_right[$view[2]]),
      'content_top' => $this->load->view($content_top[$view[3]]),
      'content_bottom' => $this->load->view($content_bottom[$view[4]]),
      'footer' => $this->load->view($footer[$view[5]]),
      'data' => $data
    ];
    // you could do other stuff here before loading the view
    $this->load->view('path_to_view',$glue);
}

而不是加载视图,你可以这样做

//header,column_left,column_right,content_top,content_bottom,footer
$view = array('with_stars','black','big_right','big_blue_top','with_title','with_big_date');
$data['somedata'] = 'data as string';
$data['somedata2'] = array('data as string','some array');

// you could do other stuff here before loading the view
$this->final_view($view,$data);