Haskell

The meeting place for professional Haskell programmers
You are not logged in. In order to participate in Special Interest Groups, you should log in now .

more html-like hamlet syntax (Discussion)

Created by Greg Weber on December 8, 2010 05:01:22 PM.
Status: Resolved

Messages

By Greg Weber @ December 8, 2010 05:14:19 PM
I am beginning to think that while HAML and hamlet have the right idea, the actual hamlet syntax is more noisy than is required. In particular, I would much rather have white space (and quotes where needed) than exclamation marks. Here is one possible alternative: https://github.com/stonean/slim

I actually think the ideal syntax would allow compatibility with existing html- this has always been the main weakness of these newer systems. And especially until a proper converter is built, this will always be a point of pain. Compatibility would also help remove part of the criticism that Yesod is off in its own little world.

Instead of '%body' why not '<body'. A closing angle bracket is allowed for the tag- '<body>'.  The 'attribute="name"' style would be kept although the quotes are optional- '<body class=page1',  or '<body class="page1">' would also be acceptable for compatibility and closing over white space. Of course, the id and class shortcuts would be available- '<body.page1'

Content on the same line would use a closing angle bracket- '<body class=page1>content

If the previous tag has no inner text on its line and the next indented line does not start with a '#', '<', or '.' then it is the inner text of the previous tag.

<body.page1
  text
  #id.class

Alternatively, a div with a class or id could still require the opening angle bracket- '<#id.class'
<body.page1
  text
  <#id.class

And the advantage is that it looks much more similar to html, and has easier compatibility. Given
<body class="page1">
  text
  <div id="id" class="class"></div>
</body>

The only thing that needs be done for the html to be parsed is consistent indentation and removal of closing tags. However, for the div above it would be possible to even keep the closing tag. It is up to the developer whether to spend the time to compact things even more once the html can be parsed.

The other syntax that I like is removing the opening angle bracket and requiring an '=' to denote where text is- the main problem is that this is a divergence from normal html, and working in html compatibility would be more complex.

body.page1
  = text
  #id.clas
By Michael Snoyman @ December 9, 2010 05:06:35 AM

It's an intriguing syntax. One thought would be to offer both styles of Hamlet with different function names. However, that runs the risk of creating two different Yesod communities. Obviously, switching syntax entirely will completely break old Yesod applications, but especially with external Hamlet templates it should be a piece of cake to write a converter.

Overall, I like, though I haven't spent a huge amount of time thinking about it. I think it would be good to get broader community input on this; you think you could send an email to the web-devel list either linking to this page or copying its content?

By Greg Weber @ December 9, 2010 01:38:01 PM
For a syntax switch it could start with modifying the first line: "!!! new syntax"
If everybody likes it, a new version of Hamlet could be released with the new syntax as default and a modification would be required to use the old version: "!!! old syntax".
But widgets would seem to require 2 functions.
Fortunately the community is not huge yet and Yesod is not 1.0- now is the time for breaking changes. I see this as an opportunity to grow the community by getting people over 1 of their objections.

I will post this on web-devel
By Michael Snoyman @ February 6, 2011 09:12:53 AM
Just to update everyone: Hamlet 0.7 introduces a brand new syntax.
By pacak @ March 3, 2011 07:32:04 PM
This brand new syntax looks kind of ugly. I think I'll start splitting yesod community by forking hamlet to work with original syntax...
By Greg Weber @ March 6, 2011 07:55:48 PM
Hi pacak, a fair number of experienced html programmers have an initial reaction similar to yours. It is useful to hear about emotional reactions even if there is normally not much we can do about that.

I am wondering if you have this reaction based on looking at this message or the Yesod book or in a scaffolded site? Personally, I have found that the font and syntax highlighting make a big difference in how nice hamlet looks to me, and I have been thinking about changing the font in the book.

Have you actually tried creating a layout and pages with the new style? I understand that it appears ugly at first to some, but once you start using it, the perspective changes. That is the perspective we are most concerned about.


