Welcome Guest, Not a member yet? Register   Sign In
Reuse HTML Block
#1

[eluser]Unknown[/eluser]
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!
#2

[eluser]BradEstey[/eluser]
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:

Code:
$data = array('color' => 'blue',
                         'type' => 'large');
$this->load->view(‘BlockView’,$data);

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().
#3

[eluser]Rolly1971[/eluser]
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.




Theme © iAndrew 2016 - Forum software by © MyBB