I was very excited to stumble upon this post and hear about the implementation of jQuery to CodeIgniter. In a sort of journey of self-discovery I’ve recently abandoned re-inventing any more wheels and decided last year not to engage in any more custom code.
I “wasted” nearly a year toying with Ruby on Rails before I sort of woke up and managed to free myself from the cult compound and came back to PHP.
So, I’ve been learning jQuery lately (again refusing to write any more custom javascript) and trying to integrate it with CodeIgniter gracefully. Which has been challenging. Here’s why:
I’m a bit of standards-geek: XTHML 1.0 Strict and CSS-positioning for everything. And yes, it better validate or I have to go to confession every week that the site stays up. Okay, maybe that’s the OCD and ADHD talking there, but you get the point.
What *kills* me is those who implement external scripts (and sometimes CSS stylesheets) on every page of a website… adding unknown kilobytes to pages where they are fully unnecessary.
So, I hope when CI makes the leap to integrate jQuery (oh, boy… I can’t wait for $this-javascript->do_magic), that they will somehow take that into account.
Here’s what I’m doing on a current project and I’m certainly not clear if this is the best way, but it’s what I thought of.
In CI, I have a header_view (as we all do, right?), but it quite ungraciously ends at some 20 lines past my opening body tag. In the old days, I’d end it one line before my closing </head> tag and then try to slap extra includes for certain pages (that would contain page specific javascript, etc) at the top of my “page” (or “section” for you T/P fans out there) before my closing </head>.
But yeah, that’s a lot of duplicate code in various sections/pages, particularly if you also want to include your template header in your header_view (your masthead, etc). You know, if you’re into that “code in one place and one place only” thing.
In T/P you could use conditional tags and just include certain “forms” (snippets), but that adds extra dB calls, wear and tear on the little PHP guy in the computer, and so forth. But that’s how I did it - so shoot me.
But in CI, here’s what I’ve been trying:
In my controller, I include two array variables: $scripts[] and $jquery_scripts[]. The $scripts[] contain the actual js filenames that I want referenced in the head for my external script loads.
The $jquery_scripts[] are the line by line calls to jquery in my body onload stuff (or $(function)... whatever).
Then in my header_view.php, I basically do an isset for $scripts and run them through a foreach loop in my load script tags… something like {base_url}/static/js/{insert $script here}.js
Then I do the same thing for the jquery_scripts array except after my $(function) call.
Thus, each page only loads the external script it needs and doesn’t waste time doing jQuery calls on elements that wouldn’t exist anyway. I’ve seen sites implement table-striping (zebra striping… whatever you wanna call it) that run that call on every page, even though there are only 2 tables on the whole site.
It could probably be even better, though. The jquery_scripts are really nothing more than lines of code (which I have to repeat on certain pages) so I really need to get those saved in some globalish variable that I can access from all controllers.
For instance, there’s no sense in loading the form handling features of some ui.jquery thing on the home page, the about page, etc… (uh, unless there is a form there of course). So, since I/we use a bunch of them thar jquery goodness for forms, why not just save the whole form calls as an array and just pass them just on controllers that deal with forms? Well, because a) I’m still somewhat lazy (come on!) and b) you have to start making allowances for different ids and classes on some forms that might not be on others (particularly true when developing a web app over a web “site”). But, it probably could be done.
Anyway, I pass that on as my official “two cents worth” and am hoping that when/if CI integrates jquery so gracefully as they seem to be promising, that this is thought of. But even if not, it will make it easier for us. Thanks so much for choosing jQuery as your first library for this.