CodeIgniter Forums
Best practice to code "includes" (views)? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Best practice to code "includes" (views)? (/showthread.php?tid=42690)



Best practice to code "includes" (views)? - El Forum - 06-15-2011

[eluser]vadimyer[/eluser]
Hello!

I'm rather new to CI and first and maybe only question I have right now is how can I make some "general" template file or files (like header.php, footer.php, sidebar.php in WordPress) and then include them just pasting one line of code in every view? Do I have to create views like header_view.php etc. etc. for that? Or is there any other possible method?

Thanks in advance for you reply! Also I want to say that I'm looking for the best practice, not the easiest one.


Best practice to code "includes" (views)? - El Forum - 06-15-2011

[eluser]marcogmonteiro[/eluser]
Normally i do this:

template_view.php

with this code:

Code:
$this->load->view('includes/header_view');
$this->load->view('includes/head_view');
$this->load->view('includes/navigation_view');
$this->load->view($main_content);
$this->load->view('includes/sidebar_view');
$this->load->view('includes/footer_view');

then when i load the view on my controller I do something like this:

Code:
$data['main_content'] = 'articles_view';

$this->load->view('template_view', $data);

I don't know if this is the best way, but this is how I manage to segment my views.


Best practice to code "includes" (views)? - El Forum - 06-15-2011

[eluser]cideveloper[/eluser]
or you could use a template library from these 2 fine gents

Colin Williams

Phil Sturgeon

I have used the first one a lot and it suits most of my needs. I haven't fully tried our Phils but it looks real good too.


Best practice to code "includes" (views)? - El Forum - 06-16-2011

[eluser]marcogmonteiro[/eluser]
ok, that's probably would be the way to go then! Smile


Best practice to code "includes" (views)? - El Forum - 06-16-2011

[eluser]vadimyer[/eluser]
Thanks guys! But I like the way you do it, cancerman. Probably, because it's much easier than diving into those template libraries, although they're pretty easy to learn.

Maybe any other solutions, people?


Best practice to code "includes" (views)? - El Forum - 06-16-2011

[eluser]marcogmonteiro[/eluser]
yep, that's why I did it like this...


Best practice to code "includes" (views)? - El Forum - 06-16-2011

[eluser]Narkboy[/eluser]
I tend to do something similar:

base.php (tempalte view file):
Code:
[...]
<body>
<div id="head">&lt;?php $this->load->view('head');?&gt;</div>
<div id="nav">&lt;?php $this->load->view('nav');?&gt;</div>
<div id="content">&lt;?php $this->load->view($content);?&gt;</div>
&lt;/body&gt;

The $content file is set in the controller, and varies from page to page. This way, the view 'frame' is set on the base.php, and area-specific content is set out in the head.php view, nav.php view etc. Save me from having to remember / check that each version is consistent.

Typically, for each new design I'll write out 2-3 'bible' view files, a form.php file that contains all possible form related elements, a table.php file, a 'text.php file, etc - that way I can quickly check how to do a given series of elements.

Whatever works for you!

/B


Best practice to code "includes" (views)? - El Forum - 06-16-2011

[eluser]marcogmonteiro[/eluser]
That's good, i normally add one more to all my applications. The error view. This one is always activated with flasdata (normally i only use flashdata for confirm or error messages)


Best practice to code "includes" (views)? - El Forum - 06-16-2011

[eluser]Narkboy[/eluser]
[quote author="cancerman" date="1308238468"]That's good, i normally add one more to all my applications. The error view. This one is always activated with flasdata (normally i only use flashdata for confirm or error messages)[/quote]

I tend to hold errors in a seperate controller. It feels more secure to me, though makes very little practical difference. Basically, if you hit an actual error that prevents completion, redirect to my-domain.com/error/[error_type].

/B