Hamlet – Simple and powerful reactive templating
hamlet.coffee
hamlet.coffee
converter = new Showdown.converter()
MarkdownEditor = React.createClass
getInitialState: () -> value: 'Type some *markdown* here!'
handleChange: (e) -> @setState(value: e.target.value)
render: () ->
d = React.DOM
d.div(
d.h3(null, 'Input'),
d.textarea(onChange: @handleChange, value: @state.value),
d.h3(null, 'Output'),
d.div(dangerouslySetInnerHTML: __html: converter.makeHtml(@state.value))
)
React.renderComponent MarkdownEditor(), document.querySelector('.container')
Showing a LOC comparison (and not even from the same dialect of a language) isn't a good proof for what you're trying to demonstrate. Clarity, simplicity, and debuggability all count. Removing a few extra lines of (non-boilerplate) code compared to React doesn't make the library simpler to work with.We have a strong preference for Haml and CoffeeScript dialects so that's how we present our demos. I built jsfiddles based on how each framework presented their own product.
Also, React is a view library, not a full-stack framework. The fact that it's put on the same level as other real JS frameworks is an acknowledgment of its paradigm's power and usefulness. So I guess people viewing it as a framework is a misleading but very telling phenomenon.
That being said, good luck with the project!
Of which there are at least two.
http://www.yesodweb.com/book/shakespearean-templates
https://github.com/gregwebs/hamlet.rb
It does look pretty cool though.
In my defence, I always thought the main feature of Hamlet is the static checking of everything, and solving the escaping problem (at compile time!), so I didn't even entertain the idea that someone would port it to Ruby.
I have a few questions:
1. Its interesting to see JS events specified in the template (e.g. `%a(onclick=@doSomething)`). Is there a way to specify that in the JS/model?
2. Does "Observable" mean that the value is updated when the model changes, when the DOM changes, or both? Could all of the attributes of the object passed into the template be observable by default or would that incur a significant performance penalty?
As to point two, observable provides a bi-directional binding so that changes to the value are reflected in the DOM and changes in the DOM are reflected in the model.
The observable interface is essentially a jQuery style getter/setter method that allows for observers to be notified of changes.
Looks very nice, just this weekend I looked at some frontend frameworks, including Vue, Mithrill, Ractive and React and found some issues with them all. Hamlet would fit my use case very well, I think. One thing I'd like is more technical details on the main page - not how it "looks like" compared to other frameworks, but how it does its thing (compared to other frameworks).
The Observable component invokes the callback to all listeners immediately when its value is changed. I find this makes testing and debugging somewhat easier than async callbacks or nextTick.
list = Observable []
list.push 'blah'
and isOrange = Observable false
isOrange = true // This obviously doesn't work this way...
Do you poll the value? Or do you check the type of the value and wrap/proxy methods that would normally change the value?The wrappers also set up automatic dependency resolution so you can do cool things like:
list = Observable []
reversed = Observable ->
list.map reverse
list.push 'blah'
reversed() # => ['halb']
There's no polling, the observable notifies all observers immediately when the value changes. isOrange(false)This is a pretty lame claim seeing as text fields are busted in Hamlet. [1]
This is another example of a "lightweight" library that hasn't hit any of the hard problems yet. It's fine if you make this your personal project to learn from, but trying to convince people to bet their projects on unproven technology is pretty disingenuous.
[1] Inserting characters does not work in the second example at http://hamlet.coffee/garden/
One thing I would love on HN is the reluctance to say something is "lame" because one example doesn't work for one person on one browser. Instead of being dismissive, why not be constructive? Or if you can't even do that, just don't post?
But "lightweight" JS fetishism at the expense of correctness (and the arrogance that comes with it!) is an epidemic in the frontend web business today. I think it's important to highlight this when it happens so we can stop making crappy web apps and start to actually deliver reasonable experiences when compared to native.
Also here is a video of the bug: http://www.petehunt.net/hamlet.mov
That said, and with lots of respect for your work on React Pete, and also acknowledging that React is probably the simplest of the big frameworks, the love of minimalism and simplicity comes from a very real need and attempts to fulfill that need shouldn't be dismissed as toy projects. From what I can tell, Mithril, for example, is anything but that.
I think people really really like being able to jump into something that can be useful and played with based on a few examples, and to model things with POJOs. I agree correctness shouldn't be sacrificed, but it doesn't have to be.
My complaint is you see a lot of upstart projects pulling mindshare when the reason they're simple is either a matter of personal preference or a lack of essential complexity. Hamlet was an easy target because cursor position management is a great example of essential complexity and they made pretty aggro claims on their site about how over-engineered everything else is.
I can't get the video to work on my machine.
When opening it with the default video player I get:
An error occurred
This file contains no playable streams.
System info: ♥ uname -a
Linux yolobookjr 3.8.0-35-generic #50-Ubuntu SMP Tue Dec 3 01:24:59 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux
Looks like a lot of software still has room to improve. :)We don't expect to have something as polished as React yet. Early feedback is all we can hope for so we can iterate on what we believe is an innovative approach to a problem that many devs have.
I apologize if it's a little aggressive, it's not our intent to belittle the work of others. We'll try to find the most constructive ways to express the tradeoffs between tools as we continue to refine our project and message.
Thanks again for the feedback, sorry if I come off as an ass sometimes.
Could you let me know which browser and version you're using so I can get to the bottom of the problem?
What browser and OS are you using? The examples have worked in our testing environments of Chrome, FF, IE9+, Safari, and Opera.
The parser is separate from the compiler and the runtime, so it should be simple enough to add a jadelet, anglet, or any other simple style of adapter.
If there is a lot of interest in a jade focused style or haml is a turn off for many people then it will become a priority for us.
I love that it seems to not be tied to node out of the box as well.
One of our goals was for Hamlet to be suitable for really small and simple web apps, without any big framework or ecosystem. There's still a lot of work for us to do on the ease of getting started (both with or without node) so if you run into trouble or have any comments let us know.
First, Blaze does not require to set a root element in a template, which could be a source of bugs with Hamlet because for instance the `each` child is a template, here is a snippet of problematic example from the Hamlet README:
- each @items, ->
.first
.second
This works perfectly fine in Blaze. IIRC Blaze uses comments node on the DOM that are never rendered in browsers in order to define some "domrange" that keep track of n children in a single parent group.The second runtime issue in Hamlet appears when a third-party library directly modifies the DOM, without telling the template engine. Basically the modification will be erased on the next template redraw which make this system incompatible with all jQuery plugins for instance. Blaze has "fined grained DOM updates" which mean that the modification of a single element in a template does not require to touch any other node in the DOM. For instance if you have a each loop of inputs, and the user start to enter some data in one input field, and for some reason the template is redrawn the text will stay in the input with Blaze, but will be erased with Hamlet.
Blaze also support reactive SVG (I'm not sure if Hamlet supports it but I haven't seen any particular mention in the code).
I think all of these features can be implemented in Hamlet drawing on Blaze and ReactJS runtimes.
Nevertheless I find the Javascript model declaration cleaner in Hamlet than in Blaze or Backbone or React. The only thing I'm not sure about is writing the js events in the template and not in the model, I actually like having all events of a given template in a single place but I don't have strong opinion on this.
[0]: Meteor support Spacebars (which is quite similar to Handlebars) by default https://github.com/meteor/meteor/blob/devel/packages/spaceba..., and there is also a package for jade https://github.com/mquandalle/meteor-jade (disclaimer: I'm the author). It also seems that it wouldn't be difficult to support other languages than Haml for Hamlet.
For the most part jQuery plugins should work fine with Hamlet, so long as one remembers to update the data in the model rather than arbitrarily throughout the DOM. It can be a moderate mental shift to go from jQuery style "The DOM is the data" to the newer Backbone, Knockout, React, Angular, etc style of "The model is the data" and may not be right for all applications.
Thanks for the comment I'll take a look at Meteor Blaze and see what cool tricks it has :)
I'm not sure if it provides as much magic as Hamlet's auto-dependencies and template syntax, but those do have costs and tradeoffs.
I would like to see an interactive demo on the site so I could get to know it better by messing around.
You could do it in jQuery, but plumbing all the update code for each input would be kind of a chore. If you get a new source of input (say from the network) you'd have to remember to resync everything. To keep it simple you'd probably have to respond to `something changed` -> `update everything`. Reactive templates would only update the elements dependent on that change.
With a reactive solution you make a model that contains observable RGB values and a function to compute the final color. Once you bind that model to your view everything stays in sync like magic.