First time here? You are looking at the most recent posts. You may also want to check out older archives or the tag cloud. Please leave a comment, ask a question and consider subscribing to the latest posts via RSS. Thank you for visiting! (hide this)

NHaml, an alternative view engine for ASP.NET MVC written by Andrew Peters, uses indentation instead of opening and closing tags to identify code blocks.

If you never saw something written in NHaml here is taste of it. If you want to loop over a list and put it inside a unordered list with webform you would write:

<div class="list">
  <ul>
    <%foreach (var route in ViewData.Model) { %>
      <li><%= route.Name %></li>
    <% } %>
  </ul>
</div>

The same code in NHaml will be:

.list
  %ul
    - foreach (var route in ViewData.Model)
      %li
        = route.Name

The biggest problem of NHaml is that it’s behavior depends on the number of spaces you put at the beginning of the line: one level of indentation is made by 2 whitespaces.

Yesterday Andrew wrote a post on his site announcing the beta of NHaml version  2, and a few people, including me, complained that the default indentation of Visual Studio is 4 spaces, but someone even uses tabs. So, he made quick change in the code, and now NHaml features a configurable indentation (2, 3, 4, 5 spaces or even tabs). A very quick response to users’ requests, well done Andrew.

Technorati Tags: ,
posted on Tuesday, November 18, 2008 10:28 AM

Comments on this entry:

# re: Configurable indentation for NHaml

Left by Phillip at 11/18/2008 11:51 PM

Wow, I hate ASP.Net MVC since it's spaghetti code...

But NHaml looks completely un-readable, how is a designer meant to come along and style that? They can't.

ASP.Net MVC is a step backward in terms of integration with design, but NHaml looks like a giant leap backward...

# re: Configurable indentation for NHaml

Left by Simone at 11/20/2008 9:58 AM

@Phillip: Spaghetti code is as Spaghetti code does.
With MVC you can use Html Helper to encapsulate some conditional statements for your UI or other View Engines if you don't like mixing <% %> areas with HTML tags.
Designers learned to cope with <asp:repeater> tags: it's only a matter of making mind shift to <% foreach %> ones.

A for NHaml, I agree with you: it's impossible for a designer to write the page with it. In fact it is a DSL from writing html for people (devs) that don't like HTML.

If you don't like traveling by bike, none will force you to, you can go on by car :)

# re: Configurable indentation for NHaml

Left by Phillip at 11/20/2008 10:23 AM

Where i work we have a style guide, it covers the in between where a design doesn't need to know asp.net, but knows enough about controls to style them, while a developer doesn't need to know HTML, but knows enough to know which controls to use so the designer can do his job.

That means a lot of what the developer uses is PlaceHolders, Literals, and Repeaters, since they provide absolutely no mark up at all. Not only does it make it piss easy to create a validate xhtml website, its piss easy for the designer to write the markup.

With MVC your mixing the actual code with the html. Not only that there needs to be basic knowledge of the HTML Helper namespace in order to add additional class's to some elements.

So MVC regardless of its html helper or such, results in spaghetti code, which as i mentioned, is a step backward.

MVC would be a LOT better if we could make proper use of the code behind and only put containing controls on a aspx page so we still have the seperation.

The only benefit MVC provides is for developers who couldn't figure out how to seperate presentation from logic from business and data, since it basically enforces it.

# re: Configurable indentation for NHaml

Left by Simone at 11/20/2008 2:07 PM

...developer doesn't need to know HTML
How can they develop web applications if they don't know HTML? How can they use javascript?

...developer uses is PlaceHolders, Literals, and Repeaters, since they provide absolutely no mark up at all.
Seems like you are not using any of the web-form features at all.
MVC is the same, but instead of having a <asp:literal id="userName" runat="server"> that you populate from the codebehind, here you have <%= model.UserName %> or <%= ViewData["UserName"] %>.
Not much difference.
And here you don't even have to write HTML tags, only their contents.

Not only that there needs to be basic knowledge of the HTML Helper namespace in order to add additional class's to some elements.
HtmlHelpers are, as the word implies, just helpers that helps you rendering some code.
But, first thing, they only render basic html, not the ugly html rendered by the more advanced web-form server controls.
Second, you don't have to use the helper if you don't want: you can just write straight HTML, so a designer can think he is doing just static html pages.

The only benefit MVC provides is for developers who couldn't figure out how to seperate presentation from logic from business and data, since it basically enforces it.
This alone is huge step forward as most of the developers, when given enough freedom, tend to put everything together: enforcing a clearer separation of concerns helps making good design by default.

And, all-in-all, if you don't like it, you can go on and develop with webforms: none is going to force you to adopt the MVC framework.

# re: Configurable indentation for NHaml

Left by Phillip at 11/20/2008 10:14 PM

How can they develop web applications if they don't know HTML? How can they use javascript?

Maybe saying they don't need to know HTML is the wrong way of putting it.
I've been doing HTML for almost 9 years now and i loath doing it. I would rather not have to worry about it which is easy to do when i can just put the required controls on the page.
Seems like you are not using any of the web-form features at all.

I do use some of them but a lot of them are not needed. For example why would you ever want to use an ASP Label? It renders a span tag and an ID...

Or why would you use an ASP Panel to hide/show content which renders a div with an ID...

Most developers use controls out of laziness, they are the same people who don't seperate their projects.

Every MS Conference i've been to has demos where they drag and drop controls like it isn't a problem... I see people at work drag and drop those controls for public facing websites, because all they know if that control will put data there for them, not why or how, and no knowledge of the output.

The benefit of MVC is you dont get all those draggy droppy controls, you actually have to go write a few lines of code to do what you need to do. However i don't agree that MVC is the way to go about writing ASP.Net Web Apps. I think we need a better source of tutorials on the interweb for these people to learn from, so they don't learn such bad habbits.

HtmlHelpers are, as the word implies, ju...


Fair enough.

not the ugly html rendered by the more advanced web-form server controls

Again i think this comes down to not knowing the controls to begin with, or where and what to use. In any public facing site it would be rare to ever use a DataGrid or GridView. By default paging requires a postback so it is not crawlable by search engines. So they shouldn't be used. The same goes to the CSS friendly adapters, the paging requires a postback.

(better stop here i got to get to work, im late now :)

Don't get me wrong, MVC is great for bringing people over from the likes of PHP or Ruby who like the MVC pattern, and for people who can't workout how to seperate their work. But it doesn't provide any benefit to those who can write good webforms app's to begin with. It does create spaghetti code. And its all my favourite blogs i used to read, seem to be only ever posting MVC related information now. Im slowly unsubscribing from them all :(

Comments have been closed on this topic.