Welcome Guest, Not a member yet? Register   Sign In
variable to layout
#1

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') ?>
Reply
#2

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>
Reply
#3

(This post was last modified: 01-19-2022, 08:56 AM by pippuccio76.)

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 ?

Reply
#4

(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); 
Reply
#5

(This post was last modified: 07-10-2023, 11:45 PM by munaja.)

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.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB