Feature proposal: Templated Helpers

Coordinator
Oct 7, 2010 at 9:10 AM

Based on this thread, I am thinking of providing a set of HtmlHelper extensions that are "templateable". To get everyone in the right mindset, here is an example on how this could be used.

1. In my Master page, I include a breadcrumb trail (SiteMapPath) like this:

<%=Html.MvcSiteMap().Templated().SiteMapPath("TemplatedSiteMapPath")%>

2. I create a partial view "TemplatedSiteMapPath.ascx", which I referenced in the call above in my master page. This is what it looks like:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<TemplatedSiteMapPathHelperModel>" %>

<% foreach (var node in Model) { %>
    <%=Html.DisplayFor(m => node)%>
    <% if (node != Model.Last()) { %>
        &gt;
    <% } %>
<% } %>

3. I create a DisplayTemplate for each "node" that is rendered in the above artial view. Here's "DisplayTemplates/TemplatedSiteMapNodeModel.ascx":

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<TemplatedSiteMapNodeModel>" %>

<% if (Model.IsActive) { %>
    <%=Model.Title %>
<% } else if (Model.IsClickable) { %>
    <a href="<%=Model.Url %>"><%=Model.Title %></a>
<% } else { %>
    <%=Model.Title %>
<% } %>

This will all behave exactly like before, but you as a developer/designer get ful lcontrol over the HTML markup that is being generated.

My question to you: is this a good direction? Should this be incorporated in MvcSiteMap or not? Start the discussion :-)

Oct 7, 2010 at 12:08 PM

Yes this is a good direction!