Professional CodeIgniter

(singke) #1

Chapter 4: Creating the Main Web Site


85


$data[‘sidef’] = $this- > MProducts- > getRandomProducts(3,$skip);
$data[‘main’] = ‘home’;
$this- > load- > vars($data);
$this- > load- > view(‘template’);
}

Running a print_r() on the data returned by the getMainFeature() function, you ’ ll see something
along the lines of the following little snippet:

[id] = > 6
[name] = > Shoes 1
[shortdesc] = > This is a very good pair of shoes.
[image] = > /images/dummy-main.jpg

Similarly, the data returned by the getRandomProducts() function would look like this via the
print_r() function:

[0] = > Array
(
[id] = > 7
[name] = > Shoes 2
[thumbnail] = > /images/dummy-thumb.jpg
)

[1] = > Array
(
[id] = > 8
[name] = > Shirt 1
[thumbnail] = > /images/dummy-thumb.jpg
)

[2] = > Array
(
[id] = > 12
[name] = > Pants 1
[thumbnail] = > /images/dummy-thumb.jpg
)

Setting $data[ ’ main ’ ] to home instructs the $this - > load - > view() command inside template.php to
load the view called home. That ’ s what you ’ ll need to create next.

Creating the Home Page View


Now all you have to do is create some HTML based on these data. Your initial thought might be to put
this logic directly in the main template view, but this is a bad idea. Why? Because you ’ re going to be
reusing the template view a lot, and if you have to maintain an ever - growing list of rules to govern the
display of the $main variable, you ’ re going to drive yourself (and everyone else) crazy trying to
maintain it.

Instead, you ’ re going to create a series of included templates, each dedicated to a different view. There
are other ways to handle the problem, of course, including doing all this work in the models, creating a
custom library, or using private functions, but the view approach is the purest when it comes to the MVC
division of labor.
Free download pdf