Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
420 views
in Technique[技术] by (71.8m points)

codeigniter - How to create Master Page(Layout) with base design style

I'm new in CodeIgniter. I want to create Master Page or Layout with base style that will be contain Menu, footer and etc. I don't want to write repeating content in all pages and load it automatically for all pages. For example, I can create Master Page in asp.net or Layout in asp.net mvc. I'm sure I can do it in CodeIgniter.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

lets assume you have an html page

<html>
    <head>
        <title> Hello World </title>
    </head>
    <body>
        <div id="menu">
            <li><a href="#">Home</a></li>
            <li><a href="#">About</a></li>
            <li><a href="#">Contact</a></li>
        </div>

        <div id="main-content">
            <!-- this is the dynamic part -->
        </div>

        <div id="footer">
            Copy Right 2013 Hello World
        </div>
    </body>
</html>

you could split it into 1- header 2- menu 3- main content 4- footer

you basically put

<html>
    <head>
        <title> Hello World </title>
    </head>
    <body>

in one view called "view_header" then you put

        <div id="menu">
            <li><a href="#">Home</a></li>
            <li><a href="#">About</a></li>
            <li><a href="#">Contact</a></li>
        </div>
        <div id="main-content">

in a view called "view_menu" and then you put

        </div>

        <div id="footer">
            Copy Right 2013 Hello World
        </div>
    </body>
</html> 

in a view called "view_footer" then in your controller

$this->load->view('view_header');
$this->load->view('view_menu');
$this->load->view('YOUR_VIEW');
$this->load->view('view_footer');

The other solution, which I see is better: create a view called view_template.php

<html>
    <head>
        <title> Hello World </title>
    </head>
    <body>
        <div id="menu">
            <li><a href="#">Home</a></li>
            <li><a href="#">About</a></li>
            <li><a href="#">Contact</a></li>
        </div>

        <div id="main-content">
            <?php $this->load->view($content); ?>
        </div>

        <div id="footer">
            Copy Right 2013 Hello World
        </div>
    </body>
</html>

in the controller lets say you want to call a view called About

$data = array('content'=>'about');
$this->load->view('view_template',$data);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...