codeigniter: loading multiple views

2019-09-03 14:59发布

问题:

i want to assign a variable $layout_data['navigation']=$this->load('layout/navigation') navigation is just a list. when i pass this variable to my layout the list comes at the top.

here are my codes:

htaccess

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

controller home

<?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
    ?>

layout/navigation (its a view)

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

layout/main (this is my layout)

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

回答1:

This line:

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

...will output the view immediately. You need to set the third parameter to TRUE:

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

The second parameter is your data array, should you need it. You can pass in an empty string or NULL if you don't need it.

It's here in the user guide, all the way down at the bottom:

  • http://codeigniter.com/user_guide/general/views.html

Returning views as data

There is a third optional parameter lets you change the behavior of the function so that it returns data as a string rather than sending it to your browser. This can be useful if you want to process the data in some way. If you set the parameter to true (boolean) it will return data. The default behavior is false, which sends it to your browser. Remember to assign it to a variable if you want the data returned:



回答2:

As an addendum, you may want to add some of that logic to a MY_Controller. I'm guessing you will be loading the navigation every page, so do that load logic as part of the constructor in MY_Controller and then extend that class, and make sure there is a protected variable like $data[] that exists as well. This way you can enforce consistent naming of the navigation data and make sure it always gets passed.