Welcome Guest, Not a member yet? Register   Sign In
CI4 beta.1 - How to load view in view with layout
#3

I solved this by extending the core View class, added a function to render partial view. 

Function renderPartial is combined from function render and renderString of the View class. I also put additional data for partial view in $options parameter.

PHP Code:
<?php namespace App\Core;

use 
CodeIgniter\View\View;

class 
MyView extends View {

 
   public function renderPartial(string $view, array $options null$saveData null): string
    
{
        
$start microtime(true);
        if (
is_null($saveData))
        {
            
$saveData $this->config->saveData;
        }

        
$fileExt                     pathinfo($viewPATHINFO_EXTENSION);
        
$realPath                    = empty($fileExt) ? $view '.php' $view// allow Views as .html, .tpl, etc (from CI3)
        
$this->renderVars['view'   $realPath;
        
$this->renderVars['options'] = $options;

        
// Was it cached?
        
if (isset($this->renderVars['options']['cache']))
        {
            
$this->renderVars['cacheName'] = $this->renderVars['options']['cache_name'] ?? str_replace('.php'''$this->renderVars['view']);

            if (
$output cache($this->renderVars['cacheName']))
            {
                
$this->logPerformance($this->renderVars['start'], microtime(true), $this->renderVars['view']);
                return 
$output;
            }
        }

        
$this->renderVars['file'] = $this->viewPath $this->renderVars['view'];

        if (! 
is_file($this->renderVars['file']))
        {
            
$this->renderVars['file'] = $this->loader->locateFile($this->renderVars['view'], 'Views', empty($fileExt) ? 'php' $fileExt);
        }

        if (! 
is_null($options)) {
            
$this->setData($options);
        }
            
        
extract($this->data);

        if (! 
$saveData)
        {
            
$this->data = [];
        }

        
ob_start();
        include(
$this->renderVars['file']);
        
$output ob_get_contents();
        @
ob_end_clean();

        
$this->logPerformance($startmicrotime(true), $this->excerpt($view));

        return 
$output;
    }



In app\Config\Services.php, add a function to override renderer service using MyView
PHP Code:
public static function renderer($viewPath null$config nullbool $getShared false)
    {
        if (
$getShared)
        {
            return static::
getSharedInstance('renderer'$viewPath$config);
        }

        if (
is_null($config))
        {
            
$config = new \Config\View();
        }

        if (
is_null($viewPath))
        {
            
$paths config('Paths');

            
$viewPath $paths->viewDirectory;
        }

        return new \
App\Core\MyView($config$viewPath, static::locator(true), CI_DEBUG, static::logger(true));
    } 

Then in a view with layout, you can call a partial view like this


PHP Code:
<?= $this->extend('layout'?>

<?= $this->section('content'?>
    <?= $this->renderPartial('partial'); ?>
    <h1>Hello World!</h1>
<?= $this->endSection() ?>
Reply


Messages In This Thread
RE: CI4 beta.1 - How to load view in view with layout - by pinebranch - 03-10-2019, 01:50 AM



Theme © iAndrew 2016 - Forum software by © MyBB