【问题标题】:CodeIgniter Template Library with a 2D array带有二维数组的 CodeIgniter 模板库
【发布时间】:2012-03-05 09:12:00
【问题描述】:

我正在使用以下模板library。我要做的是在模板中加载一个或多个上部视图,作为一个数组,以便我可以使用 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);
    }

在视图上你有一个像这样的 html:

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

这是我正在尝试使用的: 控制器:

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

例如,我可以将upper_content 传递给顶部标题,将顶部标题传递给“first_upper_content”,将幻灯片传递给“second_upper_content”,然后将其余内容传递给“content”。

HTML:

    ...
     <?=$upper_content?> 

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

     <?=$content?>

当我尝试所有我得到的东西时:

消息:pathinfo() 期望参数 1 是字符串,给定数组

文件名:core/Loader.php

行号:759

我该如何实现呢?我正在考虑修改

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

在 Template.php 中 & 在 html 中添加一个 foreach 循环;

【问题讨论】:

  • 即使经过编辑,这仍然是一个非常不清楚的问题
  • 你看到我的回答了吗?它对你有用吗?

标签: php model-view-controller codeigniter


【解决方案1】:

这就是你想要的:

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

在 Template.php 中包含“array_flat”函数调用,如上所示。

您需要定义这个函数。您可以在任何自动加载的帮助程序中执行此操作,甚至可以将其包含在自己的 Template 类中(在这种情况下,您应该在上面的代码中将其称为 $this-&gt;array_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;
}

【讨论】:

    【解决方案2】:

    使用该库无法实现您想要做的事情。

    您收到的错误表明 CI Loader 类正在传递无效数据。所以看看library的加载函数,你的代码会在这里失败:

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

    您可以在CI user guide 中看到无效。在我看来,你的选择是要么彻底改造图书馆,要么改变你的方法。

    【讨论】:

      【解决方案3】:

      只需制作您的主模板视图,并在其中包含以下内容:

      $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。

      【讨论】:

        【解决方案4】:

        要加载部分,您需要做的就是使用:

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

        您的主视图中。因此,如果您有一个名为 home_page.php 的主视图,并且您想加载页眉和页脚的部分内容,您可以使用:

        $this->load->view('header');
        
        // Main page layout
        
        $this->load->view('footer');
        

        如果您想在您的部分中使用 custom不同 数据,只需在您的控制器中定义它们:

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

        【讨论】:

        • 我已经澄清了问题中的一些细节,也许这有助于理解我想要实现的目标。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2022-08-07
        • 2012-05-14
        • 2011-08-30
        • 1970-01-01
        • 1970-01-01
        • 2011-11-28
        • 1970-01-01
        相关资源
        最近更新 更多