tags:

views:

260

answers:

5

I have repetitious markup for elements, like:

<div class="box">
    <div class="top"></div>
    <div class="content">
      content goes here
    </div>
    <div class="bottom"></div>
</div>

Top and bottom are styled with css to include images for borders.

I could use JQuery to inject tags, but is there a better way to template controls in asp.net mvc?

Thank you

A: 

As far as I know there is no Built-In templating solution in asp.net mvc. But because the output is pure html it is very easy to do your own templating with just css and maybe a bit of JS.

Of course since everything in asp.net mvc is swappable you can replace the default view engine with one that supports templating.

Sruly
Am I thinking more along the lines of a Server Controls for mvc
blu
View engines are really template engines. See http://stackoverflow.com/questions/340095/
Mauricio Scheffer
A: 

Why don't you use master pages? You can use master pages in MVC just like in ASP.Net

Jeroen Landheer
+1  A: 

I just defined two extension methods to write the tags

  public static void BeginBox(this HtmlHelper htmlHelper)
  {
      StringBuilder box = new StringBuilder();

      box.AppendLine("<div class=\"box\">");
      box.AppendLine("    <div class=\"top\"></div>");
      box.AppendLine("        <div class=\"content\">");

      htmlHelper.ViewContext.HttpContext.Response.Write(box.ToString());
  }

  public static void EndBox(this HtmlHelper htmlHelper)
  {
      StringBuilder box = new StringBuilder();

      box.AppendLine("        </div>");
      box.AppendLine("    <div class=\"bottom\"></div>");
      box.AppendLine("</div>");

      htmlHelper.ViewContext.HttpContext.Response.Write(box.ToString());
  }

I guess this will do for now.

blu
why not have the methods return the html in a string?
Sruly
+2  A: 

There are probably a lot of ways around this, here's one:

Create a ViewUserControl (let's call it "box.ascx"):

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<Action>" %>
<div class="box">
    <div class="top"></div>
    <div class="content">
        <% Model(); %>
    </div>
    <div class="bottom"></div>
</div>

In your aspx, wherever you need this block, call it like this:

<% Html.RenderPartial("box", Lambda.Action(() => { %>
here comes my content! <a href="http://www.google.com"&gt;Google!&lt;/a&gt;
<% })); %>

Here's the Lambda helper class:

public static class Lambda {
    public static Action Action(Action a) {
        return a;
    }
}

If you don't use this little helper it will crash since it will try to cast the Action.

Mauricio Scheffer
+1  A: 

You definitely want to use an extension method to the HtmlHelper so that you can then use the notation:

<%=Html.Box("content string") %>


public static stringBox(
       this HtmlHelper html, 
       string Content)
  {
      var sb = new StringBuilder();

      sb.AppendLine("<div class=\"box\">");
      sb.AppendLine("    <div class=\"top\"></div>");
      sb.AppendLine("        <div class=\"content\">");
      sb.AppendLine(Content);
      sb.AppendLine("        </div>");
      sb.AppendLine("    <div class=\"bottom\"></div>");
      sb.AppendLine("</div>");

      return sb.ToString();
  }
Richard
This would get quite ugly if the "content string" is actually html instead of just text, or if I wanted to include calls to Html.TextBox() and such.
Mauricio Scheffer
True but you can use the other extension methods (beginBox and endBox) shown blu's answer for that circumstance.
Richard
If you code both solutions then you'd have the markup duplicated... it would be in blu's extensions and then again in your extension.
Mauricio Scheffer