【问题标题】:Custom Helper Codeigniter自定义助手 Codeigniter
【发布时间】:2014-02-08 15:56:54
【问题描述】:

您好,目前我只是想出了将视图加载到数组中的方法,但输入的方法很长。

如何最好地创建一个帮助文件/函数,让我仍然可以输入页眉和页脚等并获取视图,但无需我在控制器中输入全长。

目前我必须这样输入

以这种方式工作很长时间

$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);

}

【问题讨论】:

  • 请最好的选择是转移到 hmvc 概念,因为这种方法的加载时间会以任何方式增加,检查 hmvc 功能
  • 我已经使用了 hmvc,而不是对不起
  • 我过去使用过 hmvc 而不是我现在所追求的。尝试像打开购物车一样设置我的 codeigniter 模板/主题,但他们使用自定义模板引擎。

标签: php codeigniter


【解决方案1】:

我认为这应该可行:

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

【讨论】:

  • this 会抛出一个错误,当不在对象上下文或类似的情况下使用 this。助手没有与核心控制器链接,所以你不能只打电话$this,如果我错了,请纠正我/
  • 但我仍然希望能够在控制器中调用我的 $this->children 数组头等。
  • 请解释一下在哪里
  • 您现在在哪里更正了它们。但是你把它放在那里 15 分钟,出现语法和逻辑错误以及其他便利(比如错误的函数名)
  • 我刚刚为其他每个视图的页眉、页脚、column_left、column_right、content_top、content_bottom 创建了控制器。并在控制器中包含了视图文件。我将如何做到这一点,以便像上面那样键入数组的乳清将拾取控制器。
【解决方案2】:

不建议使用助手,因为您将需要 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);

【讨论】:

  • 按照我所追求的方式使用我的阵列。我试图让它类似于打开购物车设置。有了模板系统。其他 codeigniter 模板引擎不是我想要的。
猜你喜欢
  • 2012-03-28
  • 1970-01-01
  • 2020-04-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多