Codeigniter:构建局部视图的最佳方式 [英] Codeigniter: Best way to structure partial views

查看:30
本文介绍了Codeigniter:构建局部视图的最佳方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您将如何在 Codeigniter 中构建以下页面?

我想为每个部分创建单独的控制器

  1. 左导航
  2. 内容导航
  3. 登录名
  4. 排行榜

不包括内容部分(因为这会根据左侧导航中的链接和用作有点子菜单的内容导航而改变).所有其他部分保持大致相同

我想过这样做:

Class User_Profile 扩展控制器{函数索引(){$this->load_controller('Left_Nav');$this->load_controller('Content_Nav');$this->load_controller('Login_Name');$this->load_controller('排行榜', '董事会');$this->Left_Nav->index(array('highlight_selected_pa​​ge' => 'blah'));$this->load('用户');$content_data = $this->User->get_profile_details();$this->view->load('content', $content_data);$this->Login_Name->index();$this->Board->index();}}

显然这个 load_controller 不存在,但是这个功能会很有用.每个部分的控制器从模型中获取所需的数据,然后通过$this->view->load()

加载页面

在所有左侧导航链接(如新闻、用户、关于我们等)中使用此代码可能会令人头疼.但同样,并非每个导航链接都包含所有这些部分,因此我需要将这些部分作为部分视图"

谁能提出更好的方法?

解决方案

我不能保证这是最好的方法,但我创建了一个像这样的基本控制器:

class MY_Controller 扩展 CI_Controller {公共 $title = '';//模板将默认使用它来包含 default.csspublic $styles = array('default');函数 _output($content){//加载基本模板,输出内容可作为 $content$data['content'] = &$content;$this->load->view('base', $data);}}

名为base"的视图是一个模板(包含其他视图的视图):

<html xmlns="http://www.w3.org/1999/xhtml"><头><?php $this->load->view('meta');?><身体><div id="包装器"><?php $this->load->view('header');?><div id="内容"><?php echo $content;?>

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

这实现的是每个控制器都将其输出包装在基本模板中,并且视图具有有效的 HTML,而不是在一个视图中打开标签并在另一个视图中关闭.如果我希望特定控制器使用不同的模板或不使用模板,我可以覆盖神奇的 _output() 方法.

一个实际的控制器看起来像这样:

class Home 扩展 MY_Controller {//覆盖标题公共 $title = '家';函数 __construct(){//将样式表 (home.css) 附加到默认值$this->styles[] = 'home';}函数索引(){//这个视图的输出将被包裹在基础模板中$this->load->view('home');}}

然后我可以像这样在我的视图中使用它的属性(这是填充 元素的元"视图):

echo "{$this->title}";foreach ($this->styles as $url)echo link_tag("styles/$url.css");

我喜欢我的方法,因为它尊重 DRY 原则,并且页眉、页脚和其他元素在代码中只包含一次.

How would you structure the below page in Codeigniter?

I thought about creating seperate controllers for each section

  1. Left nav
  2. Content nav
  3. Login name
  4. Leaderboard

Excluding the content section (as this changes depending on the link on the left nav and content nav used as a kinda sub-menu). All the other sections remain roughly the same

I thought about doing:

Class User_Profile extends Controller
{

    function index()
    {
        $this->load_controller('Left_Nav');
        $this->load_controller('Content_Nav');
        $this->load_controller('Login_Name');
        $this->load_controller('Leaderboard', 'Board');

        $this->Left_Nav->index(array('highlight_selected_page' => 'blah'));

        $this->load('User');

        $content_data = $this->User->get_profile_details();

        $this->view->load('content', $content_data);

        $this->Login_Name->index();
        $this->Board->index();
    }

}

Obviously this load_controller does not exist but this functionaility would be useful. The controller for each section gets the data required from the model and then loads the page through $this->view->load()

It could be a headache to have this code in all the left nav links like News, Users, About Us, etc.. But then again not every nav link has all those sections so I need that flexability of having the sections as a "partial view"

Can anyone suggest a better way of doing this?

解决方案

I can't vouch that this is the best approach, but I create a base controller like this:

class MY_Controller extends CI_Controller {

    public $title = '';
    // The template will use this to include default.css by default
    public $styles = array('default');

    function _output($content)
    {
        // Load the base template with output content available as $content
        $data['content'] = &$content;
        $this->load->view('base', $data);
    }

}

The view called 'base' is a template (a view that includes other views):

<?php echo doctype(); ?>
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <?php $this->load->view('meta'); ?>
    </head>
    <body>
        <div id="wrapper">
            <?php $this->load->view('header'); ?>

            <div id="content">
                <?php echo $content; ?>
            </div>

            <?php $this->load->view('footer'); ?>
        </div>
    </body>
</html>

What this achieves is that every controller wraps its output in the base template, and that views have valid HTML instead of opening tags in one view and closing in another. If I'd like a specific controller to use a different or no template, I could just override the magic _output() method.

An actual controller would look like this:

class Home extends MY_Controller {

    // Override the title
    public $title = 'Home';

    function __construct()
    {
        // Append a stylesheet (home.css) to the defaults
        $this->styles[] = 'home';
    }

    function index()
    {
        // The output of this view will be wrapped in the base template
        $this->load->view('home');
    }
}

Then I could use its properties in my views like this (this is the 'meta' view that populates the <head> element):

echo "<title>{$this->title}</title>";
foreach ($this->styles as $url)
    echo link_tag("styles/$url.css");

I like my approach because it respects the DRY principle and the header, footer and other elements get included just once in the code.

这篇关于Codeigniter:构建局部视图的最佳方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
PHP最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