渲染布局codeigniter 4

xyhw6mcr  于 2023-04-09  发布在  其他
关注(0)|答案(1)|浏览(125)

我在布局上挣扎了几天。无法呈现lista视图,也无法访问main.php(主模板)中定义的$settings。任何帮助都是很好的!我的控制器:

public function index()
{
    $_SESSION['userdata']['grupo'] = 'CL';
    return  view('boda');
}

我的模板main.php

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js" integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM" crossorigin="anonymous"></script>
    <title></title>
</head>
<body>
  <?php $settings = new \Config\Ncweb(); ?>
<nav class="navbar navbar-expand-lg bg-light">
  <div class="container-fluid">
    <a class="navbar-brand" href="#">Navbar</a>
    <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <span class="navbar-toggler-icon"></span>
    </button>
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav me-auto mb-2 mb-lg-0">
        <li class="nav-item">
          <a class="nav-link active" aria-current="page" href="#">Home</a>
        </li>
        <li class="nav-item">
          <a class="nav-link" href="#">Link</a>
        </li>
        <li class="nav-item">
          <a class="nav-link disabled">Disabled</a>
        </li>
      </ul>
    </div>
  </div>
</nav>
<div class="container">
  <?= $this->renderSection('templates/lista') ?>
  <?= $this->renderSection('content')?>
</div>
<script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.9.2/dist/umd/popper.min.js" integrity="sha384-IQsoLXl5PILFhosVNubq5LC7Qb9DXgDA9i+tQ8Zj3iwWAwPtgFTxbJ8NT4GN1R8p" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.min.js" integrity="sha384-cVKIPhGWiC2Al4u+LWgxfKTRIcfu0JTxR+EQDz/bgldoEyl4H0zUF0QKbrJ0EcQF" crossorigin="anonymous"></script>
</body>
</html>

我的视图content.php

<?=$this->extend('layouts/main')?>
<?=$this->section('content')?>
this is content
<?=$this->endSection()?>

我的视图lista.php

<?=$this->extend('layouts/main')?>
<?=$this->section('lista')?>
<ul class="list-group">
  <li class="list-group-item">An item</li>
  <li class="list-group-item">A second item</li>
  <li class="list-group-item">A third item</li>
  <li class="list-group-item">A fourth item</li>
  <li class="list-group-item">And a fifth one</li>
</ul>
<?=$this->endSection()?>
jhiyze9q

jhiyze9q1#

您不能像这样呈现lista.php。当您使用布局模板时,您为一个视图文件调用view()方法(在您的情况下为content.phplista.php),它被插入到layout.php模板中。要从布局模板中渲染其他视图,请使用view()view_cell()。或者,您可以合并这两个文件,以便从一个view()-调用呈现它们的内容:
content.php:

<?=$this->extend('layouts/main')?>
<?=$this->section('content')?>
this is content
<?=$this->endSection()?>

<?=$this->section('lista')?>
<ul class="list-group">
  <li class="list-group-item">An item</li>
  <li class="list-group-item">A second item</li>
  <li class="list-group-item">A third item</li>
  <li class="list-group-item">A fourth item</li>
  <li class="list-group-item">And a fifth one</li>
</ul>
<?=$this->endSection()?>

相关问题