A couple weeks back, I talked about HAML and why I’m not a fan of its approach. I promised in that article that I would be following up with some discussion of how I think UI templating should work. I intended to post earlier than this, but then I dove into a fun new coding project (which I will write about here soon), and I’m just now resurfacing for air.
Disclaimer: The criticisms I have of various web application frameworks and platforms are not entirely universal. There are some solutions out there which have vastly improved their approaches. For instance, I’ve borrowed some good parts from Django, Mustache.JS, even (whisper) .NET MVC. The problem is, the bad stuff is so overwhelmingly prevalent in the development community, it chokes out the life of those who really are trying to make it better. Add my voice to that list, and a +1 for all the others who are on that same page.
This will not be a simple quick post. There’s so much I need to write, and it just can’t all fit into a few paragraphs. Please bear with me as I expound on this topic.
Handlebar?
Before I go into further explanation, go check out HandlebarJS Template Engine, my entry into the arena of how to construct web UI. It’s important to note, this is not some “Yet-Another-” all-in-one framework that has sexy themeable widgets, a CMS, or auto-generated admin interfaces. All you will find there is a basic, simple Templating Engine, that takes JSON data as input, processes and compiles text-based templates (that you create and control 100%) into “executables”, and pumps the JSON data into the compiled templates to return a “View”.
There’s much more planned for HandlebarJS, including tools for build-processes, filters, compression, UI optimization, etc. They will come in the form of loosely coupled “plugins” that are compatible with HandlebarJS’ “view” of the world.
HandlebarJS is really quite the opposite of most UI frameworks: it’s a stripped down, deconstructed, anti-framework tool for what is now being called the “middle end” — the stuff that sits between the front-end and the back-end, and negotiates communication, View construction (templating), URL routing, data validation, and other fun things.
What HandlebarJS is not is any kind of RIA framework that attempts to handle the behavioral side (JavaScript) of rich UI. HandlebarJS is only concerned with as quickly and efficiently as possible generating the markup for the UI.
HandlebarJS is intended to be one of several independent components for re-thinking the middle-end UI architecture of web applications. Those other components deserve their own full projects and explanations/write-ups, so you’ll just have to keep an eye out here over the coming weeks for those.
Why should I care?
You may ask yourself at this point: “I have XYZ framework or platform (.NET, Java/Swing, RoR, Django, etc) already, and it does all that stuff for me. Why do I need something else?” Well, that’s a great and valid question, one that deserves a lot more discussion than can ever fit into one blog post. I do have a very specific set of answers to that question, and I will be unveiling it little by little through blog posts (including this one), separate dedicated websites, and even an upcoming planned book.
For now, I’m going to ask you the reader to take my word just a little bit — that’s there strong reasoning behind what I’m trying to do — and let me develop and explain it bit by bit. This post will present some of the high level motivations and discuss specifically HandlebarJS for templating — my goal for now is that you’ll come away more knowledgeable about just an alternative to how your web application currently approaches its Views. I’m not yet trying to convince you that you need an entirely different application architecture — we’ll get there slowly, and it won’t be as scary as it may seem.
What am I trying to solve?
I don’t want to re-write here the entire article and underlying arguments I had against the HAML approach. But, I do encourage you to read it to understand better where I’m coming from.
However, here are the primary thoughts that were motivating factors for creating HandlebarJS:
- DRY and Portability: For decades, the development community has operated (to varying degrees of authenticity and success) under the principle of not repeating yourself, meaning as much as possible, don’t have two different sets of code that do the same thing. The advent of functions/sub-routines laid the ground work years ago, upon which Object-Oriented programming expounded to give us even better patterns for write once, use many.
The problem is, the explosion of the web application paradigm has thrown several wrenches into the mix. What fundamentally used to be a single monolithic application, or even a straightforward server-client application, is now a highly complex, distributed, cloud-integrated, web connected, desktop and mobile consumed, rich, thin-and-thick application conglomeration that challenges with orders of magnitude more complexity than our older patterns are capable of handling. It’s almost like we went from n-tier to n^2-tier architecture in a relatively short period of time.
Moreover, the paradigm of the presentation layer residing in a browser has both extended and limited the evolution of such applications. For several years, even after the invention of Ajax (which revolutionized the page-refresh), web developers have struggled with wanting to do more with Rich Interfaces than the browser (or even internet connection or host computer) was capable of handling. The latest generation of browsers, web technology standards, and computing equipment has finally broken down many of those barriers.
But just as we finally got to the point where the desktop web browser could be an effective tier in the overall application (not just a thinly veiled client), now we have an intense push to extend all our web application presences into a new frontier of “connected” devices (mobile, smart phone, netbooks, etc), many of which are (in some ways) throw backs to the performance (CPU, connection speed, etc) we could rely on with desktop browsers 5 and 8 years ago.
Even as mobile Safari pushes to implement the latest and greatest HTML5/CSS3 standards, the reality is that mobile web apps are still handcuffed by limited processor capacity, severely limited browser caches, and unreliable (at best) connectivity.
So what does all this have to do with DRY and portability?
I believe we are at an inflection point in the web UI evolution where we must start to think about things differently if we have any hope of keeping up with the exponentially complicating trends of consumer technology. We’re starting to see the fundamental inadequacies (and the pain it brings) of established solutions, in their inability to efficiently handle the rapidly expanding array of consumers of UI presentation.
These all-in-one frameworks are so attractive to development teams because they fully abstract away all of the messy details of how markup and data are combined to be the delivered UI product. But with that ease-of-use comes a price — lack of flexibility to control and optimize such UI delivery on the fly depending on who is consuming it. A fancy .NET list widget may rock for UI delivered in IE8 on a desktop, but is it up to the task of usable and functional behavior/display in a mobile Opera browser on a screen 1/16 the size of a laptop screen and with 1/10 the memory and CPU? The sad answer is often: absolutely not.
And no, it’s not enough to just have a different stylesheet for mobile browsers and expect that CSS will save the usability of your app. Mobile (narrow-screen) is an entirely different paradigm in almost all respects to desktop computing. To effectively leverage both platforms simultaneously, AND with the same (unrepeated) code bases, we’re gonna have to get a lot more creative.
“And now, for something completely different”
We need an approach to UI that is powerful yet compact, that is portable (meaning it can run in both a server environment and in a variety of browser host environments), and that is flexible enough to be extended/composed/dissected/etc as necessary for the different devices consuming the UI. This isn’t just an incremental improvement on existing systems, it’s an entirely new new kind of approach.
Of course, if you prefer to write code over and over again, and maintain different application branches for different presentation consumers, then you’ll probably totally disagree with me here — you’d probably rather just stay the course. But I think the future is more re-use and convergence, not less. And I make no apologies for the fact that this is my fundamental bias for everything I’m trying to do here. I do not like at all to Repeat Myself, and I’m going to do everything I can to keep that out of my development practices.
And I’m just crazy enough to believe in the idealism that it IS possible to achieve this utopia. We’re just gonna have to be brave enough to question everything we’re currently doing. No big deal, right?
- Do front-end type work in…<gasp>…ONLY front-end technologies: I feel like I’m probably admitting frequently on this blog to not only being strongly opinionated but also a little bit crazy. But that’s ok with me. I can tell you we are not gonna ever solve any real problems by thinking the same way we always do. And I like that I’m a fundamentalist — that I believe less is more.
I think we could use a lot more of that mindset in the development community. You wouldn’t get in a jet-liner to travel up to the corner store — so why do we feel that these immensely complex frameworks and platforms are the right approach to web applications?
Perfection is achieved, not when there is nothing more to add, but when there is nothing left to take away. — Antoine de Saint-Exupery
Let’s face it: we all (at one time or another) entertain dreams that our web application is going to need to scale to hundreds of millions of concurrent requests, and our development team is going to balloon to hundreds of distributed development groups, and we must plan for that now by using an enterprise-ready Solution.
Yet, when you look at the world’s biggest web applications — the Googles, the Yahoos, the Facebooks, and the Twitters of the world — you actually don’t see them using such non-sense to drive their applications. In fact, you often see fundamentalism blatantly plastered across their approach. Why? Because as things scale, what’s not needed is more machinery to manage it, but less to abstract away and play middle-man in the transformation of a request into a response.
This post is about UI, right?
Nowhere is this more true than in the UI layer (the middle-end). It’s been recently suggested by Steve Souders of Yahoo and Google fame that actually most of the performance bottle necks (of small and large applications alike) is the UI layer.
My thesis: the fewer layers of abstraction sit on top of the presentation layer, the more efficient the application will operate (in both low and heavy traffic situations). My corollary: This fundamentalist approach to UI will also lead to better separation of skillsets — a front-end engineer can completely control the UI of an application using only front-end technologies like HTML, JavaScript, and CSS, which they are likely best at.
This means that when they want to streamline the way their UI is constructed and delivered to address a page-load efficiency issue on their site (like JS/CSS concatenation for instance), they can get directly and immediately to the problem, in the front/middle-end layer (instead of wading through who knows how much back-end cruft that they may or may not be fully capable of navigating or changing). They can also use the technologies they are likely most familiar with (their core skillset) to solve the issues.
This may seem like a trivial distinction. But I believe it’s a critical and overlooked factor in developer efficiency: staying within one’s core skillset — the sweet spot — as much as possible. And this doesn’t just apply to developers but to technologies as well. I believe there’s a reason that Java largely didn’t survive as a pure front-end technology, and why there hasn’t been a pig push by the community to bring native Ruby processing to the browser. These technologies have their place, but the browser is not that place.
Whether you like JavaScript or not, it’s quite clear that it is uniquely and best positioned to service all the needs that the UI layer needs. And as you’ll see in a little bit, that means both in browser and on the server. You may not ever (want or need to) re-write your hundreds of thousands of lines of Java, but I hope maybe you’ll consider delegating your UI code to the technology that rules the UI.
“Both-enders”
I can just hear some of you right now: you’re the ones who wear both the hat of front-end developer and back-end developer (sometimes at the same time). For you, it’s no big deal at all to “context switch” mentally and use back-end technology (like a JSP taglib) to generate your front-end. In fact, if you’re not a specialist in UI technologies, that may be more comfortable and productive for you to do so.
I don’t claim that I can convince you otherwise. But let me just submit to you this suggestion: is it possible that you might be more efficient at your UI hat tasks if you didn’t have to concern yourself at all with back-end architecture issues? And vice versa, would your back-end work be more fun and efficient if you didn’t have to constantly worry about markup, box model quirks, or cross-browser JavaScript’ing?
I think it would. In fact, I’m almost certain of it (at least the UI hat side of things). I’ve worn both hats many times in many different jobs. And I can tell you that unequivocally, I was noticeably more efficient when I had the ability to only wear one hat any given time. What is good for the back-end is often times irrelevant (or bad) for the UI, and vice versa. They are two entirely different disciplines, and it’s time we admit that. Not that you can’t do both, and effectively, but just that you can’t do both at the same time effectively.
- Stop meddling in my code: Probably my biggest issue with how most frameworks and platforms approach templating is that the templating engine is deeply embedded in, and hopelessly intertwined with, the back-end technology. This invariably leads to far too often bending/breaking the fundamental rule of the MVC pattern (keep the View, Controller and Model separate).
For instance, consider PHP as a simple example. PHP is a templating engine (if you weren’t already aware). It allows you to take a predominantly HTML file and litter any manner of PHP code logic all throughout it. THIS IS NO BETTER THAN THE SPAGHETTI CODE OF QBASIC FROM 20 YEARS AGO. You actually have to fight hard against these urges (and pretty much every tutorial on the web), and go to great lengths to keep your templates free from such mess.
And why go to this trouble, you may wonder? Let me give this example:
<div id="#controls"> <?php if ($user->IsLoggedIn() && $user->CanPublish()) { ?> <a href="publish.html">Publish</a> <?php } ?> </div>Seems harmless enough, right? WRONG. This is pure and undefiled evil. You think that just because you put the login session logic and access control logic into your Model (wait, shouldn’t that actually be in the Controller???), and you merely call and use those methods in your template, you’ve achieved separation? WRONG.
Why do I feel so strongly? Because this pattern is blatantly opening up the doors to putting Controller (and Model) logic right inside the View. First of all, your front-end developer (if that’s not also you Mr. Back-end Engineer) has to know something about your back-end technology (PHP in this case) — and in some cases, they need to know a lot about the back-end to even accomplish a small UI task. This is probably not his or her core skillset (see #2 above), so it’s bound to slow down the efficiency of UI work.
Also, you have now tied business logic into your View, which is against proper patterns we all learned in Software 101. Consider this scenario (which is so common it’s almost self-obvious): The View developer writes code like the above. Then he leaves the company.
6 months later, a back-end developer goes through and refactors the Model. He decides for one reason or another that Access Control Permissions are now going to be exposed in the Model (ie, across all the back-end code) as a bitmask property upon which efficient bitwise operators (| &) can be used to figure out permissions. Because they want to keep the back-end code pure and efficient, and don’t want unnecessary methods literring the Model, they remove the $user->CanPublish() method. No problem, right? We just do a grep/code search and find all instances of CanPublish(), and we change that code to instead be ($user->perms & USER_CAN_PUBLISH).
Ugh. If this doesn’t scream out at you as bad, I don’t know what planet you develop on. It gets worse. 6 months later, the back-end developer quits, and shortly thereafter, the business folks decide that a user must have both USER_CAN_PUBLISH permission and not ($user->restrictions & USER_IS_DISABLED). I bet at this point, the new back-end developer instead gives in and creates a method $user->CanPublishAndEnabled(). Yet more Controller/Model logic hopelessly embedded in the View. And yet another round of View refactorings. I bet your front-end developers (who don’t know much PHP) are getting annoyed by now. Are you seeing the pattern?
What if instead, there was a pre-agreed-upon Boolean property in the View data (ahem, not Model), called “allow_publish”. The Controller takes care of setting that property (with whatever logic is appropriate at that moment in time) before it invokes the View, and it just passes along that Boolean flag to the View. Then, no matter what kind of back-end refactorings or logic changes occur, the View always look like this:
<div id="#controls"> <?php if ($data["allow_publish"]) { ?> <a href="publish.html">Publish</a> <?php } ?> </div>The difference may seem subtle, but it’s actually enormously important. While I’d definitely call this an improvement, there’s definitely more we can (and should) try to achieve.
But, my MVC can do that
The point is not really whether your current environment can do what I’m suggesting. The point is that it probably isn’t, and moreover, it probably feels easier to all involved if it doesn’t. Otherwise, you probably wouldn’t still be reading this article, because you’d be so furiously effciently coding away in your utopian development environment and would have no desire to read my crazy ramblings on the topic!
I believe we need to encourage Templating Engine solutions to be absolutely minimalist on the amount of “programming” they allow in the Views. The bias should be: allow only the bare minimum of logic as is necessary to select templates and fill them with data. If it’s any harder than that, then it’s logic that belongs back in the Controller (or deeper). Sure, you need basic things like boolean logic for selecting sub-templates, simple looping constructs, and such. But you don’t need the ability to call functions, invoke complicated, deep instantiation heirarchies of mixed Models, run math computations, or any of that non-sense. Remember: Jet-liner to the corner store.
Nodding off
OK, so at this point, you must be exhausted reading this post. I’m exhausted writing it. I promise I’m gonna wrap up quickly. I know it’s a lot of information to take in, but I’m intensely passionate that it needs to be said, and these questions and issues need to be addressed.
This article has thus far been almost entirely me laying out the reasons why I didn’t like most of the templating solutions that are common across the vast majority of web application frameworks and platforms. This is by far not an exhaustive list of the problems, but those 3 points above are a pretty good summary of the types of problems motivating me to find and present another way.
So, if you’re still with me after all that, let me try to briefly explain how I believe HandlebarJS addresses these concerns (and others).
Finally, some Handlebars
HandlebarJS gets its name as the upside down mustache (from MustacheJS templating). The { and } are the things of which we speak.
Here’s what you should know about HandlebarJS:
- HandlebarJS is entirely written in JavaScript. In fact, it’s entirely written in a manner that allows it to run in either a synchronous or asynchronous environment, either in the browser or on the server (using any of several available server-side JavaScript environments). This allows the exact same code base to run either in browser or on server. Period. Doesn’t get any more DRY than that. You can do templating in either place, or both, as your application requires.
It’s small and portable and so should be suitable for your templating tasks in a wide variety of JavaScript friendly environments, including even (and especially) mobile.
- It follows naturally that since the HandlebarJS code is portable and identical both in browser and on server, the templates themselves are also identical. Absolutely. The templating “language” is simple but expressive and capable of any tasks you should rightly tackle with templating. And so you can write your templates once, and run/interpret them in any HandlebarJS capable environment. Again, this is really DRY.
- HandlebarJS accepts JSON data, and only JSON data, as its input. This means that any platform/environment which can serialize its Model/data down to JSON can send that data to a HandlebarJS instance and get back a processed template View output. Pretty much every language on the planet has JSON serialization/de-serialization support built-in (or at least available easily). JSON data is also the only format of data that is fully and natively portable from server to browser.
That’s right, some browsers (and clients) don’t natively understand XML even, but they all understand JavaScript, and thus all understand JSON data. Why go to the inefficient trouble of wrapping your data up in some other non-natively-parseable data abstraction? Just use JSON. Trust me, there is no other format better for the task.
- HandlebarJS is built with simplicity and efficiency in mind. The templating language is as simple as possible to get the job done. It doesn’t give you too much rope to hang yourself. You really can’t embed Controller or Model logic in these Views.
Also, HandlebarJS compiles templates from their text files into executable JavaScript snippets. This “compilation” can be done on the fly (in either browser or server) if necessary, or it can be done as part of a build-process to increase efficency. HandlebarJS will operate the same functionally regardless of whether it’s dealing with pre-compiled templates or needing to compile them on-demand.
Fin
OK, that’s it, I’m done, I promise. I hope by this point you’re at least convinced it might be a good idea to take a look at HandlebarJS. Even if you are still certain that your existing application architecture is sufficient and won’t need to change, perhaps you might consider exploring if just the View portion can, in whole or in part, be separated off and delegated to a HandlebarJS instance.
If you only want to use HandlebarJS in the browser, you can do so without any architectural/server changes at all. Just drop it into an existing page and start converting your existing templates one-at-a-time. See how easy it is!
And absolutely the only required dependency for HandlebarJS (on the server) is a simple server-side JavaScript environment (many options are available). In fact, keep an eye for the next blog post here, as I will be discussing my brand new released BikechainJS, which is a SSJS wrapper around the V8 JavaScript engine, allowing drop-dead simple JavaScript execution on the server. HandlebarJS and BikechainJS are specifically built to be simple and compatible.
Seriously, stop reading this blog post now and go check out HandlebarJS — isn’t it about time for you to grab your UI by the “Handlebar” and make it do what you want?
