Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/253.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 带有2D阵列的CodeIgniter模板库_Php_Model View Controller_Codeigniter - Fatal编程技术网

Php 带有2D阵列的CodeIgniter模板库

Php 带有2D阵列的CodeIgniter模板库,php,model-view-controller,codeigniter,Php,Model View Controller,Codeigniter,我正在使用以下模板。我试图做的是在一个模板中加载一个或多个上部视图,作为一个数组,这样我就可以使用for-each循环轻松地将它们加载到模板中 这是一个关于如何在控制器上使用它的简单示例: function index() { $data['title'] = 'My page title'; $partials = array('content'=>'c_location'); //Load view about inside the template.

我正在使用以下模板。我试图做的是在一个模板中加载一个或多个上部视图,作为一个数组,这样我就可以使用for-each循环轻松地将它们加载到模板中

这是一个关于如何在控制器上使用它的简单示例:

    function index() {

    $data['title']  = 'My page title';

    $partials = array('content'=>'c_location'); //Load view about inside the template.

    $this->template->load('main', $partials, $data);
    }
   $partials = array('content'=>'c_location',
       array(
       'first_upper_content'=>'1_u_location','second_upper_content'=>'2_u_location'
       ) 
   );
$data = array(
   'content'        => 'about',
   'header_content' => 'Welcome to the site!',
   'footer_content' => 'Made by me!'
);
在视图中,您有一个类似以下内容的html:

    <html>
    ....
    <?=$content?>
    ...
    </html?>
例如,我可以将顶部标题作为“first_upper_content”,将幻灯片作为“second_upper_content”,然后将剩余内容作为“content”

Html:

。。。
当我尝试我得到的一切时:

消息:pathinfo()要求参数1为字符串,数组给定

文件名:core/Loader.php

电话号码:759

我如何实现这一点?我正在考虑修改这个文件

//将视图加载到var数组中


在Template.php内部&向html添加foreach循环

要加载分区,只需使用:

$this->load->view('partial_location',$data);
内部您的主视图。因此,如果您有一个名为home_page.php的主视图,并且希望加载页眉和页脚的部分,则可以使用:

$this->load->view('header');

// Main page layout

$this->load->view('footer');
如果要在分区中使用自定义或不同的数据,只需在控制器中定义它们:

    function index() {

    $data['title']  = 'My page title';

    $partials = array('content'=>'c_location'); //Load view about inside the template.

    $this->template->load('main', $partials, $data);
    }
   $partials = array('content'=>'c_location',
       array(
       'first_upper_content'=>'1_u_location','second_upper_content'=>'2_u_location'
       ) 
   );
$data = array(
   'content'        => 'about',
   'header_content' => 'Welcome to the site!',
   'footer_content' => 'Made by me!'
);
在主视图文件中:

$this->load->view('header',$header_content);
// would echo 'Welcome to the site!'

echo $content;
// would echo 'about'

$this->load->view('footer',$footer_content);
// would echo 'Made by me!'

只需创建主模板视图,并在其中包含以下内容:

$this->load->view('header');
if (is_array($page) {
    foreach($page as $key=>$val){
        $this->load->view($key, $val); // $val being optional parameters
    }
} else {
    $this->load->view($page);
} 
$this->load->view('footer');
在控制器中,
$data['page']
将包含视图信息,包括其名称(字符串)或按顺序调用的名称数组。当然,您必须具有具有这些名称的预制视图


这是我脑子里想出来的,但它应该满足你的要求。这是在没有模板库的情况下完成的,只有普通CI。

使用该库不可能完成您尝试的操作

您收到的错误表明正在向CI加载程序类传递无效数据。因此,看看的load函数,您的代码将在此处失败:

    // Load views into var array
    foreach($view as $key => $file)
    {
        $tpl[$key] = $this->CI->load->view($file, $vars, TRUE);
    }
库将
$partials
中的嵌套数组直接传递给CI加载程序。根据您的数据,这一行的作用是:

$tpl[0] = $this->CI->load->view(array(
       'first_upper_content'=>'1_u_location','second_upper_content'=>'2_u_location'
       ), vars, TRUE);
您可以在中看到这是无效的。在我看来,您的选择是要么彻底检查库,要么改变方法。

这就是您想要的:

    // Load views into var array
    foreach(array_flat($view) as $key => $file)
如上图所示,在Template.php中包含“array\u flat”函数调用

您需要定义此函数。您可以在任何自动加载的帮助器中执行此操作,甚至可以将其包含在自己的模板类中(在这种情况下,您应该在上面的代码中将其称为
$this->array\u flat
)。这是:

function array_flat($array)
{
    $result = array();

    foreach ($array as $key => $value)
    {
        if (is_array($value))
        {
            $result = array_merge($result, array_flat($value));
        }
        else
        {
            $result[$key] = $value;
        }
    }

    return $result;
}

我已经澄清了问题中的一些细节,也许这有助于理解我试图实现的目标。即使经过编辑,这仍然是一个非常不清楚的问题。你看到我的答案了吗?对你有用吗?