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

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

问题描述

如何构建Codeigniter中的以下页面?





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


  1. 左导航

  2. 内容导航

  3. 登录名称

  4. 排行榜

排除内容部分(根据左侧导航栏和内容导航栏中的链接进行更改,用作一个子菜单)。

 <$ c $ 

c> 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_pa​​ge'=>'blah'));

$ this-> load('User');

$ 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()



这可能是一个头痛,在所有的左导航链接,如新闻,用户,关于我们等等这个代码。但然后再次不是每个导航链接都有所有这些部分,所以我需要灵活性



$ b < >解决方案

我不能证明这是最好的方法,但我创建一个基本控制器如下:

 code> class MY_Controller extends CI_Controller {

public $ title ='';
//模板将使用它来包含default.css默认
public $ styles = array('default');

函数_output($ content)
{
//加载输出内容可用的基本模板$ content
$ data ['content'] =& $ content;
$ this-> load-> view('base',$ data);
}

}

一个模板(包含其他视图的视图):

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

这实现的是每个控制器将其输出包装在基本模板中,并且视图具有有效的HTML而不是在一个视图中打开标签并在另一个视图中关闭。如果我想要一个特定的控制器使用不同的或没有模板,我可以重写魔术 _output()方法。



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

  class Home extends MY_Controller {

//覆盖标题
public $ title ='Home';

函数__construct()
{
//将样式表(home.css)附加到默认值
$ this-> styles [] ='home' ;
}

function index()
{
//此视图的输出将包含在基本模板中
$ this-> load - > view('home');
}
}

然后我可以在我的视图中使用它的属性(这是填充< head> 元素的meta视图):

 code> echo< title> {$ this-> title}< / 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屋!

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