Welcome Guest, Not a member yet? Register   Sign In
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, 01: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.
Reply
#7

(03-13-2019, 09:03 PM)kilishan Wrote: A new method, include() has been added to the View system that can be used just like renderPartial above.


Hello, i'm having issues with this method it reads the file but encounters a problem

Code:
CRITICAL - 2020-03-25 23:45:57 --> Invalid file: ci4_footer.php
#0 C:\AppServ\www\ferv\PTP\vendor\codeigniter4\framework\system\View\View.php(224): CodeIgniter\Exceptions\FrameworkException::forInvalidFile('ci4_footer.php')
#1 C:\AppServ\www\ferv\PTP\vendor\codeigniter4\framework\system\View\View.php(503): CodeIgniter\View\View->render('ci4_footer', NULL, true)
#2 C:\AppServ\www\ferv\PTP\app\Views\CR\Ini\inicioSesion\inicioSesion.php(43): CodeIgniter\View\View->include('ci4_footer')
#3 C:\AppServ\www\ferv\PTP\vendor\codeigniter4\framework\system\View\View.php(236): include('C:\\AppServ\\www\\...')
#4 C:\AppServ\www\ferv\PTP\vendor\codeigniter4\framework\system\Common.php(1071): CodeIgniter\View\View->render('cr/ini/inicioSe...', Array, true)
#5 C:\AppServ\www\ferv\PTP\app\Controllers\Ini.php(9): view('cr/ini/inicioSe...')
#6 C:\AppServ\www\ferv\PTP\vendor\codeigniter4\framework\system\CodeIgniter.php(910): App\Controllers\Ini->inicioSesion()
#7 C:\AppServ\www\ferv\PTP\vendor\codeigniter4\framework\system\CodeIgniter.php(398): CodeIgniter\CodeIgniter->runController(Object(App\Controllers\Ini))
#8 C:\AppServ\www\ferv\PTP\vendor\codeigniter4\framework\system\CodeIgniter.php(306): CodeIgniter\CodeIgniter->handleRequest(NULL, Object(Config\Cache), false)
#9 C:\AppServ\www\ferv\PTP\public\index.php(45): CodeIgniter\CodeIgniter->run()
#10 {main}

and the file "ci4_footer.php" contains
Code:
    <div class="environment">

            <p>Page rendered in {elapsed_time} seconds</p>

            <p>Environment: <?= ENVIRONMENT ?></p>

    </div>


in the view
Code:
<footer>
    <?php $this->include('ci4_footer'); ?>
</footer>

it's pretty simple, but i don't know where i'm making the error, any recommendation?

best regards
Reply
#8

(03-13-2019, 09:03 PM)kilishan Wrote: A new method, include() has been added to the View system that can be used just like renderPartial above.

When I use this method like this:
PHP Code:
<?php $data["name"] = ""?>
<?= $this
->include("includes/modal",$data); ?>

In Views/includes/modal.php I do:

PHP Code:
<?php echo $name;?>


Then I get an error:

ErrorException

Undefined variable: name

How do we put variables for just the php partial page I am including, I dont want to define the partial page variables in the controller, I need to define them in the template as its building out the page html in the view
Reply
#9

Then create a Library and then use the View Cell Method.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply




Theme © iAndrew 2016 - Forum software by © MyBB