CodeIngiter:在另一个视图中加载一个视图 [英] CodeIngiter: Load a view inside of another view

查看:72
本文介绍了CodeIngiter:在另一个视图中加载一个视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用CodeIgniter。我想在其他视图中加载视图。我该怎么做?

I'm using CodeIgniter. I want to load views inside of other views. How can I do this?

示例:

假设我有一个视图称为 CommentWall。在CommentWall中,我想要一堆评论视图。我在整个网站上都使用评论视图!

Let's say I have a "view" called "CommentWall". In CommentWall, I want a bunch of "Comment" views. I use the view for "comment" all over my site!

我该怎么做?看来CodeIgniter只允许我顺序加载视图,考虑到我在其他视图内部使用可重用的视图,这有点奇怪!

How can I do this? It seems CodeIgniter only allows me to load views sequentially, which is sort of strange considering I use reusable views INSIDE of other views!

我可以做一个 $ this-> load-> view('comment'); 在我的CommentWall视图内?还是有其他方法可以使可重用视图包含在内部视图中?

Can I do a $this->load->view('comment'); inside of my view for CommentWall? Or is there some other way to have reusuable views contained inside a view?

推荐答案

您可以轻松地做到这一点,只需从控制器中加载主视图,例如 CommentWall

You can do it easily, just load the main view, for example CommentWall from the controller

$this->load->view('CommentWall');

CommentWall 中添加子视图可以在 CommentWall 视图内添加以下行

To add child views in CommentWall view you can add following line inside your CommentWall view

$this->view('Comment');

例如,如果加载 CommentWall 视图从您的控制器这样

For example, if you load CommentWall view from your controller like this

$data['comments'][] = 'Comment one';
$data['comments'][] = 'Comment two';

// load the parrent view
$this->load->view('CommentWall', $data);

现在在 CommentWall 中(父视图) ,如果您输入此

Now in the CommentWall (parent view), if you put this

foreach ($comments as $comment) {
    $this->view('Comment', array('comment' => $comment));
}

并在您的评论中(子视图),如果您有此

And in your Comment (child view) if you have this

echo $comment . '<br />';

然后您应该获得类似这样的输出

Then you should get output something like this

Comment one

Comment two

更新::Alos,查看此答案

这篇关于CodeIngiter:在另一个视图中加载一个视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