在Codeigniter中调用1视图但在所有视图中共享页眉/页脚?

时间:2013-02-26 16:14:50

标签: php codeigniter codeigniter-2 templating

喜欢Codeigniter,但我觉得很烦人,我必须为每个控制器调用页眉,页脚和主视图。

在所有视图中共享页眉/页脚并最小化代码复制的最佳方法是什么。我不想使用模板引擎。

1 个答案:

答案 0 :(得分:2)

创建一个视图,我们用这个代码将其命名为template.php

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

在您的控制器上执行此操作

//the name of the view file you want to show, in my case I want to open the login_page.php view that's inside the folder user
$data['view'] = 'user/login_page';
//load the data variable so I can use it across the views
$this->load->vars($data);
//load the template.php view that will load header, user/login_page and footer
$this->load->view('template');