public bool TryRender(RenderingContext context, TextWriter writer)
		{
			var renderer = ResolveRenderer(context.Displayable.GetType());
			if (renderer == null)
				return false;

			renderer.Render(context, writer);
			return true;
		}
        public void Render(RenderingContext context, TextWriter writer)
        {
            var tw = WebExtensions.GetEditableWrapper(context.Content, context.IsEditable, context.PropertyName, context.Displayable, writer);

            using (tw)
            {
                var renderer = ResolveRenderer(context.Displayable.GetType());
                renderer.Render(context, writer);
            }
        }
Exemple #3
0
		public static RenderingContext Create(HtmlHelper html, string propertyName)
		{
			var context = new RenderingContext();
			context.Content = html.CurrentItem();
			var template = html.ResolveService<ITemplateAggregator>().GetTemplate(context.Content);
			if (template != null)
				context.Displayable = template.Definition.Displayables.FirstOrDefault(d => d.Name == propertyName);
			context.Html = html;
			context.PropertyName = propertyName;
			return context;
		}
        public override Control AddTo(ContentItem item, string detailName, Control container)
        {
            using(var sw = new StringWriter())
            {
                var rc = new RenderingContext { Content = item, Displayable = this, Html = CreateHtmlHelper(item, sw), PropertyName = detailName };
                Render(rc, sw);

                var lc = new LiteralControl(sw.ToString());
                container.Controls.Add(lc);
                return lc;
            }
        }
        internal void Render(RenderingContext context, TextWriter writer)
        {
            string text = context.Content[context.PropertyName] as string;
            if (text == null)
                return;

            var tokens = context.Content.GetDetailCollection(context.PropertyName + "_Tokens", false);
            if (tokens != null)
            {
                int lastFragmentEnd = 0;

                foreach (var detail in tokens.Details)
                {
                    if (lastFragmentEnd < detail.IntValue)
                        writer.Write(text.Substring(lastFragmentEnd, detail.IntValue.Value - lastFragmentEnd));

                    string tokenTemplate = detail.StringValue.TextUntil(2, '|', '}');

                    ViewEngineResult vr = null;
                    try
                    {
                        vr = ViewEngines.Engines.FindPartialView(context.Html.ViewContext, "TokenTemplates/" + tokenTemplate);
                    }
                    catch (System.Exception ex)
                    {
                        Trace.WriteLine(ex);
                    }
                    if (vr != null && vr.View != null)
                    {
                        var model = detail.StringValue[tokenTemplate.Length + 2] == '|'
                            ? detail.StringValue.Substring(tokenTemplate.Length + 3, detail.StringValue.Length - tokenTemplate.Length - 5)
                            : null;
                        var vc = new ViewContext(context.Html.ViewContext, vr.View, new ViewDataDictionary(model) { { "ParentViewContext", context.Html.ViewContext } }, context.Html.ViewContext.TempData, writer);
                        vr.View.Render(vc, writer);
                    }
                    else
                        writer.Write(detail.StringValue);

                    lastFragmentEnd = detail.IntValue.Value + detail.StringValue.Length;
                }

                if (lastFragmentEnd < text.Length)
                {
                    writer.Write(text.Substring(lastFragmentEnd, text.Length - lastFragmentEnd));
                }
            }
            else
            {
                writer.Write(text);
            }
        }
		internal void Render(RenderingContext context, TextWriter writer)
		{
			string text = context.Content[context.PropertyName] as string;
			if (text == null)
				return;

			var tokens = context.Content.GetDetailCollection(context.PropertyName + CollectionSuffix, false);
			if (tokens != null)
			{
				int lastFragmentEnd = 0;

				foreach (var detail in tokens.Details)
				{
					var token = detail.ExtractToken();

					if (lastFragmentEnd < token.Index)
						writer.Write(text.Substring(lastFragmentEnd, token.Index - lastFragmentEnd));

					ViewEngineResult vr = null;
					if (context.Html.ViewContext.HttpContext.IsCustomErrorEnabled)
					{
						try
						{
							vr = ViewEngines.Engines.FindPartialView(context.Html.ViewContext, "TokenTemplates/" + token.Name);
						}
						catch (System.Exception ex)
						{
							logger.Error(ex);
						}
					}
					else
					{
						vr = ViewEngines.Engines.FindPartialView(context.Html.ViewContext, "TokenTemplates/" + token.Name); // duplicated to preserve stack trace
					}

					if (vr != null && vr.View != null)
					{
						var viewData = new ViewDataDictionary(token.Value) { { "ParentViewContext", context.Html.ViewContext } };
						viewData[RenderingExtensions.ContextKey] = context;
						viewData[RenderingExtensions.TokenKey] = token;
						var vc = new ViewContext(context.Html.ViewContext, vr.View, viewData, context.Html.ViewContext.TempData, writer);
						vr.View.Render(vc, writer);
					}
					else
						writer.Write(detail.StringValue);

					lastFragmentEnd = token.Index + detail.StringValue.Length;
				}

				if (lastFragmentEnd < text.Length)
				{
					writer.Write(text.Substring(lastFragmentEnd, text.Length - lastFragmentEnd));
				}
			}
			else
			{
				writer.Write(text);
			}
		}
