/// <summary> /// Renders the parent's body (invokable at a layout template only). If no parent exists an exception is thrown. /// </summary> /// <returns></returns> public virtual LiteralString RenderBody() { ITemplateInternal @this = this; if (@this.Parent == null) { throw new TemplateException("Can only invoke RenderBody() at a child (layout) template."); } return(new LiteralString(@this.Parent.Result)); }
/// <summary> /// Renders the requested page. Optionally you may pass a specific model. By default the current model is used by the rendered page /// </summary> /// <param name="virtualPath">The virtual path of the page that must be rendered.</param> /// <param name="model">The model.</param> /// <param name="skipLayout">if set to <c>true</c> the any layout setting is ignored</param> /// <returns></returns> public virtual LiteralString RenderPage(string virtualPath, object model = null, bool skipLayout = false) { ITemplateInternal @this = this; var page = @this.RazorContext.TemplateFactory.CreateTemplateInstance(VirtualLocation.CombineWith(virtualPath)).CastTo <ITemplateInternal>(); page.SetModel(model ?? Model); page.SetParent(@this); page.Execute(); if (!skipLayout) { page.TryApplyLayout(); } return(new LiteralString(page.Result)); }
/// <summary> /// Renders any parent's section (i.e., the first parent's section with the rquested name). /// </summary> /// <param name="sectionName">Name of the section.</param> /// <param name="required">if set to <c>true</c> an exception is throw if the section was not found.</param> /// <returns></returns> public virtual LiteralString RenderSection(string sectionName, bool required = true) { ITemplateInternal @this = this; if (@this.Parent == null) { throw new TemplateException("Can only invoke RenderSection() at child templates (layouts)."); } if ([email protected](sectionName)) { if (required) { throw new TemplateException("Required section '{0}' not found at any parent template of layout '{1}'. If you want to render this section conditionally invoke RederSection with argument required=false".FormatWith(sectionName, @this.VirtualPath)); } return(string.Empty); } return(new LiteralString(@this.Parent.CastTo <ITemplateInternal>().RenderSectionByChildRequest(sectionName))); }
string ITemplateInternal.RenderSectionByChildRequest(string sectionName) { ITemplateInternal @this = this; Action renderAction; if (!_sections.TryGetValue(sectionName, out renderAction)) { return(@this.Parent == null ? string.Empty : @this.Parent.CastTo <ITemplateInternal>().RenderSectionByChildRequest(sectionName)); } var previous = _outputTarget; var output = _outputTarget = new StringBuilder(); renderAction(); _outputTarget = previous; return(output.ToString()); }
ITemplateInternal ITemplateInternal.SetParent(ITemplateInternal parent) { Parent = parent; if (parent != null) { _hierarchicalTreeDepth = parent.CastTo <TemplateBase>()._hierarchicalTreeDepth + 1; parent.AddChild(this); } else { _hierarchicalTreeDepth = 0; } if (_hierarchicalTreeDepth > _maxHierarchicalTreeDepth) { throw new TemplateTreeException("The hierarchical tree depth overflows the MaxHierarchicalTreeDepth of {0}. Did you configure recursive layout settings? Did you set a layout (at a _viewStart template) that is not at the shared folder resulting in a recursive loop? ".FormatWith(_maxHierarchicalTreeDepth)); } return(this); }
ITemplateInternal ITemplateInternal.ApplyLayout(ITemplateInternal layoutTemplate) { if (layoutTemplate.Parent != null) { throw new TemplateException("You cannot apply a layout more than once. The layout in argument 'layoutTemplate' already has been applied before. You must create a new layout instance on each assignment."); } if (_finalResult != null) { throw new TemplateException("You cannot apply more than one layout to a template. This template already had a layout applied."); } layoutTemplate.SetParent(this); _finalResult = _outputTarget.ToString(); _outputTarget.Clear(); layoutTemplate.Execute(); layoutTemplate.TryApplyLayout(); _finalResult = layoutTemplate.Result; return(this); }
/// <summary> /// Determines whether a section is defined with the specified section name at any parent. /// </summary> /// <param name="sectionName">Name of the section.</param> /// <returns> /// <c>true</c> if any parent defines a section with the specified section name; otherwise, <c>false</c>. /// </returns> public virtual bool IsSectionDefined(string sectionName) { ITemplateInternal @this = this; return(_sections.ContainsKey(sectionName) || (@this.Parent != null && @this.Parent.CastTo <ITemplate>().IsSectionDefined(sectionName))); }
ITemplateInternal ITemplateInternal.AddChild(ITemplateInternal child) { _childs = _childs ?? new List <ITemplate>(); _childs.Add(child); return(this); }