Welcome Guest, Not a member yet? Register   Sign In
How to call controller from view without form submission ?
#11

(This post was last modified: 09-22-2017, 11:15 AM by PaulD. Edit Reason: Spacing in code )

Quote:Thanks for reply. Is there any way to do like i have my leftsidebar_contolle,leftsidebar _model and leftsidebar _view so i can load them once and use in application. like other controller,model,views we use.

This does not feel right but you could do it with MVC but I think that would be overkill.

If I were doing this, I would create a template library. The library could call by default your model methods for filling in whatever data you need for the left, right, top, bottom or whatever nav bars or blocks you wanted. Then render the page appropriately using the default views needed to construct the page, and then render whatever view you actually need for the page. Using setters you can set the page view in your controller then render the page by calling the relevant method in the library. If you ever need to change the sidebar or topbar you just change your template library. Once it is all working though, your controller just collects the page data you need, sets the view you want and calls the template render function. It can be as complex or simple as you want.

Here is a very simple example from a simple site I built needing data in a similar way,

Template_library.php
PHP Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class 
Template_library {
 
  
// set some page_data defaults
  
public $page_data = array();
 
  public function 
__construct()
  {
    
$this->CI =& get_instance();
  }

  public function 
render_page($template)
  {
    
// render a page
 
    // get books for header nav
    
$this->page_data['books'] = $this->CI->book_model->get_books();
 
    
// get data types for page usage
    
$this->page_data['data_types'] = $this->CI->data_model->get_data_types();
 
    
// render view
    
$this->CI->load->view('common/member_header_view'$this->page_data);
    
$this->CI->load->view($template);
    
$this->CI->load->view('common/member_footer_view');
  }
 
  public function 
set_page_data($name$data NULL
  {
    
$this->page_data[$name] = $data;
    return;
  }
 


And associated controller method just for example usage
PHP Code:
<?php defined('BASEPATH') OR exit('No direct script access allowed');

class 
Dashboard extends CI_Controller {
 
    public function 
index()
    {
        
// setting template data
        
$this->template_library->set_page_data('page_title''Dashboard');

        
// breadcrumb
        
$breadcrumb = array(
            array(
'link' => 'dashboard''link_text' => 'Home''link_active' => TRUE),
        );
        
$this->template_library->set_page_data('breadcrumb'$breadcrumb);

        
// getting some data for the page
        
$this->template_library->set_page_data('books_with_counts'$this->book_model->count_book_pages());

        
// render page
        
$this->template_library->render_page('dashboard_view');
    }



Edited down for example. Please don't mock poor coding :-)

Hope that helps,

Paul.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB