• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Do this code for most functions

First, it should be a standalone library (/application/libraries/yourcode.php), not "at the top of your viewfile".

Second, you would load your library in the controller or autoload it, not manually, using CI's load method: $this->load->library('yourcode');

Third, you would access your example like:
$test = $this->yourcode->returnTrue();

I think you are missing the point of doing this in a library. The library should do everything for you, including creating the header/footer/menu/etc, along with showing the appropriate view for if they are logged in or not.

Lets say you had a controller for a shopping cart. In the shopping cart controller, you are creating a main content view that shows the products and prices for a category. In that controller, you only create the view that is for displaying the products and send that to the library, where the library will add the header, footer, menu, etc.

Personally, I autoload my "template" library.

Then in my controller, I do what is needed to create the "current view".
function index()
  //load the model to get products

  //set the data that the view will need to display the products
  $data['products'] = $this->product_model->get_products();

  //Now load the view into a variable (don't output immediately) by setting the 3rd parameter to TRUE
  $view = $this->load->view('display_product_view', $data, TRUE);

  //now send this view to your template library where it will add the header/footer/etc.
  $this->template_library->create_page('this is my page title', $view);

Template library:
public function create_page($page_title, $page_view)
  //set the page title that goes with the header view
  $header_data['page_title'] = $page_title;
  //Check whether user is logged in and get appropriate view
  if ($this->is_logged_in())
    $header_data['user'] = $this->CI->load->view('logged_in_view', null, TRUE);
    $header_data['user'] = $this->CI->load->view('not_logged_in_view', null, TRUE);
  //assemble the page title along with login view
  $header = $this->CI->load->view('header', $header_data, TRUE);

  //Get the footer
  $footer = $this->CI->load->view('footer', null, TRUE);

  //set the final output (header, content and footer).  Use CI::output() so we can use caching
  $this->CI->output->set_output($header . $page_view . $footer);

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.