Welcome Guest, Not a member yet? Register   Sign In
Where place render page code?
#4

First of all, you should read CI manual / documentation and from that understand what you can do / make hand-in-hand with what you need to achieve.
This forum is not meant for teaching you understand programming and developing, or do a work for you, but for solving issues and helping each other.

https://codeigniter4.github.io/CodeIgnit...youts.html
https://codeigniter4.github.io/CodeIgnit...dules.html


Long story short:
Extend controller to some kind of base controller, where base controller always collects all "routine" stuff. Use view layouts with extending and creating sections.


This is how i use CI benefits. This is jsut a basic and totally simple example.

App/Controllers/BaseController.php
PHP Code:
<?php 
namespace App\Controllers;

use 
CodeIgniter\Controller;

class 
BaseController extends Controller
{
 public 
object $data;

 public function 
initController(RequestInterface $requestResponseInterface $responseLoggerInterface $logger)
    {
        parent::initController($request$response$logger);

        $this->initData();
    }

    protected function initData()
    {
    // collect all "routine" code, data, etc...
    $this->data = (object) [
        'layout' => (object) [
        'header_title' => '',
        ]
        'menu' => [
        0 => [
        'title' => 'Contact Us',
        'url' => '/contact-us',
        ],
        1 => [
        'title' => 'About us',
        'url' => '/abour-us',
        ],
        ],
        'misc' => [],
        ];
    }

    public function render(string $name): string
    
{
        return view($name, (array) $this->data);
    }




App/Controllers/MainController.php
PHP Code:
<?php 
namespace App\Controllers;

use 
App\Controllers\BaseController;

class 
MainController extends BaseController
{
 public function 
initController(RequestInterface $requestResponseInterface $responseLoggerInterface $logger)
    {
        parent::initController($request$response$logger);
    }

    public function index(): string
    
{
    $this->data->layout->header_title 'Homepage';

        return $this->render(__FUNCTION__);
    }




App/Views/layout.php
PHP Code:
<!DOCTYPE html>
<
html lang="en" dir="ltr">
 <?=
$this->include('/views/layout_header');?>
 <body>
 <?=$this->renderSection('content');?>
 </body>
 <?=$this->include('/views/layout_footer');?>
</html> 


App/Views/layout_header.php
PHP Code:
<head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
    <title><?=$layout->header_title;?></title>
</head> 


App/Views/layout_footer.php
PHP Code:
<footer>
 <?
php foreach ($menu as $link) { ?>

 <a href="<?=$link['url'];?>">
 <?=$link['title'];?>
 </a>

 <?php ?>
</footer> 



App/Views/index.php
PHP Code:
<?=$this->extend('App\Views\layout');?>

<?=$this->section('content');?>

 <h1>Hello, World!</h1>
<p>This is <?=$layout->header_title;?></p>

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


Messages In This Thread
Where place render page code? - by motoroller - 11-17-2022, 12:51 AM
RE: Where place render page code? - by cb32 - 11-17-2022, 01:18 AM
RE: Where place render page code? - by motoroller - 11-17-2022, 02:52 AM
RE: Where place render page code? - by davis.lasis - 11-17-2022, 05:18 AM
RE: Where place render page code? - by Sprint - 11-17-2022, 05:42 AM



Theme © iAndrew 2016 - Forum software by © MyBB