Exemple #7
0
		private void RenderDisplayable(TextWriter writer)
		{
			var displayable = DefinitionMap.Instance.GetOrCreateDefinition(CurrentItem).Displayables.FirstOrDefault(d => d.Name == propertyName);

			if (displayable == null)
			{
				if (isOptional || swallowExceptions)
					return;

				throw new N2Exception("No attribute implementing IDisplayable found on the property '{0}' of the item #{1} of type {2}", propertyName, CurrentItem.ID, CurrentItem.GetContentType());
			}

			if (Wrapper != null)
				writer.Write(Wrapper.ToString(TagRenderMode.StartTag));

			var ctx = new RenderingContext
			{ 
				Content = CurrentItem, 
				Displayable = displayable, 
				Html = Html, 
				PropertyName = propertyName, 
				IsEditable = isEditable && ControlPanelExtensions.GetControlPanelState(Html) == ControlPanelState.DragDrop 
			};
			Html.ResolveService<DisplayableRendererSelector>()
				.Render(ctx, writer);

			if (Wrapper != null)
				writer.Write(Wrapper.ToString(TagRenderMode.EndTag));
		}
Exemple #8
0
        private void RenderDisplayable()
        {
            var displayable = DefinitionMap.Instance.GetOrCreateDefinition(CurrentItem).Displayables.FirstOrDefault(d => d.Name == propertyName);

            if (displayable == null)
            {
                if (!swallowExceptions)
                    throw new N2Exception("No attribute implementing IDisplayable found on the property '{0}' of the item #{1} of type {2}", propertyName, CurrentItem.ID, CurrentItem.GetContentType());
                return;
            }

            var writer = Html.ViewContext.Writer;
            if (Wrapper != null)
                writer.Write(Wrapper.ToString(TagRenderMode.StartTag));

            var ctx = new RenderingContext { Content = CurrentItem, Displayable = displayable, Html = Html, PropertyName = propertyName };
            Html.ResolveService<DisplayableRendererSelector>()
                .Render(ctx, writer);

            if (Wrapper != null)
                writer.Write(Wrapper.ToString(TagRenderMode.EndTag));
        }
Exemple #9
0
 public override RegisteringDisplayRenderer <T> Create <T>(N2.Web.Rendering.RenderingContext context, N2.Definitions.Runtime.ContentRegistration re)
 {
     return(new UnencodedRegisteringDisplayRenderer <T>(context, re));
 }
Exemple #10
0
 public override void Render(RenderingContext context, IWritingDisplayable displayable, TextWriter writer)
 {
     displayable.Write(context.Content, context.PropertyName, writer);
 }
 public void Render(RenderingContext context, TextWriter writer)
 {
     ResolveRenderer(context.Displayable.GetType()).Render(context, writer);
 }