• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Reuse HTML Block

Hi Guys,

I am thinking how to achieve below:

I create HTML Block and want to reuse it in different locations within one page.
For example, I create one small category list basic different types, And what I plan is:
1, to create the HTML Block to be one view file.
2, fetch the data by types, and assign the data to the Block view and shows in main page.(I load the Block view in main page view.)
3, back to step 2, load one more again to create one more category list in the same page.

I already googled it and some guys said can ues "$string = $this->load->view('BlockView',$data,true)" to get the html string and then resend to other view.
But I found, the $data will become undefined in the BlockView file. Any one came across this issue?

Or, any one can share his way to reuse the HTML Block? Thanks in advance!

Whenever you pass an array into a view, the extract() function is run on the array. So for example if you include a view using:

$data = array('color' => 'blue',
                         'type' => 'large');

The in BlockView.php you would call these as variables $color and $type.

It's also worth noting that you can include $this->load->view(‘BlockView’); directly in your main view and all of the data that you passed to the parent view will automatically be accessible from your BlockView view. Just like an include().

you could also incorporate a 'widget' library into your CI. There are a couple good ones around.

but like Bradillac said, you can using the $this->load->view() function call in your views if that will make it easier for you to re-use certain blocks.

but i would recommend looking up a Widget library.

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

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