• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
CI4 beta.1 - How to load view in view with layout

#1
Hello,

In beta 1, how can I load a partial view in a view with layout?

When I use this code, nothing is rendered. 
PHP Code:
<?= $this->extend('layout'?>

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


But without layout, it will work.
PHP Code:
<?= view('partial'); ?>
<h1>Hello World!</h1> 
Reply

#2
(03-08-2019, 02:40 AM)pinebranch Wrote: Hello,

In beta 1, how can I load a partial view in a view with layout?

When I use this code, nothing is rendered. 
PHP Code:
<?= $this->extend('layout'?>

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


But without layout, it will work.
PHP Code:
<?= view('partial'); ?>
<h1>Hello World!</h1> 

His error report seems to be similar to that found by me:
https://forum.codeigniter.com/thread-72967.html
Reply

#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

#4
Thanks ! just what i was searching for Smile
Reply

#5
You should never ever edit or add to a CodeIgniter system class file!

You need to extend the class in question.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply

#6
A new method, include() has been added to the View system that can be used just like renderPartial above.
Support Development  • Practical CodeIgniter 3  • Vulcan - CLI Tools for CI4
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


Users browsing this thread:
2 Guest(s)


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.