Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
public Template and passing $data
  • Hi,
    I am new to FuelPHP and I am following some tutorials.
    I am doing this:

    <?php

    class Controller_Admin extends Controller_Base
    {
        public $template = 'layouts/template';

    public function action_index()
        {
            $data["subnav"] = array('index'=> 'active' );
            $this->template->title = 'Dashboard';
            $this->template->content = View::forge('admin/dashboard', $data);
        }
    //... etc

    And the template contains:

    <?php echo View::forge('layouts/partials/sidebar',array('subnav'=>$subnav)); ?>
    I need to pass $subnav to the sidebar menu.

    I get error "Fuel\Core\PhpErrorException [ Notice ]: Undefined variable: subnav"

    I am following some tutorials but I can't find what I am doing wrong.
    Controller_Base extends from Controller_Template.
    I have read many times already the docs here:
    http://fuelphp.com/docs/general/controllers/template.html
    and at the part that says "Changing the default template file" it has more or less the same that I have (except for variable initialization)

    class Controller_Example extends Controller_Template
    {

    public $template = 'template_admin';

    public function action_index()
    {
    $this->template->title = 'Example Page';
    $this->template->content = View::forge('test/index', $data);
    }
    }



    Any hint? I can't move on.
    Thanks a lot. Regards
  • HarroHarro
    Accepted Answer
    View data is not global, it's local to the View object you pass it to.

    You pass the data to the "admin/dashboard" view, so the $subnav variable is only available in that view. If you need it in the template, pass it to the template:

    $this->template->subnav = array('index' => 'active');

    Alternatively you can use set_global(), but that is not best practice and should be avoided if possible.
  • Thank you so much!!
    This is exactly what I needed!!

    Thank you!!!

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion