9

私は CodeIgniter の初心者です。メニュー、フッターなどを含む基本スタイルでマスターページまたはレイアウトを作成したいのですが、すべてのページに繰り返しコンテンツを書き込んで、すべてのページに自動的にロードしたくありません。たとえば、asp.net でマスター ページを作成したり、asp.net mvc でレイアウトを作成したりできます。CodeIgniter でできると思います。

4

4 に答える 4

17

あなたがhtmlページを持っていると仮定しましょう

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

1-ヘッダー 2-メニュー 3-メインコンテンツ 4-フッターに分割できます

あなたは基本的に置く

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

「view_header」と呼ばれる1つのビューに入れます

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

「view_menu」と呼ばれるビューで、次に配置します

        </div>

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

「view_footer」というビューで、次にコントローラーで

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

私が見る他の解決策はより良いです: 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>

コントローラーで、Aboutというビューを呼び出したいとしましょう

$data = array('content'=>'about');
$this->load->view('view_template',$data);
于 2013-11-14T13:11:09.217 に答える
3

動的レイアウト

public $template 変数を使用して新しい Controller を作成すると、拡張された Controller は Master Controller から $template 変数を継承します。

MY_コントローラー

class MY_Controller extends CI_Controller
{
    public $template=null;

    public function __construct()
    {
        if(is_null($this->template)){
            $this->template = 'layouts/default';
        }   
    }
}

管理者コントローラー

class Admin_Controller extends MY_Controller
{
    public function __construct()
    {
        //Still inherits from MY_Controller
        //this time, any controller extending Admin_Controller
        //will point to 'views/layouts/admin'

        if(is_null($this->template)){
            $this->template = 'layouts/admin';
        }   
    }
}

-

class User extends MY_Controller
{
    public function index()
    {
        //$this->template is inherited 
        //from MY_Controller
        //which point to 'views/layouts/default'

        //We can also load a view as data
        //ie no master layout, INTO our master layout
        //note we don't pass $this->template
        //and set the third param as true
        $dynamic_sidebar = $this->load->view('views/sidebar/dynamic', array(
            'data'  =>  'some_data'
        ), true);

        return $this->load->view($this->template, array(
            'partial'   =>  'users/index' //partial view,
            'dynamic_sidebar'   =>  $dynamic_sidebar
        ));
    }
}

ビュー/レイアウト/デフォルト

<body>

    //load the main view
    //in our example we have also loaded
    //a dynamic sidebar with this view
    <?php $this->load->view($partial); ?>
    <?php $this->load->view($dynamic_sidebar); ?>

    //load a static view
    //views/sidebar/static
    <?php $this->load->view('sidebar/static'); ?>

</body>
于 2013-11-14T14:45:13.773 に答える
2

Laravelのマスターページのアイデアに従って、これを行うことができます。

コントローラーコード

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

「show.php」を見る

マスター ページの値を設定し、変数をマスター ページに渡します。マスターページのコードをob_start() & ob_get_clean()内に保管してください。

<?php $page_title = "My first page"; ?>


<?php ob_start(); ?>
Your header stylesheet links and scripts
<?php $page_header = ob_get_clean(); ?>


<?php ob_start(); ?>
<div>
    <h1>This is a Header</h1>

    <?php $this->load->view('Partials/list'); ?>
</div>
<?php $page_content = ob_get_clean(); ?>


<?php ob_start(); ?>
Your footer html or scripts
<?php $page_footer = ob_get_clean(); ?>


<?php $this->load->view('Layout/app',array(
    'page_title' => $page_title,
    'page_header' => $page_header,
    'page_content' => $page_content,
    'page_footer' => $page_footer
)); ?>

部分ビュー「Partials/list.php」

コードを混雑させたくない場合。いくつかの部分的なビューを作成して、物事を単純にすることができます。

<ul>
    <li>LIst item 1</li>
    <li>LIst item 2</li>
    <li>LIst item 3</li>
</ul>

マスターページ「Layout/app.php」

<html>
  <head>
    <title><?= v($page_title) ?></title>
    <?= v($page_header) ?>
  </head>
  <body>
    <?= v($page_content) ?>


    <?= v($page_footer) ?>
  </body>
</html>

<?php
function v(&$var)
{
    return isset($var) ? $var : '';
}

したがって、コードが生成されます。

<html>
    <head>
        <title>My first page</title>
        Your header stylesheet links and scripts
    </head>
    <body>

    <div>
        <h1>This is a Header</h1>

        <ul>
            <li>LIst item 1</li>
            <li>LIst item 2</li>
            <li>LIst item 3</li>
        </ul>
    </div>


    Your footer html or scripts
  </body>
</html>
于 2016-04-06T06:04:00.067 に答える
0

おそらく、すべてのページに共通する、ヘッダー、メニュー、フッターなどの個別のビュー ファイルです。そして、それらを各ビューに含めます。お気に入り

$this->view('header');
$this->view('menu');
//Some specific content
$this->view('footer');

上記をすべてのビューにコピーせずに同じ機能が必要な場合は、次のようにコントローラーに関数を作成する必要があります。

private function myviewfunction($current_view) 
{
    $this->load->view('header');
    $this->load->view('menu');
    $this->load->view($current_view);
    $this->load->view('footer');
    return NULL;
}

すべてのページ(メソッド)でこの関数を呼び出します

$this->myviewfunction('about'); //About is the specific view for the method
于 2013-11-14T12:05:54.340 に答える