Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
including sub-views while keeping all assigned (local) vars
  • Hi! I am trying to render a part of the page (menu) in a sub-template. Ideally I would like to pass all vars from the main template to sub-template, but not to other templates. Can this be done?
    // in controller, action_index():
    $view = \View::forge('main_template');
    $view->set_global('test_global', 'aaa'); 
    $view->set('test_local', 'bbb');
    $this->template->content = $view;
    
    // in main_template.php:
    <?php  
        $view = \View::forge();
    //    $view->set('test_local', $test_local); // I would like to avoid this
        echo $view->render('menu');
    ?>
    
    // in menu.php:
    echo $test_local.' '.$test_global;
    
    I do not wish to use \View::set_global() because I am using other templates on the same call too (and they do not need the assigned data). Is it possible to assign all variables from the current template to the sub-template? I hope my question makes sense... :)
  • There is a hack possible. in your main template, use
    echo \View::forge('menu')->set($__data, null, false);
    

    This will pass all variables assigned to the main template on the the menu template. Note that this is undocumented and unsupported behaviour as it is based on internal code of the view class. It might change in future versions without notice.
  • Thanks, WanWizard! I guess it'll do for now, until View gets some official methods for deleting / inheriting / retreiving data... :)
  • function action_index()
    {
    // main template
    $this->view = View::forge('layout/main_templete'); // content
    $this->view->content = View::forge('layout/content'); // pass data to only menu
    $this->view->nav = View::forge('layout/menu',$data);
    }
  • That works in your controller, but not if the view itself loads other views...

Howdy, Stranger!

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

In this Discussion