【问题标题】:Layout.php is not showing the header.php partLayout.php 未显示 header.php 部分
【发布时间】:2015-10-28 10:11:15
【问题描述】:

我有如下的 HTML。

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <!-- Meta, title, CSS, favicons, etc. -->
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
    </head>
    <body style="background:#F7F7F7;">
        <div class="">
        </div>
    </body>
</html>

我把这个 html 分成两个 PHP 文件。所以,有Header.php。如下所示。

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <!-- Meta, title, CSS, favicons, etc. -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
</head>

Layout.php

<!DOCTYPE html>
<html lang="en">

    <? include 'header.php';?>

    <body style="background:#F7F7F7;">
        <div class="">
        </div>
    </body>
</html>

问题

Layout.php 没有显示 header.php 部分。

【问题讨论】:

标签: php codeigniter codeigniter-2 codeigniter-3


【解决方案1】:

我认为问题出在短标签上,

<?php include 'header.php';?>

但我想说,在使用 codeigniter 时,

$this->load->view("header.php");

【讨论】:

    【解决方案2】:

    把这一行改成

    <?php include 'header.php';?>
    

    【讨论】:

      【解决方案3】:

      我发现加载页眉和页脚最好的方法是创建一个views/template_view.php,您可以在其中加载页眉和页脚,这样您就不必每次都加载它。

      views/template_view.php

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

      在控制器上,您所要做的就是

      public function index() {
         // Would be the name of the view you would like for this function / controller
         $data['page'] = 'somefile_view'; 
         $this->load->view('template_view', $data);
      }
      

      【讨论】:

        【解决方案4】:

        使用这个

        $path = APPPATH.'views/header.php';
        <?php include $path; ?>
        

        Codeigniter 可以使用APPPATH 调用路径。其 CI 预定义变量

        【讨论】:

          【解决方案5】:

          检查您的 php 配置是否有 short_open_tag = On 以及您的文件位置路径。

          你也可以试试这个:

          但对于 codeigniter,最好这样使用:$this->load->view('header.php');

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2015-08-14
            • 2018-03-28
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2015-02-20
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多