protected string RenderNodeBlock(NodeBlock block, HtmlHelper html)
        {
            //check if the node exists
            if (block.Content == null) return null;

            //initialise WebBlocksAPI
            WebBlocksAPI blockInstanceAPI = new WebBlocksAPI();
            blockInstanceAPI.CssClasses = new List<string>();
            blockInstanceAPI.BlockElement = "";
            blockInstanceAPI.BlockAttributes = new Dictionary<string, string>();

            IRenderingEngine renderingEngine = ResolveRenderingEngine(block);

            //if rendering engine is null then the document doesn't exist anymore
            if (renderingEngine == null || block.Content == null) return "";

            //set the block model for the view to access
            WebBlocksUtility.CurrentBlockContent = block.Content;

            string blockIdAttribute = WebBlocksUtility.IsInBuilder ? string.Format(" wbid='{0}'", block.Id) : "";
            string blockTemplateAttribute = WebBlocksUtility.IsInBuilder ?
                string.Format(" templateblock='{0}'", block.IsTemplateBlock.ToString().ToLower()) : "";
            string blockDeletedAttribute = WebBlocksUtility.IsInBuilder && block.IsDeleted ? " deletedBlock='deleted' style='display:none;visibilty:hidden;'" : "";

            string renderedContent = "";

            //render
            try
            {
                renderedContent = renderingEngine.Render(html);
            }
            catch(Exception ex)
            {
                renderedContent = HttpUtility.HtmlEncode(ex.ToString());
            }

            List<string> CssClasses = blockInstanceAPI.CssClasses;

            string blockElement = blockInstanceAPI.BlockElement ?? "div";
            blockElement = blockElement != "" ? blockElement : "div";

            Dictionary<string, string> blockAttributeDictionary = blockInstanceAPI.BlockAttributes;
            string blockAttributes = string.Join(" ", blockAttributeDictionary.Keys.Select(c => c + "='" + blockAttributeDictionary[c] + "'"));

            string blockClass = string.Format("{0}{1}{0}{2}{3}{4}",
                block.Class.Length > 0 ? " " : "",
                block.Class,
                WebBlocksUtility.CurrentBlockContent.GetPropertyValue("cssClasses"),
                CssClasses.Count() > 0 ? " " : "",
                String.Join(" ", CssClasses));
            blockClass = WebBlocksUtility.IsInBuilder ? "block " + blockClass : blockClass;

            renderedContent = string.Format("<{0} class='{1}'{2}{3}{4}{5}>{6}</{0}>",
                blockElement, blockClass, blockIdAttribute, blockTemplateAttribute, blockDeletedAttribute, blockAttributes, renderedContent);

            return renderedContent;
        }
示例#2
0
        protected IRenderingEngine ResolveRenderingEngine(NodeBlock block)
        {
            try
            {
                var blockDoc = new Document(block.Id);

                //resolve with partial view
                IRenderingEngine engine = new PartialViewRenderingEngine
                {
                    Macro = new MacroModel(new Macro { ScriptingFile = blockDoc.ContentType.Alias })
                };

                return engine;
            }
            catch (Exception)
            {
                return null;
            }
        }