CodeIgniter Forums
variable to layout - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: variable to layout (/showthread.php?tid=81046)



variable to layout - pippuccio76 - 01-18-2022

hi how can i send variable to footer in layout ( with <?= $this->renderSection('content') ?> ) ? variable set to every view that extend a template  : <?= $this->extend('templates/layout_user') ?>


RE: variable to layout - BilltheCat - 01-18-2022

You can set the variable from your controller, and then show the variable through a footer include:

app/Controllers/Cart.php
PHP Code:
<?php
namespace App\Controllers;
class 
Cart extends BaseController
{

    public function index()
    {
        $data['variable'] = "Footer variable";

        echo view('Cart/index'$data);
    }


app/Views/templates/layout_user.php
PHP Code:
<?= $this->renderSection('content'?>

<?= $this->include('\App\Views\templates\footer'?>

app/Views/cart/index.php
PHP Code:
<?= $this->extend('App\Views\templates\layout_user'?>
<?= $this
->section('content'); ?> 
<p>Some cart content here</p>
<?= $this->endSection(); ?>



app/Views/templates/footer.php
PHP Code:
<p><?= $variable?></p> 


Display:
Code:
<p>Some cart content here</p>
<p>Footer variable</p>



RE: variable to layout - pippuccio76 - 01-19-2022

The footer is used from several page of my project , i cannot set variable in every method of every controller that show the template . I need a  global variable (data from db ) to set user contact (email , telephone ecc.) in footer . It's possible to do ?




RE: variable to layout - BilltheCat - 01-19-2022

(01-19-2022, 08:56 AM)pippuccio76 Wrote: The footer is used from several page of my project , i cannot set variable in every method of every controller that show the template . I need a  global variable (data from db ) to set user contact (email , telephone ecc.) in footer . It's possible to do ?


I like to set in my base controller
PHP Code:
$this->data['variable'] = "variable info"

And then in each of my app controllers I just call the whole thing

PHP Code:
echo view('Cart/index'$this->data); 



RE: variable to layout - munaja - 07-10-2023

I am facing the similar issue, and unfortunately there is no documentation regarding the issue (should be in view-layout related section in the documentation) which I presume there is no feature about it in the current version of CI. I ended up utilizing the layout as simple as possible with very little needs of logic, and utilizing other view's features, like loading multiple views, cell, etc. Flexible, but less structured. Lets hope CI team adds the feature in the future.
------------
EDIT:
I think made a mistake by assuming that there is no feature about it because there is no documentation about it. It seems we don't need to pass any data to the layout since it can detect all data passed from controller to view.