Welcome Guest, Not a member yet? Register   Sign In
Create a Layout-Template
#1

(This post was last modified: 12-09-2024, 02:07 PM by 4usol.)

Hi,

i try to create a layout-template. For that i start with this:

add a template function to the base-controller
Code:
public function template(string $page, array $data)
    {
        //pass this datas to all template files
        $data['view_page']= view($page, $data);
        $data['view_data']= $data;

        //go and create it
        echo view('templates/v1/create_page', $data);
    }


in the central template view "create_page" i want to add/include more additional files, for example 1 file for the header, 1 file for sidenav etc. So i trie this:
Code:
<?= view('templates/v1/view_file_header') ?>
<?= view('templates/v1/view_file_sidebar') ?>
Code:
<div id="header" class="app-header">here content from view_file_header<?= $this->renderSection('header') ?></div>
<div id="header" class="app-header">here content from view_file_sidebar<?= $this->renderSection('sidebar') ?></div>


In my "view_file_header"

Code:
<?= $this->extend('templates/v1/create_page') ?>

<?= $this->section('header') ?>
Put this text from sidebar file
<?= $this->endSection() ?>



In my "view_file_sidebar"

Code:
<?= $this->extend('templates/v1/create_page') ?>

<?= $this->section('sidebar') ?>
Put this text from sidebar file
<?= $this->endSection() ?>



At the end, it doesnt work for me. I recieved a error 500.

Changing the from "view('templates/v1/view_file_sidebar') " to "$this->include('templates/v1/view_file_sidebar')" makes no difference.

What i do wrong?
Reply
#2

In my opinion, the template does not work like that. It is necessary to expand "Create_Page" once and create sections for it. There should be no other "extend" inside
Simple CI 4 project for beginners codeigniter-expenses ( topic )
Reply
#3

My central page looks like this:
Code:
<body class="hold-transition sidebar-mini">
<div class="wrapper"><!-- wrapper -->
<?= $this->include('Modules\Views\Admin\navigation') ?>
<?= $this->include('Modules\Views\Admin\sideNavigation') ?>
<div class="content-wrapper"><!-- /.content-wrapper -->
<?= view('Modules\Views\Admin\Header') ?>
<?= $this->renderSection('content') ?>
</div><!-- /.content-wrapper -->
<?= $this->include('Modules\Views\Admin\footer') ?>
</div><!-- ./wrapper -->
</body>

Then, any sub-page uses this format:
Code:
<?= $this->extend('Modules\Views\Admin\page') ?>
<?= $this->section('content') ?>

<p>All your code here</p>

<?= $this->endSection() ?>

This might not be perfect, but it works for me.
Reply
#4

ok,i test a bit -by the way is there a way (or to need), to pass the ($DATA) data-variable from the controller for the main view (central layout page) in my example the "create_page(path,$DATA) to the other sub-views by inlcuding or if i try to use the way with " $this->extend('templates/v1/create_page') " ?
Reply
#5

I usually pass $data in a sub-page, including data for top and side navigation like this:
PHP Code:
echo view('Modules\Admin\Departments\Views\index'$data); 
Reply
#6

(This post was last modified: 12-11-2024, 11:21 AM by 4usol.)

In this case i think the $data from controller need also a $data['data']=this is for subpage, isnt it?

(12-10-2024, 01:34 AM)JustJohnQ Wrote: My central page looks like this:
Code:
<body class="hold-transition sidebar-mini">
<div class="wrapper"><!-- wrapper -->
<?= $this->include('Modules\Views\Admin\navigation') ?>
<?= $this->include('Modules\Views\Admin\sideNavigation') ?>
<div class="content-wrapper"><!-- /.content-wrapper -->
<?= view('Modules\Views\Admin\Header') ?>
<?= $this->renderSection('content') ?>
</div><!-- /.content-wrapper -->
<?= $this->include('Modules\Views\Admin\footer') ?>
</div><!-- ./wrapper -->
</body>

Then, any sub-page uses this format:
Code:
<?= $this->extend('Modules\Views\Admin\page') ?>
<?= $this->section('content') ?>

<p>All your code here</p>

<?= $this->endSection() ?>

This might not be perfect, but it works for me.

If i use: "<?= $this->extend('templates/v1/page_content') ?>" in my subpage i recieved a error 500. I dont understand why?

If i dont use the "extend" command, all works fine - i'm confused...
Reply
#7

(This post was last modified: 12-12-2024, 01:43 AM by JustJohnQ.)

I think you are making it more complicated than necessary, pleasae look at these guidelines in the documentation:
https://codeigniter4.github.io/userguide...youts.html
Start with a basic setup, and work from there. I am never calling
Code:
Modules\Views\Admin\page
from any controller.

In a fresh CI 4.51 installation, I created 3 files in the App\Views folder:
create_page.php
Code:
<!DOCTYPE html>
<html lang="en">
<head>
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Main page</title>
    <!-- bootstrap & fontawesome -->
    <link rel="stylesheet" href="<?php echo base_url("Resources/bootstrap/css/bootstrap.css");?>" />
    <!-- jQuery -->
    <script src="<?php echo base_url("Resources/plugins/jquery/jquery.js");?>"></script>
    <!-- Bootstrap 4 -->
    <script src="<?php echo base_url("Resources/plugins/bootstrap/js/bootstrap.bundle.js");?>"></script>
</head>
<body>
<div class="wrapper">
    <?= $this->include('Views\header') ?>
    <div class="content-wrapper">
        <div class="content">
            <?= $this->renderSection('content') ?>
        </div>
    </div>
</div><!-- ./wrapper -->
</body>
</html>

header.php
Code:
<section class="content-header">
    <div class="container-fluid">
    </div>
    <div class="row">
        <div class="col-sm-12">
            <h4>This is the header</h4>
        </div>
    </div>
    </div><!-- /.container-fluid -->
</section>

home.php
Code:
<?= $this->extend('Views\create_page') ?>
<?= $this->section('content') ?>

<section class="content">
    <div class="container-fluid">
        <h4>Content goes here</h4>
    </div>
</section>

<?= $this->endSection() ?>

I changed the line with welcome_message in Home controller to:
PHP Code:
return view('home'); 

The result is:
[Image: home-page.png]
Reply




Theme © iAndrew 2016 - Forum software by © MyBB