8 ms·
Twitter releases new JS template engine: hogan.js
- stdbrouw 15y agoCompiling is nice. Not a big fan of Mustache though, the tags look like Brainfuck to me.
- thomaslee 15y agoIf you're into compiled templates & more traditional syntax, you could try jazz: http://github.com/shinetech/jazz http://github.com/shinetech/jazz (Shameless self-plug. I wrote the initial implementation.)
- bricestacey 15y agoYou can change the delimiter if that helps.
- decklin 15y ago<script src="//goo.gl/OCK7V"></script> Really?
- kylek 15y agoThis irked me on a few levels too.
- Skywing 15y agoI don't understand the problem. What is wrong with the example they've given?
- tednaleid 15y agoIt's because they're using an url shortener in a non-twitter/character limited situation. Makes the browser need to go through a couple of jumps to load something that the user will want to load fast.
- alecperkins 15y agoAlso, it's bad practice to obfuscate the urls to scripts (outside of an automatic concat/compress process), especially external ones.
- jasonkostempski 15y agoIs it possible using goo.gl might get you something like a poor mans CDN? I'm thinking it may possibly respond immediately with cached results.
- mburns 15y agoNo. Regardless, a second DNS query will have to be performed by the client when Google responds with the permanent redirect.
- rhubarbquid 15y agoNo, it responds with a redirect: $ curl http://goo.gl/OCK7V <HTML> <HEAD> <TITLE>Moved Permanently</TITLE> </HEAD> <BODY BGCOLOR="#FFFFFF" TEXT="#000000"> <H1>Moved Permanently</H1> The document has moved <A HREF="http://twitter.github.com/hogan.js/1.0.0/hogan.js">here </A>. </BODY> </HTML> Edit: I suppose the headers are more important: $ curl -I http://goo.gl/OCK7V HTTP/1.1 301 Moved Permanently Content-Type: text/html; charset=UTF-8 Expires: Thu, 22 Dec 2011 18:18:46 GMT Date: Thu, 22 Dec 2011 18:18:46 GMT Cache-Control: private, max-age=86400 Location: http://twitter.github.com/hogan.js/1.0.0/hogan.js X-Content-Type-Options: nosniff X-Frame-Options: SAMEORIGIN X-XSS-Protection: 1; mode=block Server: GSE Transfer-Encoding: chunked
- adgar 15y ago> I'm thinking it may possibly respond immediately with cached results. Just because nobody explicitly said it - a 301 redirect is actually cacheable per §10.3.2 of RFC 2616. But google doesn't set a long cache duration - another one of your responders posted the response headers, it was 86400s which is 1 day.
- adgar 15y ago
- rhizome 15y agoIs that why I just got a menu-bar failure message 20sec after the page nominally loads? Terrible. Also, follower notification emails appear to be broken.
- yahelc 15y agoEven worse, goo.gl doesn't actually support HTTPS. So, on HTTPS pages, that will just fail. (It actually just loads google.com)
- mcobrien 15y agoWhat's more is it redirects to a GitHub-hosted URL. Maybe it's just me, but it seems cheap that twitter would advocate hotlinking off a GitHub server when they have so much capacity of their own.
- Raphael 15y agoI would have expected //t.co
- fat 15y agolol sorry, i'll take the blame for that. The link was originally just a placeholder for our awesome designer @dhg (who rocked the site in like an hour). I missed the link before it got pushed, and forgot to swap it out. When first published it didn't even point to an actual js page. :( Anyways, I've fixed the redirect, but should have just removed the link all together. I've updated it now to point directly to github. The main reason that we're using github to host this (and bootstrap) rather than a proper cdn is because it makes publishing new versions super fast and easy. Of course we don't recommend that you hotlink from github for any serious application. You should bundling these with your other assets :)
- ascendant 15y agoOh sweet. I was just saying to my coworkers that what we REALLY need is another javascript templating engine.
- intranation 15y agoIt's not another JS template engine - it's a pure JS implementation of Mustache that's apparently significantly faster than Mustache.js. They also support compiling of templates for deployment purposes, which is nice.
- hallman76 15y agoAnd then twitter released one! Awesome timing! Now you don't need to write your own.
- fat 15y agoyeah we felt the same way, but we're trying to move off of mustache.js for performance reasons (also for size in our new tweet embeds projects).
- equark 15y agoI'm surprised that no JS template engines ever implement binding. It seems like everybody just ports some server-side ideas, even though there's a lot more that is possible client-side. Why do I need to rerender the entire template when my data changes? Sproutcore2 is an example of this, but it's pitched as a full framework rather than a binding aware template engine.
- andrewingram 15y agoAssuming I understand your use case correctly, I think Knockout is what you're looking for: http://knockoutjs.com/ http://knockoutjs.com/ I used it a fair bit last year and quite enjoyed it.
- shadowfiend 15y agoYeah, I love knockout. And honestly with version 2, they've implemented native templates, which basically means you can write your templates in HTML instead of strings (gasp!). I much prefer this concept. Templates in strings never made sense to me when we already have a DOM to take advantage of, but only recently have I seen some of the great work out there to make this possible, like this, Lift's CSS selector transforms (http://simply.liftweb.net/index-7.10.html http://simply.liftweb.net/index-7.10.html ), Nodejitsu's weld (https://github.com/hij1nx/weld https://github.com/hij1nx/weld ), etc.
- stephen 15y agoI agree, just rendering is a server-side notion that obviously can work on the client-side, but I don't think is ideal. Web 1.0/server-side templates had to render and sling strings of HTML around because that's all you could do (Rails's RJS was a cool Web 1.5 hack in that regard). But once you're on the client-side, and have state, I think binding/mutating the DOM in-place is better than trying to explicitly maintain all the state needed to re-render-from-a-string-on-change all the time. And having a template engine/view layer that supports as a first-class notion is a good thing. I used to (naively) think all JS template engines worked that way (merely re-rendering), but as the other comment says, Knockout at least does not, and I'm sure there are others that don't as well; I'm not an expert on all (or any) of them. I wrote up some of this render vs. binding differences in a post describing my port of the todomvc sample app to my backbone-ish GWT framework: http://draconianoverlord.com/2011/12/10/todomvc-in-gwt-mpv.html http://draconianoverlord.com/2011/12/10/todomvc-in-gwt-mpv.h... (Not that GWT is a terribly popular technology in these circles, and it's not perfect, but I enjoy it.) Another interesting assertion I make is that selectors are another Web 1.0/1.5 hack that can go away now that we have state and aren't forced into "the server gave us a huge blob of HTML, now do stuff with it!". IMHO of course.
- saturdayplace 15y agoCould someone perhaps help me out re: the difference between Compiling and Rendering JS templates? What it feels like is this: Compiling a JS template on the server gives you a template object that has a render() method. On the client side, you create an instance of the object and pass its render() method a context? Am I missing anything? If anything, this looks like what I've been looking for to get my templates off my page and into a separate JS file. Makes things cleaner. And apparently faster?
- tjholowaychuk 15y agoIt's usually two separate steps anyway. Populating HTML via the object passed in _while_ parsing is a sloppy way to go about it. This lib (and most template engines) compile down to an executable Function, that you then pass this object to. This means you can re-invoke it several times with less overhead
- donbronson 15y agoNice to see Twitter has a sense of humor. "Lovingly crafted by bros" in the footer is a nice touch.
- reinhardt 15y agoQuick, someone remove this before the sexism police sees it and we end up with yet another ZOMG-wont-someone-think-of-the-women 100+ comment thread.
- mundizzle 15y agoThat's a regrettable comment. Are you feeling victimized yourself? Self reflection may help you figure out why.
- fat 15y agothis was switched up a bit today. That said - for the ladies out there - girls can be bros. total bros.
- turtle4 15y agoResembles handlebars.js. Can someone point out the difference?
- tjholowaychuk 15y agohandlebars is based on mustache
- devinus 15y agoSo is Hogan, apparently.
- Void_ 15y agoNot based, it just does the same thing.
- superchink 15y agoActually, “hogan.js is a compiler for the Mustache templating language” –from https://github.com/twitter/hogan.js https://github.com/twitter/hogan.js
- Void_ 15y agoIt is compiler for Mustache, it does the same thing, it passes the same tests, but they didn't take Mustache code and improve it -- which is in my opinion what being based on means, they instead started from scratch and wrote a brand new parser.
- devinus 15y agoRight now Hogan seems to be slower and appears to have less features than Handlebars: http://jsperf.com/t-bench2/6 http://jsperf.com/t-bench2/6
- jarin 15y agoI may be misunderstanding this, but that benchmark appears to be based on an in-browser compilation, while Hogan's advantage is the ability to precompile templates.
- krmmalik 15y agoI was dubious when Twitter released Bootstrap.css, but i have to say, not being a UI expert i found it quite easy to work with. Concisely documented and the right set of tools that i need, for the most part at least. Some more on typography would have been nice. I have yet to understand how templating engines work, and what advantages they provide, but knowing this comes from twitter piques my interest for the future.
- tednaleid 15y agoOne place that it really clicked for me in a real world situation was by looking at the source for NodeUp, a node.js podcast. http://nodeup.com/ http://nodeup.com/ It's a beautiful website, and I was curious what their HTML looked like. If you look at the source for the page, all you see is the mustache template that they then populate with the information for all of the podcasts that they've done. You then can start to see the power that templating gives you. Serve up a template in your html, request some JSON to populate it with on page load and have it replicated. Makes your initial page very cacheable and clean. Much better than assembling html by hand with "foo" + "bar" or .append semantics.
- intranation 15y agoIf they're using Node (and I assume they are, based on the fact that it's a Node.js podcast), I don't really understand why they wouldn't render the HTML server side (I may be showing my age by asking this) instead of relying on the client to render it.
- siculars 15y agoShifts computational work to the client.
- scorpion032 15y agoNot necessarily a good thing.
- micheljansen 15y agoCurrently using Underscore, but I would almost switch to Hogan for the name. I am assuming Mr. Hogan's mustache was used for inspiration?
- leftnode 15y agoAnd it looks to be based off of mustache.js, another JavaScript templating library.
- micheljansen 15y agoHence the pun ;)
- dmix 15y agoThis looks just like Dust.js: http://akdubya.github.com/dustjs/ http://akdubya.github.com/dustjs/
- wavephorm 15y agoI have yet to see a template engine that has any practical advantages over conventional <%= stuff %> scriptlet style. Moving format logic out of your template into your controller always results an unmaintainable mess.
- LeafStorm 15y agoJinja2? http://jinja.pocoo.org/ http://jinja.pocoo.org/ A lot of template engines seem to either go for "FULL PROGRAMMING LANGUAGE POWER!" or "NO LOGIC! LOGIC IS EVIL!", but I've found that the sweet spot lies somewhere in the middle - you have enough power in the templates to do cool stuff without polluting your controllers, but it uses a language designed for templating and not a general purpose language shoehorned into HTML tags, and doesn't give you features you'll definitely never need.
- greghinch 15y agoHere here, the Django/Jinja model for templating is so much nicer than dealing with partials in every template
- bryanlarsen 15y agoIf you want to see a template engine that doesn't treat HTML as a dumb string, take a look at DRYML: http://cookbook.hobocentral.net/manual/dryml-guide http://cookbook.hobocentral.net/manual/dryml-guide
- RobertKohr 15y agoYep, that is why I use EJS myself. Coming from a PHP background probably biases me though. It is just easy to create an html template with inserted values an minimal control flow using the <%...%> style.
- Void_ 15y agoMy biggest question is: Is rendering faster too? Because rendering is pretty much just string concatenation. So my guess is it's not. Is it? If rendering is not faster then this is no use to me, since I'm a very happy user of haml-coffee (https://github.com/9elements/haml-coffee https://github.com/9elements/haml-coffee).
- rhizome 15y agoSince it happens on the client, it will be faster for Twitter this way. Not so much for the user, though.
- asktell 15y agoNice to see that the hogan.js site itself is using Skeleton ( http://getskeleton.com/ http://getskeleton.com/ ) for its responsive layout. I've been enjoying using it quite a bit.
- nbclark 15y agoLooks a lot like jquery tmpl -- the syntax at least
- Maascamp 15y agoI was wondering if anyone was going to mention that. If you're already working with jQuery I would think the tmpl extension would be a much better choice.
- bentlegen 15y agoThe jQuery tmpl plugin has been deprecated / put "on hold": http://www.borismoore.com/2011/10/jquery-templates-and-jsviews-roadmap.html http://www.borismoore.com/2011/10/jquery-templates-and-jsvie...
- Lazare 15y agoSo let's recap: This is a new Javascript template engine that uses Mustache syntax and allows you to pre-compile template syntax. Sound's good... But...why didn't they just use Handlebars? Or if you aren't wedded to Mustache syntax, one of the twenty other template engines out there that support pre-compiling?
- tjholowaychuk 15y agohandlebars is a pretty inelegant lib, a parser generator for a grammar as simple as mustache is just silly, even this current twitter implementation could be significantly smaller. For large deployments like Twitter I imagine these savings are quite important
- wycats 15y agoThe Handlebars grammar is more complex than Mustache's grammar. It supports helpers (normal and block), paths, strings, numbers and booleans. My initial implementation of Handlebars did not include a proper compilation flow, but optimizing its output became very complicated. The new approach (the one used for the past year or so in Handlebars) makes it easier to reason about how the input gets compiled into JavaScript, and each stage in the compilation process provides ways to perform certain kinds of optimizations. One of the major goals of Handlebars is for users to avoid paying for features they are not using. For instance, the ".." feature, which allows a template to reference a parent context, only impacts the compiled code if it's used. Finally, for performance-sensitive deployments, Handlebars provides a precompiler that eliminates the need to compile the template on the client. This eliminates the compiler requirement on the client, and only requires a small (1k'ish) library at runtime.
- drewblaisdell 15y agonpm ERR! 404 'hogan' is not in the npm registry.