简体   繁体   English

Codeigniter将数据传递到嵌套视图

[英]codeigniter pass data to nested view

I have done very much research on this topic but I cannot find how i can do this. 我已经对该主题做了很多研究,但是我找不到如何做到这一点。 I am trying to add data to the $data parameter in a view that is being called from the controller of another view. 我试图将数据添加到从另一个视图的控制器调用的视图的$data参数中。 However, any data i add to the subview via the subcontroller cannot be accessed by the subview . 然而,任何数据I添加到subview经由subcontroller不能被访问的subview However, when I try to pass data to the subview via the client view, it works just fine. 但是,当我尝试通过客户端视图将数据传递到子视图时,它就可以正常工作。 Most of the fixes on SO seem to reference just calling the $key in $data['key'] rather than $data so that doesn't seem really relevant here... SO的大多数修补程序似乎只引用$key in $data['key']而不是$ data,因此在这里似乎并不重要...

I have two classes: 我有两节课:

  • welcome.php - a page welcome.php-页面
  • welcomemenu.php - a set of controls intended to be loaded into welcome.php welcomemenu.php-打算加载到welcome.php中的一组控件

Here is my Client Controller (the page that it's on, welcome.php ), which stores the return val from subview $welcomemenu in its own $data array...: 这是我的客户端控制器(位于其上的页面welcome.php ),它将子视图$welcomemenu的返回值存储在其自己的$ data数组中...:

<?php

class Welcome extends CI_Controller {

    function __construct() {
        parent::__construct();      
    }

    function index() {
        //echo 'this is the Welcome index function';
        $data['clienttestdata'] = 'data from welcome.php!';
        $data['welcomemenu'] = $this->load->view('welcome/welcomemenu', $data, true);

        $this->load->helper('url');
        $this->load->view('templates/header');
        $this->load->view('pages/welcome', $data);
        $this->load->view('templates/footer');

    }
}

And here is the client view ("welcome_view.php" - seems simple enough. The $welcomemenu var is where I put the returns from my component class...): 这是客户端视图(“ welcome_view.php”-看起来很简单。$ welcomemenu var是我将组件类的返回值放在其中的位置...):

    <section id="allWelcomeContent" class="mainBody">
        <header id="mainPageHdr" class=mainPageHdr>
            <!-- other stuff from my header -->
        </header>
        <!-- this is where i want to put the welcome menu... -->
        <section id="mainWelcomeContent" class="mainContent">
            <div>
                <?php echo $welcomemenu;?>
            </div>
        </section>
    </section>

And here is the Controller for my sub-component welcomemenu.php : 这是我的子组件welcomemenu.php的控制器:

<?php

class Welcomemenu extends CI_Controller {

    public function __construct() {
        parent::__construct();
    }

    public function index() {
        $data['menu_items'] = array('About', 'Portfolio', 'Resume', 'Fun', 'Blog'); 
        $data['testdata'] = 'data from welcomemenu.php!';

        $this->load->view('welcome/welcomemenu', $data);
    }
}

And lastly: Here is the sub-view that is supposed to get data from its own controller, but cannot, even though it can take data from a calling client (ie, the $clienttestdata shows up fine but $testdata doesn't)! 最后:这是应该从其自己的控制器获取数据的子视图,但是即使它可以从调用方客户端获取数据也不能(例如,$ clienttestdata显示正常,但$ testdata不能)!

<section>
<!-- TODO:  make this element repeatable so content can load from controller and/or model. -->
<div id="divMenuItems">
    <?php echo $clienttestdata;?>
    <?php echo $testdata;?>
</div>
</section>

Still i couldn't find any proper solution. 我仍然找不到任何合适的解决方案。 if anyone then please give me 如果有人的话,请给我

When you're including the welcomemenu partial in your Welcome/index method, you have to remember that the view does not go through its own controller. 当您将Welcomemenu部分包含在Welcome / index方法中时,您必须记住该视图不会通过其自己的控制器。 Instead, its contents are returned as a string and stored as a parameter. 而是将其内容作为字符串返回并存储为参数。 It gets all of its own parameters through the ones you send to it via $data : 它通过您通过$data发送给它的参数来获取自己的所有参数:

$data['welcomemenu'] = $this->load->view('welcome/welcomemenu', $data, true);

This view will thus have access to everything in $data so far - nothing extra is added through the Welcomemenu controller. 到目前为止,该视图将可以访问$data所有内容-通过Welcomemenu控制器无需添加任何其他功能。 So, in the above case, it will have: 因此,在上述情况下,它将具有:

array
(
   'clienttestdata' => 'data from welcome.php!'
)

If you add the parameters you need to $data (as $data['testdata'] ), your sub-view will have what it needs. 如果将所需的参数添加到$data (作为$data['testdata'] ),则子视图将具有所需的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM