Go to the documentation of this file.00001 using System;
00002 using System.Text;
00003 using System.Web.Routing;
00004 using N2.Collections;
00005 using N2.Web.UI;
00006 using System.Web.Mvc;
00007 using N2.Web.UI.WebControls;
00008 using System.IO;
00009
00010 namespace N2.Web.Mvc.Html
00011 {
00012 public class ZoneHelper : ItemHelper
00013 {
00014
00015 public ZoneHelper(HtmlHelper helper, string zoneName, ContentItem currentItem)
00016 : base(helper, currentItem)
00017 {
00018 ZoneName = zoneName;
00019 }
00020
00021 protected System.Web.Mvc.TagBuilder Wrapper { get; set; }
00022
00023 protected string ZoneName { get; set; }
00024
00025 public ZoneHelper WrapIn(string tagName, object attributes)
00026 {
00027 Wrapper = new System.Web.Mvc.TagBuilder(tagName);
00028 Wrapper.MergeAttributes(new RouteValueDictionary(attributes));
00029
00030 return this;
00031 }
00032
00033 public override string ToString()
00034 {
00035 var partialResult = new StringBuilder();
00036 using (var writer = new StringWriter(partialResult))
00037 {
00038 Render(writer);
00039 }
00040 return partialResult.ToString();
00041 }
00042
00043 public virtual void Render()
00044 {
00045 Render(Html.ViewContext.HttpContext.Response.Output);
00046 }
00047
00048 public virtual void Render(TextWriter writer)
00049 {
00050 foreach (var child in PartsAdapter.GetItemsInZone(CurrentItem, ZoneName))
00051 {
00052 RenderTemplate(writer, child);
00053 }
00054 }
00055
00056 protected virtual void RenderTemplate(TextWriter writer, ContentItem model)
00057 {
00058 if (Wrapper != null)
00059 writer.Write(Wrapper.ToString(TagRenderMode.StartTag));
00060
00061 var adapter = Adapters.ResolveAdapter<MvcAdapter>(model);
00062 adapter.RenderTemplate(Html, model);
00063
00064 if (Wrapper != null)
00065 writer.WriteLine(Wrapper.ToString(TagRenderMode.EndTag));
00066 }
00067 }
00068 }