↓ Skip to main content

Extending the V in MVC - revisited

This post is an re­vised ver­sion of ‘Ex­tend­ing the V in MVC on the web’ of Novem­ber 9, 2008.


When I first star­ted work­ing with the MVC method it was with my own frame­work. After no­ti­cing that main­tain­ing my own frame­work would be very time-comsuming I em­barked on the search for a free, fast and widely sup­por­ted PHP web frame­work. The one I found that time was ‘CodeIgniter’.

After work­ing on dif­fer­ent pro­jects I no­ticed that most web­sites present the same data to the vis­itor in sev­eral dif­fer­ent formats. Think of RSS/Atom for blog posts for use in an RSS reader to­gether with the (x)html ver­sions.

While es­sen­tially provid­ing the same data to the vis­itor the pro­gram­mer of the web­site has to put siz­able amount of work to add little ex­tra func­tion­al­ity in the form of an RSS feed. Re­peat­ing a lod of code for in­put val­id­a­tion, data re­trieval from the data­base and cre­at­ing ex­tra routes into the ap­plic­a­tion. As you can ima­gine this could grow into a te­di­ous job.

I’ll be de­scrib­ing tech­niques known and in use today, but re­in­ven­ted each time again as a (new) de­veloper starts work­ing on an web ap­plic­a­tion. Here we’ll be giv­ing them name and be de­scrib­ing them.

Theory

What are layouts? What are partials? The answers to these questions will be discussed in the following sections.

Layouts

What is a layout? We will be using the following definition of the word layout: "A layout is the representation of data in a specific format". So the layout of a page can be either, xhtml, RSS, PDF, PPT or any other data format.

Sup­pose you would find your­self in the situ­ation where you need to write a blog. You could start with build­ing the mod­els that de­scribe users, posts, com­ments and maybe even back­tracks. Then you start writ­ing the con­trol­lers and views and let someone else do the design for you. :)

So, the de­signer has made some nice designs and you are con­vert­ing those to (x)html, CSS and im­ages for use in your views. In the same time you write your con­trol­lers, one for every as­pect of your blog. Con­sider the con­trol­ler that shows a list of your most re­cent posts.

Blogs are es­sen­tially meant for pub­lish­ing in­form­a­tion, the mes­sages the poster posts, to the world. People want to see a nice read­able web page when they look at your blog. But it is hard to keep track of all the blogs one vis­its for new posts. That’s why people in­ven­ted the syn­dic­ates. And look now, here you are, hav­ing two URLs provid­ing the same in­form­a­tion, the latest posts, and the only dif­fer­ence is that the in­form­a­tion is that they present the data in an­other format. So why should the URLs be dif­fer­ent?

  • Other than the ex­ten­sion that de­notes in which format the page should be presen­ted?
  • Right, they shouldn’t. Nor should you have to write any ad­min­is­trat­ive code. The only code you should have to write is the code re­spons­ible for present­ing the given data in a spe­cific lay­out.

With just a small amount of ini­tial trouble, which we’ll see in the next chapter, it will save you a lot of time and ad­min­is­trat­ive rut. Sounds good, doesn’t it?

Partials

Another aspect of this article is 'partials'. Programming today is all about re-using code and rapid development and low-maintenance. OOP gives you the ability to re-use your previously written code you worked so hard on in a sensible matter. It also can help in reducing the draught of maintenance. The MVC paradigm is extending this and helps by further aiding rapid development.

In the as­pect of re-using code, par­tials are very im­port­ant. Where OOP , Mod­els and Con­trol­lers can save you time by re-using pro­gram code, par­tials can help you save time by of­fer­ing a new way to re-use your html (or any other lay­out) code. Thus not only de­creas­ing the main­ten­ance needed for your ap­plic­a­tion but sim­ul­tan­eously in­creas­ing con­sist­ency through­out your ap­plic­a­tion without caus­ing you any head­aches.

Where does the name ‘partial’ come from? The ori­gins of this name are quite simple. A ‘partial’ is a small re-usable snip­pet of View code and is a View it­self.

Par­tials can be used for many things. For ex­ample, the header on all your blog pages could be the same, apart from vari­ables as titles and needed stylesheets and javas­cript in­cludes. As we have learned dur­ing the age of the static webpages, main­tain­ing this would be an hell of a pain. So let’s not do that any­more and let’s use par­tials in­stead. Keep the code in one place and just use the par­tial if you need the header. Do no re­type or copy any code.

Of course, head­ers and foot­ers are not the only things that we can store in par­tials. The menu, the lo­gin form, the code for a blog post and the lot can be placed in par­tials. Ac­tu­ally al­most any­thing that will be used more than once should be placed in a par­tial.

An implementation in CodeIgniter

This part shows one way to implement layouts and partials in CodeIgniter. We assume you have at least version 1.6.3 of CodeIgniter and that you have a fair idea of what we are going to do. Proceeding is completely at your own expense and I am not accountable for any issues that may rise do to these adjustments

Adjustments

The Controller class
The first thing we have to do is extending the main controller class provided by CodeIgniter. This enables us to do whatever we want without changing anything in the CodeIgniter code base. Place this function in the extending controller:

[file lang=“php”]snippets/extendingthevinmvc/controller.php[/file]

The loader
Now put these functions inside class extending the CI Loader class:

[file lang=“php”]snippets/extendingthevinmvc/loader.php[/file]

Usage

Views
Now that we have extended the functionality of CodeIgniter with our own we can start to use layouts and partials. Consider the following url:
http://someurl/blog/category/5

It will give you a nice and cheesy over­view format­ted in xhtml. So let’s take a look at the view file.

[file lang=“php” pre­serve­format=“on”]snippets/extendingthevinmvc/short/xhtml_blog_category.php[/file]

And the code for the short sum­mary looks like this:

[file lang=“php”]snippets/extendingthevinmvc/short/xhtml_blog__shortsummary.php[/file]

Now con­sider this url:

http://someurl/blog/category.rss/5
This url would give you the overview of the posts in category 5. Just as with the url above. But this time the information will be formatted in RSS. Notice that the only thing we had to do was to change the url. Create these two files:

APPPATH/views/rss/blog/summary.php

[file lang=“php”]snippets/extendingthevinmvc/short/rss_blog_summary.php[/file]

APPPATH/views/rss/blog/_shortsummary.php

[file lang=“php”]snippets/extendingthevinmvc/short/rss_blog__shortsummary.php[/file]

Wow. Now we only had to write a few lines (15) for adding RSS feeds to our site. This is really all there is to it. Now go out there and have fun.