ToSic.SexyContent.SxcInstance.Render C# (CSharp) Method

Render() public method

public Render ( ) : System.Web.HtmlString
return System.Web.HtmlString
        public HtmlString Render()
        {
            var renderHelp = new RenderingHelpers(this);

            try
            {
                string innerContent = null;

                #region do pre-check to see if system is stable & ready
                var notReady = new InstallationController().CheckUpgradeMessage(PortalSettings.Current.UserInfo.IsSuperUser);
                if (!string.IsNullOrEmpty(notReady))
                    innerContent = renderHelp.DesignErrorMessage(new Exception(notReady), true, "Error - needs admin to fix this", false, false);

                #endregion

                #region check if the content-group exists (sometimes it's missing if a site is being imported and the data isn't in yet
                if (innerContent == null)
                {
                    if (ContentBlock.DataIsMissing)
                    {
                        if (Environment.Permissions.UserMayEditContent)
                        {
                            innerContent = ""; // stop further processing
                        }
                        else // end users should see server error as no js-side processing will happen
                        {
                            var ex =
                                new Exception(
                                    "Data is missing - usually when a site is copied but the content / apps have not been imported yet - check 2sxc.org/help?tag=export-import");
                            innerContent = renderHelp.DesignErrorMessage(ex, true,
                                "Error - needs admin to fix", false, true);
                        }
                    }
                }
                #endregion

                #region try to render the block or generate the error message
                if (innerContent == null)
                    try
                    {
                        if (Template != null) // when a content block is still new, there is no definition yet
                        {
                            var engine = GetRenderingEngine(InstancePurposes.WebView);
                            innerContent = engine.Render();
                        }
                        else innerContent = "";
                    }
                    catch (Exception ex)
                    {
                        innerContent = renderHelp.DesignErrorMessage(ex, true, "Error rendering template", false, true);
                    }
                #endregion

                #region Wrap it all up into a nice wrapper tag
                var editInfos = JsonConvert.SerializeObject(renderHelp.GetClientInfosAll());
                var startTag = (RenderWithDiv
                    ? $"<div class=\"sc-viewport sc-content-block\" data-cb-instance=\"{ContentBlock.ParentId}\" " +
                      $" data-cb-id=\"{ContentBlock.ContentBlockId}\""
                      +
                      (RenderWithEditMetadata
                          ? " data-edit-context=\'" + editInfos + "'"
                          : "")
                      + ">\n"
                    : "");
                var endTag = (RenderWithDiv ? "\n</div>" : "");
                string result = startTag + innerContent + endTag;
                #endregion

                return new HtmlString(result);
            }
            catch (Exception ex)
            {
                // todo: i18n
                return new HtmlString(renderHelp.DesignErrorMessage(ex, true, null, true, true));
            }
        }

Usage Example

Esempio n. 1
0
        /// <summary>
        /// Process View if a Template has been set
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_PreRender(object sender, EventArgs e)
        {
            try
            {
                // check things if it's a module of this portal (ensure everything is ok, etc.)
                var isSharedModule = ModuleConfiguration.PortalID != ModuleConfiguration.OwnerPortalID;
                if (!isSharedModule && !SxcI.ContentBlock.ContentGroupExists && SxcI.App != null)
                {
                    new DnnStuffToRefactor().EnsurePortalIsConfigured(SxcI, Server, ControlPath);
                }

                var renderNaked = (Request.QueryString["standalone"] == "true");
                if (renderNaked)
                {
                    SxcI.RenderWithDiv = false;
                }
                var renderedTemplate = SxcI.Render().ToString();

                // If standalone is specified, output just the template without anything else
                if (renderNaked)
                {
                    SendStandalone(renderedTemplate);
                }
                else
                {
                    phOutput.Controls.Add(new LiteralControl(renderedTemplate));
                }
            }
            catch (Exception ex)
            {
                Exceptions.ProcessModuleLoadException(this, ex);
            }
        }
All Usage Examples Of ToSic.SexyContent.SxcInstance::Render