dsjojts9734 2012-02-13 23:13
浏览 30
已采纳

具有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;

  • 写回答

4条回答 默认 最新

  • dongxian7194 2012-03-14 03:09
    关注

    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;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向