May 13 2009

Building Applications using CodeIgniter (Part 4) – Code Templates

by Chris

Last July I wrote a post entitled “What does your code say about you?” In that post I discuss the importance of writing clean well formatted code. It’s vitally important, even if you are the only programmer working on a project, that you document and format your code because you may be the only programmer now but who knows about 6 months or a year from now. Plus, as a programmer, the code you write is a direct reflection on you as a professional.

So far in this series I’ve discussed my typical application structure, configuration and helper files when developing apps using CodeIgniter (CI). In this final post I’ll review creating code templates for quick consistent development.

There is nothing really functional/technical about code templates. Templates are a set of simple files that have the basic document structure for the specified file (controller, model, library, etc.) that you use as a common starting point when creating any new file for your app. Your template should contain common elements to all files like the header comment block, class declaration, constructor, etc. There is no need to come up with your own standard when creating your templates because CI has a very nice style guide you should follow.

CI Style Guide

A few versions ago, CI added a page to their users guide entitled PHP Style Guide. In this section they do a great job at outlining the proper format when declaring variables, writing comments, naming files, etc. This is a great place to start when creating your templates. If you don’t already, you should think about getting in the habit of following these standards (even if you aren’t programming with CI) because they will help keep your code clean and consistent.

Template Files

Before I start coding my app I typically create a template file and place it in the controller and model folders. The templates are different of course, but they both create a nice starting point whenever I need to create a new file. It’s important to note that you don’t necessarily have to create physical template files to accomplish clean and well formatted code. Some tools like Coda and TextMate give you the ability to save bits of text and reuse them in your files. So you could create your header comment block and save it in your editor and just call upon that whenever you create a new file. Doesn’t really matter how you do it, it just matters that you do.

That’s a Wrap

OK… lecture over. That does it for the building applications using CodeIgniter post series. If interested, you can download a copy of the final CI install that we’ve created over the last 4 posts with helpers, code templates and all.

I hope by sheding some light on my process and explaining how I do things you came up with some ideas on how you can improve your own CI apps. As always, feel free to leave any questions or feedback as a comment below.


10 Responses to Building Applications using CodeIgniter (Part 4) – Code Templates

  1. Derek Allard May 13, 2009 at 7:34 am

    Chris, I’m a fan man. Great posts!

  2. Jacob June 2, 2009 at 9:17 am

    Great reading, thanks a lot!

  3. Abdul Aziz November 11, 2009 at 10:06 am

    Great Post… Really helpful… Thanks a bunch :)

  4. Pingback: Getting Started with CodeIgniter and How to Create All Those Great Apps | DevSnippets

  5. Amit Gupta October 20, 2010 at 4:05 am

    Great Post ….thanks a lot.

  6. Manoj Soyal December 20, 2010 at 2:16 am

    hi !!!

    Very nice post !! I am getting help !!

    Thankx

    Manoj

  7. Danny September 2, 2012 at 6:36 am

    Great work with your posts!!

    I would like to see the _templates and the layouts you use in codeigniter. I use a way but it can probably be done better. If you have time can you add an article that gets more in depth with the _templates and different layouts. The way I do my view folder is by using a header.php footer.php in an include folder in the views folder. Then i use home_view.php and an inner_page.php view file and i just add the header and footer to the home_view.php or inner_pages.php by using the load->view(‘includes/header’) ?> or load->view(‘includes/footer’) ?> as well as a nav.php in the include folder that i have under the views folder..

    If you know of a better way on making this not as cluttered or if you know how to make this better by using your _template and layouts folder that would be great! I am using the new codeigniter 2.1.2 of course. i would love to make different layouts and still have the view folder still be a nice looking folder and not cluttered up with different files and folders. Please let me know if you can get in more depth for the _templates and the layouts. I would love to see another viewpoint on how someone else does their templates and layouts.

    Thanks again for all your hard work, it has been very helpful!

    - Danny.

  8. Jean January 26, 2013 at 3:40 am

    Thank you so much for this codeigniter intro series, I am totally using this guide to get my team back on track with a rewrite using codeigniter framework to give some structure.

  9. Rick Mason February 21, 2013 at 10:20 am

    I’m sort of new to CI and PHP in general. I arrived at this site because I came up with my own way of organizing files and was wondering what other people are doing. It seems that you follow a very similar structure as far as templating goes that I do.

    I would recommend an addition though (maybe a rewrite for 2013?), and that would be the addition of a kind of ‘widgets’ folder under the view folder. After about 5 or so reiterations of my site starting from scratch each time, I found that there was no better way of doing it.

    As you suggested, passing view data as a string is the way to go. With widgets, you make sure that everything you build is completely reusable anywhere on your site.

    I build a controller which puts together the template. Within the template is a ‘$content’ variable that fills up the dynamic portion(s) of the page. This feeds from the content_model, which feeds from the template_model which goes to the view.

    Example: I want to have a right hand column that displays 2 widgets…a Latest Posts, and a Navbar.

    For my index controller…part of my template will be

    $page['right_column'] = $this->content_model->right_column();
    $this->load->view(“template/template”, $page);

    Within the content_model, the right_column() function will ‘build’ the layout. That’s all. So essentially, it is a larger widget that is made up of smaller widgets. Sure you can do this step in the controller, but I feel like its easier to manage in its own littler area. So the right_column() function looks like:

    $data['latest_posts] = $this->template_model->latest_posts();
    $data['navbar] = $this->template_model->navbar();
    return $this->load->view(‘content/right_column’, $data, TRUE);

    Then in the template model you build your actual widgets. Its great to be able to build a widget and know you can insert it anywhere, easily and conveniently.

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>