codeigniter:加载多个视图

时间:2012-01-08 06:42:40

标签: codeigniter layout views

我想分配一个变量$ layout_data ['navigation'] = $ this-> load('layout / navigation') 导航只是一个列表。 当我将此变量传递给我的布局时,列表位于顶部。

这是我的代码:

htaccess的

RewriteEngine On
RewriteBase /ci/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /ci/index.php/$1 [L]

控制器主页

<?php
    class Home extends CI_Controller{
        function index() {

            $layout_data['navigation']=$this->load->view('navigation');
            $this->load->view('layout/main',$layout_data);
        }//end of index
    }//end of class
    ?>

布局/导航(其视图)

  <li><a href="#"><span>About Us</span></a></li>
    <li><a href="#"><span>Objective</span></a></li>

layout / main(这是我的布局)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Rainbow Handicraft-giving training and employment oppurtunities to women |in association with Rainbow Children Home</title>
<link href="<?php echo $this->config->base_url('assets/css/css/style.css'); ?>" type="text/css" rel="stylesheet" media="all"/>
</head>

<body>
<div id="top_header">
    In association with Rainbow Children Home Nepals
</div>
<!-- end of top header-->
<div id="container">

        <div id="header">

                <div id="left_logo">
                </div>

                 <div id="logo">
                            <h1>Rainbow Handicraft Nepal</h1>   
                             <h2>Lakeside Pokhara 06 Nepal</h2>
                            <h3>Email- rhn@gmail.com</h3>
                 </div>


                <div id="right_logo">
                </div>

                <div id="right_logo2">
                </div>

                <div id="right_logo3">
                </div>
        </div>   

<!--end of header-->

        <div id="wrapper">

                <div id="menu">



   <div id="tabs">

<ul>
    <li></li>
<?php  echo $content_navigation;?>

      </ul>
      </div>







                </div><!--end of menu-->

                <div id="main_content">
                <div id="leftbar">
                        <div id="left_content">

                        <p class="title">mytitle</p>
 <p class="mycontent">                       

mycontent
</p>
</div><!--end of left content-->
<div id="left_content">

<p class="title">asdfasdf</p>
 <p class="mycontent">                       
sadfsadfasdf</p>
                        </div><!--end of left content-->
                        </div><!--end of left bar-->
                    <div id="rightbar">
                    <div id="right_content">
                    <div>
                      <p class="title">This is the title</p>
                   <p class="mycontent"> mycontent
                   </p>
                    </div>

                    </div><!--end of right content-->





                    <div id="right_content">
                    <div>
                      <p class="title">This is the title</p>
                   <p class="mycontent"> this is right content
                   </p>
                    </div>

                    </div><!--end of right content-->

                    </div><!--end of right bar-->






        </div><!--end of wrapper-->


     <div class="push"></div>
 </div><!--end of container-->
         <div id="footer">

                <div id="footer_main">
               Rainbow Handicraft Nepal</br>
              Po Box 210,Dihikopatan, Lakeside ,Pokhara-6,Nepal</br>
               © Copyrights 2010 All rights Reserved. </br>Designed by: <span class="sagaritabd"><i><b>sagaritabd</b></i></span>
                </div>
         </div><!--end of footer-->


</body>
</html>


when i see source of output file i see the problem but cant fix it

 <li><a href="#"><span>About Us</span></a></li>

    <li><a href="#"><span>Objective</span></a></li>

    <li><a href="#"><span>Items</span></a></li>

    <li><a href="#"><span>Contact</span></a></li><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>Rainbow Handicraft-giving training and employment oppurtunities to women |in association with Rainbow Children Home</title>

<link href="http://localhost/ci/assets/css/css/style.css" type="text/css" rel="stylesheet" media="all"/>

</head>



<body>

<div id="top_header">

    In association with Rainbow Children Home Nepals

</div>

<!-- end of top header-->

<div id="container">



        <div id="header">



                <div id="left_logo">

                </div>


......

please help

2 个答案:

答案 0 :(得分:4)

这一行:

$layout_data['navigation'] = $this->load->view('layout/navigation');

...会立即输出视图。您需要将第三个参数设置为TRUE

$layout_data['navigation'] = $this->load->view('layout/navigation', NULL, TRUE);

如果需要,第二个参数是您的数据数组。如果您不需要,可以传入一个空字符串或NULL

它位于用户指南中,一直到底部:

  

将视图作为数据返回

     

有第三个可选参数可让您更改行为   该函数使它以字符串形式返回数据而不是发送   它到你的浏览器。如果要处理数据,这可能很有用   某种程度上来说。如果将参数设置为true(布尔值),它将返回   数据。默认行为为false,将其发送到您的浏览器。   如果要返回数据,请记住将其分配给变量:

答案 1 :(得分:0)

作为附录,您可能希望将一些逻辑添加到MY_Controller中。我猜你会在每个页面加载导航,所以在MY_Controller中加载逻辑作为构造函数的一部分,然后扩展该类,并确保存在一个像$ data []这样的受保护变量。这样,您就可以强制执行导航数据的一致命名,并确保它始终通过。