Exemple #1
0
        public HttpResponseMessage RenderTemplate([FromUri] int templateId)
        {
            try
            {
                var template = Sexy.TemplateContext.GetTemplate(templateId);

                var engine     = EngineFactory.CreateEngine(template);
                var dataSource = (ViewDataSource)Sexy.GetViewDataSource(ActiveModule.ModuleID, SexyContent.HasEditPermission(ActiveModule), template);
                engine.Init(template, Sexy.App, ActiveModule, dataSource, InstancePurposes.WebView, Sexy);
                engine.CustomizeData();

                if (template.AttributeSetID.HasValue && !template.DemoEntityID.HasValue && dataSource["Default"].List.Count == 0)
                {
                    var toolbar = "<ul class='sc-menu' data-toolbar='" + Newtonsoft.Json.JsonConvert.SerializeObject(new { sortOrder = 0, useModuleList = true, action = "edit" }) + "'></ul>";
                    return(new HttpResponseMessage(HttpStatusCode.OK)
                    {
                        Content = new StringContent("<div class='dnnFormMessage dnnFormInfo'>No demo item exists for the selected template. " + toolbar + "</div>")
                    });
                }

                var response = new HttpResponseMessage(HttpStatusCode.OK);
                response.Content = new StringContent(engine.Render(), Encoding.UTF8, "text/plain");
                return(response);
            }
            catch (Exception e)
            {
                DotNetNuke.Services.Exceptions.Exceptions.LogException(e);
                throw e;
            }
        }
        /// <summary>
        /// Get the content data and render it with the given template to the page.
        /// </summary>
        protected void ProcessView(PlaceHolder phOutput, Panel pnlError, Panel pnlMessage)
        {
            #region Check if everything has values and return if not

            if (Template == null)
            {
                ShowError(LocalizeString("TemplateConfigurationMissing.Text"), pnlError);
                return;
            }

            if (Template.AttributeSetID.HasValue && DataSource.GetCache(ZoneId.Value, AppId.Value).GetContentType(Template.AttributeSetID.Value) == null)
            {
                ShowError("The contents of this module cannot be displayed because it's located in another VDB.", pnlError);
                return;
            }

            if (Template.AttributeSetID.HasValue && !Template.DemoEntityID.HasValue && Items.All(e => !e.EntityID.HasValue))
            {
                var toolbar = IsEditable ? "<ul class='sc-menu' data-toolbar='" + Newtonsoft.Json.JsonConvert.SerializeObject(new { sortOrder = Items.First().SortOrder, useModuleList = true, action = "edit" }) + "'></ul>" : "";
                ShowMessage(LocalizeString("NoDemoItem.Text") + " " + toolbar, pnlMessage);
                return;
            }

            #endregion

            try
            {
                //var renderTemplate = Template;
                string renderedTemplate;

                var engine     = EngineFactory.CreateEngine(Template);
                var dataSource = (ViewDataSource)Sexy.GetViewDataSource(this.ModuleId, SexyContent.HasEditPermission(this.ModuleConfiguration), Template);
                engine.Init(Template, Sexy.App, this.ModuleConfiguration, dataSource, Request.QueryString["type"] == "data" ? InstancePurposes.PublishData : InstancePurposes.WebView, Sexy);
                engine.CustomizeData();

                // Output JSON data if type=data in URL
                if (Request.QueryString["type"] == "data")
                {
                    if (dataSource.Publish.Enabled)
                    {
                        var publishedStreams = dataSource.Publish.Streams;
                        renderedTemplate = Sexy.GetJsonFromStreams(dataSource, publishedStreams.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries));
                    }
                    else
                    {
                        Response.StatusCode = 403;
                        var moduleTitle = new ModuleController().GetModule(ModuleId).ModuleTitle;
                        renderedTemplate = Newtonsoft.Json.JsonConvert.SerializeObject(new { error = "2sxc Content (" + ModuleId + "): " + String.Format(LocalizeString("EnableDataPublishing.Text"), ModuleId, moduleTitle) });
                        Response.TrySkipIisCustomErrors = true;
                    }
                    Response.ContentType = "application/json";
                }
                else
                {
                    renderedTemplate = engine.Render();
                }

                // If standalone is specified, output just the template without anything else
                if (StandAlone)
                {
                    Response.Clear();
                    Response.Write(renderedTemplate);
                    Response.Flush();
                    Response.SuppressContent = true;
                    HttpContext.Current.ApplicationInstance.CompleteRequest();
                }
                else
                {
                    phOutput.Controls.Add(new LiteralControl(renderedTemplate));
                }
            }
            // Catch errors; log them
            catch (Exception Ex)
            {
                ShowError(LocalizeString("TemplateError.Text") + ": " + HttpUtility.HtmlEncode(Ex.ToString()), pnlError, LocalizeString("TemplateError.Text"), false);
                Exceptions.LogException(Ex);
            }
        }