Click here to Skip to main content
15,886,765 members
Articles / Web Development / ASP.NET

Introduction to Bellevue View Engine - Part 1

Rate me:
Please Sign up or sign in to vote.
3.43/5 (4 votes)
18 Mar 2010CPOL8 min read 26K   126   10  
Prototype of a new template engine for ASP.NET MVC Framework that respects HTML and uses CSS-like syntax for model binding.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Ope.Bellevue.Renderers.General;

namespace Ope.Bellevue.Parsing
{
    public class DeclarationHandler
    {        
        public bool Handle(HtmlAgilityPack.HtmlNode node, ElementDeclaration declr, Ope.Bellevue.Parsing.ParsedViewBase parseResult)
        {
            var instruction = new Ope.Bellevue.Rendering.RenderingInstruction(declr);

            switch (instruction.DeclarationName)
            {
                // Elements
                case "apply-template":
                    instruction.SetRenderer(typeof(ApplyTemplate));
                    instruction.AddParameter("value", declr.Expression, 0);
                    instruction.AddParameter("template-id", declr.Expression, 1);
                    parseResult.AddRendering(node, instruction);
                    return true;                
                case "debugger":
                    instruction.SetRenderer(typeof(Ope.Bellevue.Renderers.Infra.PrintDebugger));
                    parseResult.AddRendering(node, instruction);
                    return true;
                case "text":
                    instruction.SetRenderer(typeof(Text));
                    instruction.AddParameter("value", declr.Expression, 0);
                    parseResult.AddRendering(node, instruction);
                    return true;
                case "text-replace":
                    instruction.SetRenderer(typeof(TextReplace));
                    instruction.AddParameter("replace-text", declr.Expression, 0);
                    instruction.AddParameter("value", declr.Expression, 1);
                    instruction.AddParameter("original-text", node.InnerText);
                    parseResult.AddRendering(node, instruction);
                    return true;
                case "text-format":
                    instruction.SetRenderer(typeof(TextFormat));
                    instruction.AddParameter("value", declr.Expression, 0);
                    if (declr.Expression.Terms.Count > 1)
                    {
                        instruction.AddParameter("format-text", declr.Expression, 1);
                    }
                    else
                    {
                        instruction.AddParameter("format-text", node.InnerText);
                    }
                    parseResult.AddRendering(node, instruction);
                    return true;
                case "html":
                    instruction.SetRenderer(typeof(Text));
                    instruction.AddParameter("value", declr.Expression, 0);
                    instruction.AddParameter("noEncoding", "true");
                    parseResult.AddRendering(node, instruction);
                    return true;
                case "render-control":
                    instruction.SetRenderer(typeof(RenderControl));
                    instruction.AddParameter("value", declr.Expression, 0);
                    parseResult.AddRendering(node, instruction);
                    return true;
                case "action-link":
                    instruction.SetRenderer(typeof(Ope.Bellevue.Renderers.MvcHtmlHelper));
                    for (int i = 0; i < declr.Expression.Terms.Count; i++)
                    {
                        instruction.AddParameter("parameter" + i, declr.Expression, i);
                    }
                    parseResult.AddRendering(node, instruction);
                    return true;
                case "placeholder-id":
                    instruction.SetRenderer(typeof(Ope.Bellevue.Renderers.Infra.ContentPlaceholder));
                    instruction.AddParameter("id", declr.Expression, 0);
                    // HACK: Rewrite rendering of the inner HTML
                    instruction.AddParameter("original-value", node.InnerHtml);
                    parseResult.AddRendering(node, instruction);
                    return true;

                // Attributes
                case "attr":
                    instruction.SetRenderer(typeof(Text));
                    // TODO: More robust way to get the term
                    string attrName = declr.Expression.Terms[0].Value;
                    instruction.AddParameter("attr-name", attrName);
                    instruction.AddParameter("original-value", node.GetAttributeValue(attrName, ""));
                    instruction.AddParameter("value", declr.Expression, 1);
                    parseResult.AddRendering(attrName, node, instruction);
                    return true;
                case "style":
                    instruction.SetRenderer(typeof(Text));
                    instruction.AddParameter("attr-name", "style");
                    string originalValue = node.GetAttributeValue("style", "");
                    if (originalValue != "")
                    {
                        if (instruction.Position == Ope.Bellevue.Rendering.RelativePosition.Append)
                        {
                            originalValue = originalValue + ";";
                        }
                        if (instruction.Position == Ope.Bellevue.Rendering.RelativePosition.Insert)
                        {
                            originalValue = ";" + originalValue;
                        }
                    }
                    instruction.AddParameter("original-value", originalValue);
                    instruction.AddParameter("value", declr.Expression, 0);
                    parseResult.AddRendering("style", node, instruction);
                    return true;
                case "class":
                    instruction.SetRenderer(typeof(Text));
                    instruction.AddParameter("attr-name", "class");
                    string originalClass = node.GetAttributeValue("class", "");
                    if (originalClass != "")
                    {
                        if (instruction.Position == Ope.Bellevue.Rendering.RelativePosition.Append)
                        {
                            originalClass = originalClass + " ";
                        }
                        if (instruction.Position == Ope.Bellevue.Rendering.RelativePosition.Insert)
                        {
                            originalClass = " " + originalClass;
                        }
                    }
                    instruction.AddParameter("original-value", originalClass);
                    instruction.AddParameter("value", declr.Expression, 0);
                    parseResult.AddRendering("class", node, instruction);
                    return true;
                case "value":
                    instruction.SetRenderer(typeof(Text));
                    instruction.AddParameter("attr-name", "value");
                    instruction.AddParameter("original-value", node.GetAttributeValue("value", ""));
                    instruction.AddParameter("value", declr.Expression, 0);
                    parseResult.AddRendering("value", node, instruction);
                    return true;
                case "checked":
                    instruction.SetRenderer(typeof(Checked));
                    instruction.AddParameter("value", declr.Expression, 0);
                    parseResult.AddRendering("checked", node, instruction);
                    return true;
                default:
                    return false;
            }
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Founder OPE AG
Switzerland Switzerland
Olli is a .Net developer and architect.

He specializes in Asp.net MVC and other web technologies, XML and lately Domain Specific Languages.

Olli is originally from Finland, but currently works for his own one-man-initiative OPE AG (www.ope.ag) from Switzerland. He has over 10 years of experience as one of the founding partners and Chief Technology Officer of Quartal group of companies (www.quartal.com).

Comments and Discussions