需要帮助在codeigniter中配置CSS模板

时间:2011-04-26 18:18:44

标签: php css codeigniter


我在codeigniter中只配置css模板时遇到问题。例如:我是codeigniter的初学者。任何人都可以告诉我如何配置它或任何教程。 希望你能理解我糟糕的英语..

这是我的代码

在视图页面home.php

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



     <div id="page">
        <div id="page-bgtop">
            <div id="page-bgbtm">
                <div id="content">
                    <div class="post">
                        <h2 class="title"><a href="#">Home</a></h2><br>

                        <div class="entry">
                            <p><img src="<?=site_url();?>images/img08.jpg" width="538" height="200" alt="" /></p>
                            <p>Sylhet Engineering College (SEC), established in the year 2007 under the School of Applied Science and Technology, Shahjalal University of Science and Technology is best of its kind with a motto to produce the best in class engineers for the 21st century in Bangladesh. There are five universities of Engineering and Technology and some private universities in the country for providing engineering education at B.Sc level which is not sufficient to meet the requirement of today's fast paced engineering sector of Bangladesh. As a divisional city of Bangladesh, Sylhet had no engineering institute. So the Government of Peoples Republic of Bangladesh has established Sylhet Engineering College with a vision to expand the engineering education of Bangladesh as an engineering faculty section of Shahjalal University of Science and Technology.</p>

                        </div>
                    </div>



                    <div class="post">


                    </div>
                    <div style="clear: both;">&nbsp;</div>
                </div>
                <!-- end #content -->


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


在控制器页面home.php

<?php

class Home extends Controller {

    function Home()
    {
        parent::Controller();   

    }

    function index()
    {

        $this->load->view('home');


    }
}

提示:

header_final.phpfooter-final.php

2 个答案:

答案 0 :(得分:3)

CodeIgniter允许您同时加载多个视图。您希望将视图加载到控制器中。所以你想要做的是在控制器中加载标题视图,然后加载你想要渲染的实际视图,然后加载页脚视图。完成后,它将看起来像这样:

function index(){
    $this->load->view('header_final');
    $this->load->view('home');
    $this->load->view('footer_final');
}

然后从home.php视图中删除所有视图加载。

答案 1 :(得分:0)

执行此操作的正确方法是使用控制器来操纵您的视图。您的默认控制器在“system / app / config / routes.php”文件中指定。参数“default_controller”告诉应用程序默认使用哪个控制器。

正如Carl所说,一旦你放置了所有代码行,你需要加载必要的视图,你可以将default_controller指向这个控制器。请注意,函数索引是控制器使用的默认函数。

一旦你继续,你可能需要构建一个自定义控制器,因为你的所有页面都需要这个页眉和页脚。我是通过在“system / app / core / MY_Controller.php”下添加一个文件来实现的(Code igniter 2.X,对于不同的版本不一样),我改为扩展了这个控制器。此控制器中的代码如下所示:

    <?php

 class MY_Controller extends CI_Controller {

    function __construct() {
        parent::__construct();
    }
    public function loadHeader($data = Array(),$customHeader = 'parts/header'){
        $this->load->view($customHeader,$data);
    }
    public function loadFooter($data = Array(),$customFooter = 'parts/footer'){
        $this->load->view($customFooter,$data);
    }
    public function loadSidebar($data = Array(),$customSidebar = 'parts/sidebar'){
        $this->load->view($customSidebar,$data);
    }
}

然后从我的控制器我做这样的事情:

    <?php

class Articles extends MY_Controller {
    public function  __construct() {
        parent::__construct();
    }
    /**
     * Controller for all news.
     * will be used as home controller.
     */
    public function index() {
        //Needed helper

        //End helper
        $data = Array();
        //Fill the array for the header part
        $data["header"] = Array();
        $data["header"]["title"] = "Articles shelf";
        $data["header"]["logo"] = "David Francoeur";
        $data["header"]["slogan"] = "just a blog about me and knowledge...";
        $this->loadHeader($data["header"]);// I would add a second parameter for a custom header.
        //Fill the array for the rest of the pages
        $data["main"] = Array();
        $this->load->model('Article');
        $data["main"]["query"] = $this->Article->get_last_ten_entries();
        $this->load->view('article', $data["main"]);
        //Fill the array for the right sidebar
        $data["sidebar"] = Array();
        $this->loadSidebar($data["sidebar"]);
        //Fill the array for data concerning the footer
        $data["footer"] = Array();
        $this->loadFooter($data["footer"]);//I would add a second parameter for a custom footer.
    }

}

如果您不需要自定义页眉或页脚,您也可以在自定义控制器中使用单个函数:

<?php

类MY_Controller扩展了CI_Controller {

function __construct() {
    parent::__construct();
}
public function loadView($data = Array(),$view){
    $this->load->view("header_final");
    $this->load->view($view,$data);
    $this->load->view("footer-final");
}
}

这取决于你!