2014-01-19 78 views
0

你好目前我只是想办法,我想在数组中加载我的意见,但它是键入的很长一段路。自定义帮手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); 

} 
+1

请最好的选择是移动到hmvc的概念,因为这种方法的加载时间将以任何方式增加检查hmvc功能 –

+0

我有使用hmvc不是我后对不起 – user3113124

+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实例。

+0

这会抛出一个错误,使用此时不在对象上下文或类似的东西。该助手没有与核心控制器链接,所以你不能只调用'$ this',纠正我,如果我错了/ – tomexsans

+0

,但我仍然希望能够调用我的$ this->儿童数组头等。在控制器中。 – user3113124

+0

请解释一下 – Kalzem

0

这不建议使用助手,因为你将需要core功能,只需要创建一个新的核心,每个控制器将其扩展

那么你可以做一些事情likethis(尚未测试),但它会动态的,给予你总是在你的代码中使用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); 
+0

与我的数组我的方式。我试图让它类似于打开购物车设置。有了模板系统。其他的codeigniter模板引擎不是我所追求的。 – user3113124