CodeIgniter 入门实践(博客站-2:多视图)

CodeIgniter 入门实践(博客站-2:多视图)


DATE: 2017-10-12 12:59:16

CodeIgniter 入门实践(博客站-1:单视图主页)
前文中已经有了博客站的单个主页开发过程,接下来进阶开发博客站的视图。

控制器中,加载多个视图

/application/controllers/Blog.php

    public function index()
    {
        $data['title'] = "My Real Title";
        $data['heading'] = "My Real Heading";
        $this->load->view('blogview', $data);
    }

视图文件准备

/application/views/blogview.php

<html>
<head>
<title><?php echo $title;?></title>
</head>
<body>
<h1><?php echo $heading;?></h1>
</body>
</html>

效果
这里写图片描述

进阶,动态加载多视图

/application/controllers/Blog.php

    public function index()
    {
        $data['todo_list'] = array('Clean House', 'Call Mom', 'Run Errands');
        $data['title'] = "My Real Title";
        $data['heading'] = "My Real Heading";
        $this->load->view('blogview', $data);        
    }

/application/views/blogview.php

<html>
<head>
    <title><?php echo $title;?></title>
</head>
<body>
    <h1><?php echo $heading;?></h1>

    <h3>My Todo List</h3>
    
    <ul>
    <?php foreach ($todo_list as $item):?>
    
        <li><?php echo $item;?></li>
    
    <?php endforeach;?>
    </ul>

</body>
</html>

效果
这里写图片描述

至此,多个视图成功调用。

版权声明

弈心博客


本文首发site_name,转载请附上博文链接!