Welcome Guest, Not a member yet? Register   Sign In
Content - templates
#1

[eluser]CinoGenX[/eluser]
Hi @ All

Can someone clarify something simple for me please.

If im building my pages via a controller with something like:

$this->load->view("header")
$this->load->view("content",$data)
$this->load->view("footer")
etc...

If the view being loaded (content.php) is very complex, (like a website homepage where its made up of multiple sections which need info from the controller - like seperate database queries, forum post summaries, sliders etc)....should ALL that data be generated in one controller then loaded into $data array and passed to the content view?

Feels wrong doing so much in one controller and passing via one array to a single view.

(noob friendly replies please)

Many Thanks
#2

[eluser]adamck[/eluser]
You can create a model that grabs everything you need for the view (if its repeated alot) and return it to the controller as an array.
Then pass that to the view.
I would suggest using the CI parser function, its much easier to work with!

Something like this...

Model
Code:
function getstuff(){
$data = array();
$query = $this->db->select...
$query2 = $this->db->select...
$query3 = $this->db->select...
$data['files'] = $query->result_array();
$data['clients'] = $query2->result_array();
$data['friends'] = $query3->result_array();
return $data;
}

Controller
Code:
$data = $this->some_model->getstuff();
$this->load->view('header');
$this->parser->parse('content', $data)
$this->load->view('footer');

View
Code:
{files}
{file}
{/files}
{clients}
{name} - {surname}
{/clients}
{friends}
{friend_name} - {friend_surname}
{/friends}

Along those lines

Adam.
#3

[eluser]CinoGenX[/eluser]
Cheers Adam.

Is there any other advantage to using the template parse class above just php other than ease of use in the view for outputting the array items?
#4

[eluser]adamck[/eluser]
Hmm not sure. I've found it better for tamplating as when I load 3 views they load in order of whatever has the least code. Which usually means my site loads


Footer
---------
Header
---------
Content

But the parser seems to load in the order I set.




Theme © iAndrew 2016 - Forum software by © MyBB