具有2D数组的CodeIgniter模板库

I am using the following template library. What I am trying to do is to load one or more upper views inside a template,as an array so that I can easily load them in the template with a for-each loop.

This is a simple example on how it can be used on a controller:

    function index() {

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

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

    $this->template->load('main', $partials, $data);
    }

On the View you have an html like:

    <html>
    ....
    <?=$content?>
    ...
    </html?>

This is what I am trying to use: Controller:

   $partials = array('content'=>'c_location',
       array(
       'first_upper_content'=>'1_u_location','second_upper_content'=>'2_u_location'
       ) 
   );

So for example I could pass for upper_content, a top header as "first_upper_content" and a slide for "second_upper_content" and then the remainder of the content for "content".

Html:

    ...
     <?=$upper_content?> 

     <--if upper_content is a array, 
     I could display each content with a for loop-->

     <?=$content?>

When I try everything I get:

Message: pathinfo() expects parameter 1 to be string, array given

Filename: core/Loader.php

Line Number: 759

How can I implement this? I am thinking of modifying the

// Load views into var array

Inside the Template.php & adding a foreach loop to the html;

This is what you want:

    // Load views into var array
    foreach(array_flat($view) as $key => $file)

Include the "array_flat" function call in Template.php, as shown above.

You will need to define this function. You can do this in any auto-loaded helper, or even include it the own Template class (in this case, you should call it as $this->array_flat in the code above). Here is it:

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;
}

To load partials, all you need to do is use:

$this->load->view('partial_location',$data);

inside your main view. So if you have a main view called home_page.php, and you want to load partials for headers and footers, you can just use:

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

// Main page layout

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

If you want to use custom or different data inside your partials, just define them in your controller:

$data = array(
   'content'        => 'about',
   'header_content' => 'Welcome to the site!',
   'footer_content' => 'Made by me!'
);

And in your main view file:

$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!'

Just make your main template view, and have something like this in it:

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

In the controller, $data['page'] would contain view info, either its name (string) or an array of names to call in order. Of course, you'll have to have premade views with those names.

This was from the top of my head, but it should do what you wanted. This is done without template libraries, just plain CI.

What you're trying to do is not possible with that library.

The error you received indicates the CI Loader class is being passed invalid data. So taking a look at the library's load function, your code will fail right here:

    // Load views into var array
    foreach($view as $key => $file)
    {
        $tpl[$key] = $this->CI->load->view($file, $vars, TRUE);
    }

The library passes your nested array in $partials directly to the CI Loader. With your data, this line works out to:

$tpl[0] = $this->CI->load->view(array(
       'first_upper_content'=>'1_u_location','second_upper_content'=>'2_u_location'
       ), vars, TRUE);

You can see in the CI user guide that isn't valid. It seems to me that your options are to either overhaul the library or change your approach.