By pacak @ March 13, 2011 01:03:20 PM
I have a lot of experience working with both real HAML and HTML. Also I have some background in PHP, but mostly in Ruby on Rails. And now i am looking for web framework in haskell to use in my project. So yes, I tried to read Yesod book, checked some examples. This new aproach took worst parts from both HTML (all those <> stuff) and HAML (incompatibility most templates you can find on the web, tools and all other stuff).
By pacak @ March 13, 2011 01:06:06 PM
It rather inconvinient to type <> chars. Actually we can move this discussion to [email protected] maillist. Something makes me think that i am not alone :)
By Greg Weber @ March 13, 2011 02:26:38 PM
Hi pacak,

I thought you were probably a HAML user. 
If you want a productive conversation you are going to have to spell out your complaints more specifically then "<> stuff", and give an actual example of what you are writing in haml vs. what you would like to be writing. Otherwise we can only interpret this complaint as: "its not HAML!".

It would be nice to re-use HAML tooling. However, we did not want that constraint on our design. Also, because our syntax is html based, we get to take some advantage of normal html. It is much quicker to copy and paste a short HTML snippet by hand then in HAML. HTML syntax highlighters can actually still work to some effect on hamlet also, particularly if you include the '>' attribute.

Note that the book generally quotes attributes, which is unecessary unless the attribute contains white space- we will be updating the book to reflect that. Also, it may not be obvious that the '>' character is optional if there is no text inside the tag. We should modify the book if any of these points were unclear to you.

I have used and liked HAML myself. My main problem with HAML has always been their ridiculously verbose attribute syntax, which they improved by adding a second way of achieving that with parentheses- that way basically copies html! I recognized that HTML is actually less verbose than HAML attribute syntax, and that was the main motivation for going back to a more html like syntax. Actually, the original hamlet used '!' to separate attributes, which avoided the need for a closing '>', but going back to html white space attribute separation made a great improvement.


By pacak @ March 13, 2011 07:34:53 PM
As you said - it is not haml. In my RoR project most of haml code looks like this: .navigation = render :partial => "shared/tree_select" %span Structure: = link_to "Departments", departments_path | = link_to "Users", users_path It can be typed pretty fast using some keyboard shortcuts and so on. Hamlet uses <> symbols to separate tags so it will probably look like this <.navigation @[[ render_partial "/shared/tree_select" ]] Structure: @[[ link_to "departments", departments_path ]] | @[[ link_to "users", users_path ]] So there are more keypresses (located in inconvinient places - <>[], and will be more of those brackets when you will write more complicated layout: %table %thead %tr %th= User.human_attribute_name :cn %th= User.human_attribute_name :uid %th{:colspan => 3}= t 'actions' Syntax for attributes in HAML are more verbose, yes, but out of 67 haml templates i found those attributes only in 9 places and in each of those places it can be avoided using a bit different layout. on the other hand there was about 500 of %tag stuff, so looses from verbose syntax for attributes are 10 times less than those for angle brackets. { :foo => "bar" } is native syntax for hash tables in Ruby, so in haskell version of haml it probaby can be extended to support both ruby and haskell notation { foo = "bar" } and { :foo => "bar" }. In ruby those attributes are not only string/number values, but any valid ruby code: %th{:colspan => 3 + 1}= t 'actions'
By pacak @ March 13, 2011 07:36:42 PM
*sigh* You can view source to view original code.
By Greg Weber @ March 14, 2011 02:47:41 PM
Can you use hpaste or something like that? View source will have its own problems.

The double bracket syntax was used to get around haddock description limitations- that is not correct. Hamlet uses a single left and right curly bracket. This tells me we should remove that description and put a link to correct Hamlet.

This discussion has alerted me to some documentation issues I am cleaning up and the need to try to make a better first impression. However, It is very clear that you haven't actually used hamlet- please at least use it in a Yesod app for a layout and a few pages, and your feedback will have much more impact.
By pacak @ March 14, 2011 05:26:37 PM
Well.. I tried to use it and simple did not get to that part when you can add any non-static elements. Probably i will try it once more when all documentation will be finished, but i am already almost half way through my brand-new-haml-99%-complete template language.

Login with