Art Of Programming

musings by Dmytrii Nagirniak

NHAML – Tips

I’ve working with NHAML for some time. I just love it.
Now I want to share some issues that I have faced and how I solved them.
This is small Question/Answer list:

Q1: What is NHAML analogue of
<%= Html.ActionLink("Home", "Index", "Home")%>

A1: != Html.ActionLink(“Home”, “Index”, “Home”)

Q2: What is NHAML analogue of
<%= Server.HtmlEncode(Model.Name) %>
A2: &= Model.Name

Q3: What is analogue of
<a href='<%= Html.Content().HomeLink %>' target=’_blank’>Home</a>
A3: More complex code should be wrapped in #{}:
%a { href=#{Html.Content().HomeLink} target=”_blank”} Home
BUT see Q4

Q4: What is analogue of
<a href='<%=Model.ProductUrl %>' target='_blank'><%= Server.HtmlEncode(Model.ProductName) %> </a>
A4: Simple code may not be wrapped in anything:
%a { href=Model.ProductUrl target=”_blank”} &= Home

Q5: What is analogue of:
<% foreach(Product currentProduct in Model.Products) {%>
  <% Html.RenderPartial("ProductInfo", currentProduct); %>
<% }; %>
A5: Local variables (currentProduct) are available in partial views (_ProductInfo):
- foreach(Product currentProduct in Model.Products)
  _ ProductInfo

Q6: What about HTML encoding?
A6: See the reference for more info. Shortly: if you want to encode – use “&=”, if you don’t – use “!=”.

Q7: Can I use WebForms View engine and NHAML?
A7: Yes. No problem with that. In fact I do it as a fallback in case something goes wrong (hope it will never happen :) ).

Q8: Any integration with Visual Studio
A8: At the moment I am aware of Visual Studio syntax highlighter plug-in only .

This should be enough for now :)