Using the application/views/_layouts/main.php in advanced modules

edited June 2018 in Installation

Is there a way of using the application folder layouts other than repeating code inside an advanced module's layouts / views?

Comments

  • What's the code you are using to create the page?

  • edited June 2018

    Well for example:
    $this->load->module_view(SHOP_FOLDER, 'category', $this->data);

  • I recalled that $this->data = $this->fuel->pagevars->retrieve(uri_segment(1));is one way of getting ANY vars, but the main layout doesn't get applied. Looking back at other advanced module layouts, I have repeated the main layout inside of the module, but this seems un-DRY

  • So looking back at previous advanced modules I've done, and I think sort of like the blog module, I've ended up doing this sort of thing:

    $output = $this->load->module_view(SHOP_FOLDER, '_layouts/products/category', $this->data, TRUE);
                $this->output->set_output($output);
    

    This is necessary because of the HMVC structure?

  • Correct. You could also try the following:

    $vars = array(); // ... add any other $vars to your page outside of the CMS
    $this->fuel->pages->render('my_uri', $vars, array('view_module' => SHOP_FOLDER));
    
  • edited June 2018

    yes - that's much neater, plus handles the layout in the 3rd arg
    array('view_module' => SHOP_FOLDER, 'layout' => 'shop_main')

    Many thanks!

  • I anticipated that

    $this->fuel->pages->render('category', $this->data, array('view_module' => SHOP_FOLDER, 'layout' => 'shop_main'));

    would render the 'category' view inside the 'shop_main' layout from the SHOP_FOLDER module when used from a shop module controller. But in fact the /application/ 'main' layout is used (which isn't named 'shop_main' so I guess that key is ignored). So the 'view_module' key doesn't set the layout to be from the same folder (understandable) but is there a property that would make use of a module layout file?

    I realise I'm now asking the opposite of how the thread began!! I'm fine keeping the layout in the application folder, which is what I wanted, but I'm curious!

    My folder structure in the module is vanilla HMVC:

    application
    -views
    category.php
    --_layouts
    ---shop_main.php

  • I believe the reason why that layout key isn't working is probably because it's not defined as a layout in MY_fuel_layouts.php. In that case, there is a "module" property you can specify in your layout definition as SHOP_FOLDER.

    $config['layouts']['shop_main'] = array(
        'module' => SHOP_FOLDER,
        'fields'    => array(....)
    );
    
  • edited September 2018

    OK, so this is a bit late, but I found a solution!

    In MY_fuel_layouts.php as you suggested:
    /**
    * For shop module
    */
    $config['layouts']['shop_main'] = array(
    'module' => SHOP_FOLDER
    );

    then in the controller:

        $this->page_props = [
            'view_module' => SHOP_FOLDER,
            'layout' => 'shop_main',
            'views_path' => SHOP_PATH.'/views/',
            'render_mode' => 'views'
        ];
    

    ....
    $this->data['layout'] = $this->page_props['layout'];
    $this->fuel->pages->render('categories', $this->data, $this->page_props);

Sign In or Register to comment.