Sep 02, 2010
After you make the building blocks of your application, using Models, Views are how you display that information to a user. Having written a lot of JavaScript that adds content to the page, I know just how much it can suck. You typically have two options:
innerHTML of a div.document.createElement and el.appendChild, ad infinitum.Both are equally unappetizing. Wouldn’t it be so much nicer to write your views like we do in other languages?
Here’s a nice mix of HTML and JavaScript. No tricky string concatenation1. You just have to put your JavaScript inside tags like you would in PHP.
<% tasks.forEach(function(task) { %>
-
<%= task.get('title') %>
<% if(tag.get('tags').length) {
print(view('tasks/inline_tags', { tags: task.get('tags') }));
} %>
<% }); %>
Besides print, which does exactly what you think it does, there are a bunch of other functions that are sort of “globally” accessible from a View. And these are functions defined on View.Helpers. The most basic version of the helpers only includes a view function, which let’s you nest views inside each other, like I used above. If you include the View.Helpers file, you get more functions, such as excerpt (truncates to a certain length) and date (allows date formating similar to PHP’s date method). As I find more basic functions that you would want in a templating system, I’ll add more, such as possibly something like escape.
These are saved as .html files. You don’t need to manually include them like Models or Controllers; the View class will first check to see if they’ve been included, and if not, go and fetch them over the wire. And they go in you app’s views folder. The example above would have a structure like so:
js/
views/
tasks/
list.html
The View Class is what converts templates into views for your application. You can instantiate a View, and then pass it data and ask for it to process itself using the passed data.
var myView = new View('tasks/list').render({ tasks: myTasks });
myEl.grab(myView);
myOtherEl.set('html', myView);
Views have both a toElement and toString function defined to return the processed mark-up in the proper format. This means you can simply pass the view as a parameter to most MooTools Element methods, and it just works2.
Good news, though: Controllers have a convenience method called view that really makes this a joy to use. I’ll have more to say about Controllers in my next report.
The string concatenation is done internally. Everything in the template is turned into a string using John Resig’s micro-templating suggestion. ↩
Regular DOM methods might not though. appendChild expects an Element, and since it’s not part of MooTools, it’s not rigged up to called toElement. Also, because of the way toString works with Classes in Internet Explorer, this feature doesn’t work in those browsers. MooTools 1.3 repairs that problem. ↩
Aug 30, 2010
Ladies. Look at your code. :Now look at mine.
Sadly, it isn’t mine.
I’m on a horse.1
I really shouldn’t be amazed that any time someone points out something neat coming to their language, everyone must point out “language X does it better” or “we’ve had that forever in language Y”. ↩
Aug 30, 2010
Chrome started it.

Firefox is following along in version 4. The browser in Android does this too.
There is only one bar to enter text. You can enter a URL, and it will work as you’d expect. Or you can enter some terms, and it will send you to your default search engine.
Google has done the same with the default browser in Android. Apparently, it can be confusing in the iPhone’s Safari.
To think that most users of a browser are going to know the difference between a URL and search term would be wrong thinking. To make users care about the difference, for those that do know it, is not caring about your users. All the users should care about is:
How can I find what I’m looking for? Ah yes, the browser bar. I’ll just type what I’m looking for here. It will tell me.
When will all browsers finally combine the location bar with the search bar?
Aug 26, 2010
What's in your Simplenote?:It’s always cool to learn new systems to productivity and work. Patrick Rhone asks people to share what they keep in their Simplenote, and so I join in:
I quite like Patrick’s Yay me! idea, and Randy’s elevator pitch/bio idea.
Aug 26, 2010
Stylebot:This is a fantastic extension for Chrome. This lets you easily edit the styles of a web page, and have them be saved and applied next time you visit.
The demo shows one excellent use: improving a site you frequent. I did that for Kotaku/Lifehacker. I never use the Share box, and always mistake it for a search box. So, share box hidden, search box moved into place.
Also, I have refrained from Adblockers in the past because I don’t want to block all ads. I respect websites’ needs to make money. But I don’t respect the need to make a website that is more about the ads than the content. So, certain websites that cover their articles in flash ads, or have far too many ads in the sidebar, have their ads hidden.
Yes, I know. Shame on me, etc.