两个控制器一个视图,CodeIgniter? [英] Two controllers for one view, CodeIgniter?

查看:109
本文介绍了两个控制器一个视图,CodeIgniter?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,我是新的CodeIgniter,需要一些帮助。我有一个控制器格式化一个帖子的内容区域。问题是,我还需要创建一个包含动态组的边栏,以及包含最近帖子的右列。这并不困难,我遇到的问题是,我想要侧边栏,右栏在每一个页面,我不想重新编码相同的位,以获得每个控制器中的数据。

Hey guys, I am new to CodeIgniter and need some help. I have a controller that formats the content area of a post. The problem is that I also need to create a sidebar that contains dynamic groups, and a right column that contains recent posts. This isn't hard, the problem I'm running into is that I want the sidebar, and right column on every page, and I don't want to recode the same bits to get the data in every controller.

没有复制/粘贴,最好的办法是什么?

What would be the best way to do this without copy/paste?

推荐答案

有很多方法可以做到这一点。

There are a lot of ways to do this.

1)模板:这是我的偏好大多数情况下(因为我的模板是复杂的),我使用类似的东西将我的视图变成一个变量:

1) Templating: This is my preference for most cases (because my templates are complex), I render my view into a variable using something like:

$content = $this->load->view('myview', $page_data, true);

然后我将它加载到模板解析器(fyi你可以加载到另一个视图) :

Then I load it into the template parser (fyi you could load it into another view too) like this:

$this->load->library('parser');
$data = array(
        'page_title' => 'My Page Title',
        'page_content' => $content,
        'side_bar' => side_bar(), // function which generates your side bar
        'right_col' => right_col() // function which generates your right column
        );
$this->parser->parse('my_template', $data);

然后你的模板是:

<html>
<head>
<title>{page_title}</title>
</head>
<body>
    <div>{side_bar}</div>
    <div>{page_content}</div>
    <div>{right_col}</div>
</body>
</html>

2)在视图中加载另一个视图:(假设你的菜单是视图而不是控制器)像这样:

2) Load another view in your view: (assumes you menu is a view not a controller) Something like this:

<?php $this->load->view('menu_view'); ?> 

3)PHP包括:你如何在纯PHP中使用类似这样的东西:

3) PHP Includes: exactly how you would do it in plain PHP (just include a url which points to a controller which returns a menu), Something like this:

<?php include("/common/sidebar"); ?>

Codeigniter将渲染该页面,然后包含它。

Codeigniter will render that page and then include it.

4)AJAX ..我使用这个如果模板内容中的内容不那么重要,像横幅,建议相关项目列表等。

4) AJAX.. i use this if the content in the "template" content is less important, like banners, suggested related item lists and such.

这篇关于两个控制器一个视图,CodeIgniter?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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