/// <summary> /// Called by the ASP.NET page framework to notify server controls that use composition-based implementation to create any child controls they contain in preparation for posting back or rendering. /// </summary> protected override void CreateChildControls() { // collect all attributes set on the control var keys = Attributes.Keys; foreach (string key in keys) { MacroAttributes.Add(key.ToLower(), Attributes[key]); } if (!MacroAttributes.ContainsKey("macroalias") && !MacroAttributes.ContainsKey("macroAlias")) { MacroAttributes.Add("macroalias", Alias); } // set pageId to int.MinValue if no pageID was found, // e.g. if the macro was rendered on a custom (non-Umbraco) page int pageId = Context.Items["pageID"] == null ? int.MinValue : int.Parse(Context.Items["pageID"].ToString()); if ((!String.IsNullOrEmpty(Language) && Text != "") || !string.IsNullOrEmpty(FileLocation)) { var tempMacro = new macro(); tempMacro.GenerateMacroModelPropertiesFromAttributes(MacroAttributes); if (string.IsNullOrEmpty(FileLocation)) { tempMacro.Model.ScriptCode = Text; tempMacro.Model.ScriptLanguage = Language; } else { tempMacro.Model.ScriptName = FileLocation; } tempMacro.Model.MacroType = MacroTypes.Script; if (!String.IsNullOrEmpty(Attributes["Cache"])) { var cacheDuration = 0; if (int.TryParse(Attributes["Cache"], out cacheDuration)) { tempMacro.Model.CacheDuration = cacheDuration; } else { System.Web.HttpContext.Current.Trace.Warn("Template", "Cache attribute is in incorect format (should be an integer)."); } } var c = tempMacro.renderMacro((Hashtable)Context.Items["pageElements"], pageId); if (c != null) { Exceptions = tempMacro.Exceptions; Controls.Add(c); } else { System.Web.HttpContext.Current.Trace.Warn("Template", "Result of inline macro scripting is null"); } } else { var tempMacro = macro.GetMacro(Alias); if (tempMacro != null) { try { var c = tempMacro.renderMacro(MacroAttributes, (Hashtable)Context.Items["pageElements"], pageId); if (c != null) { Controls.Add(c); } else { System.Web.HttpContext.Current.Trace.Warn("Template", "Result of macro " + tempMacro.Name + " is null"); } } catch (Exception ee) { System.Web.HttpContext.Current.Trace.Warn("Template", "Error adding macro " + tempMacro.Name, ee); } } } }
/// <summary> /// Called by the ASP.NET page framework to notify server controls that use composition-based implementation to create any child controls they contain in preparation for posting back or rendering. /// </summary> protected override void CreateChildControls() { // collect all attributes set on the control var keys = Attributes.Keys; foreach (string key in keys) { MacroAttributes.Add(key.ToLower(), HttpUtility.HtmlDecode(Attributes[key])); } if (MacroAttributes.ContainsKey("macroalias") == false && MacroAttributes.ContainsKey("macroAlias") == false) { MacroAttributes.Add("macroalias", Alias); } // set pageId to int.MinValue if no pageID was found, // e.g. if the macro was rendered on a custom (non-Umbraco) page var pageId = UmbracoContext.Current.PageId == null ? int.MinValue : UmbracoContext.Current.PageId.Value; if ((string.IsNullOrEmpty(Language) == false && Text != "") || string.IsNullOrEmpty(FileLocation) == false) { var tempMacro = new MacroModel(); MacroRenderer.GenerateMacroModelPropertiesFromAttributes(tempMacro, MacroAttributes); // executing an inline macro? // ie the code of the macro is in the control's text body // ok, this is not supported in v8 anymore if (string.IsNullOrEmpty(FileLocation)) { throw new NotSupportedException("Inline macros are not supported anymore."); } // executing an on-disk macro // it has to be a partial (cshtml or vbhtml) macro in v8 var extension = System.IO.Path.GetExtension(FileLocation); if (extension.InvariantEndsWith(".cshtml") == false && extension.InvariantEndsWith(".vbhtml") == false) { throw new NotSupportedException(""); } tempMacro.MacroSource = FileLocation; tempMacro.MacroType = MacroTypes.PartialView; if (string.IsNullOrEmpty(Attributes["Cache"]) == false) { int cacheDuration; if (int.TryParse(Attributes["Cache"], out cacheDuration)) { tempMacro.CacheDuration = cacheDuration; } else { Context.Trace.Warn("Template", "Cache attribute is in incorect format (should be an integer)."); } } var renderer = new MacroRenderer(Current.ProfilingLogger); var c = renderer.Render(tempMacro, (Hashtable)Context.Items["pageElements"], pageId).GetAsControl(); if (c != null) { Exceptions = renderer.Exceptions; Controls.Add(c); } else { Context.Trace.Warn("Template", "Result of inline macro scripting is null"); } } else { var m = Current.Services.MacroService.GetByAlias(Alias); if (m == null) { return; } var tempMacro = new MacroModel(m); try { var renderer = new MacroRenderer(Current.ProfilingLogger); var c = renderer.Render(tempMacro, (Hashtable)Context.Items["pageElements"], pageId, MacroAttributes).GetAsControl(); if (c != null) { Controls.Add(c); } else { Context.Trace.Warn("Template", "Result of macro " + tempMacro.Name + " is null"); } } catch (Exception ee) { Context.Trace.Warn("Template", "Error adding macro " + tempMacro.Name, ee); throw; } } }