0

I have googled for partial views usage and benefits but I didn't get useful references so thought of posting a question here. Basically I have a list of common html code to place in a common file and call/load that view wherever it included to avoid duplication of code so that whenever if something needs to be changed I will not change in all places I will just modify at common view where I kept my code.

My question here is I can load that common view with 2 types that is from controller I can pass with a $data['variable_name'] and use that with 'variable_name directly in the view or I can simply load that common view within a view like '$this->load->view('common_folder_name/my_partial_view')'

I just want to know which method is the best one to use and benefits of using that method? Also any disadvantages of using that method.

Method:1

My_controller_name:

$data['users_types'] = $this->load->view('common/users_type',NULL,TRUE);

My_view_name:

<?php echo $users_types; ?>

Method:2

My_view_name:

$this->load->view('common/users_type');

Which method is best to use and benefits..! Any help would be appreciated, Thanks.

Prasad Patel
  • 707
  • 3
  • 16
  • 53

1 Answers1

0

Method 1 is useful for ajax responses (if you are returning html) or pre-compiling and storing HTML for use in something like sending an HTML formatted email. You're holding the view in memory until you need it.

Why do that instead of just holding the same information as an html string? In addition to being useful as a templating mechanism (you can pass variables into it), $this->load->view has access to the super-global $this object, all high level config data and the Output class (which can assist with ajax responses, xss cleaning, tracking memory usage and processing time etc).

Method 2 is useful for any direct output of HTML. In a full page HTML, I'll have any number of $this->load->view partials, like header, footer, nav, etc. It's much more readable and flexible in this format than in a Method 1 type variable

Kinglish
  • 23,358
  • 3
  • 22
  • 43