ASP. net mvc Preview 2-NVelocityViewEngine

Source: Internet
Author: User

I mentioned earlier in MVC process analysis that I prefer to use Velocity instead of WebForm. This is not to say that WebForm itself is poor, not to mention the reason for "performance. In actual development, the pages we face are often very complex, and there are a lot of seemingly headache div, style, span or something, I don't think many "programmers" are willing to adjust some border la s or color settings. Based on the design and encapsulation, WebForm uses a large number of "non-standard HTML code", which leads to an embarrassing situation-conflict between the artist and the page programmer.

Artist: "You messed up all the pages I designed. You should use this style ...... "

Programmer: "I already told you about nonsense. You must write in this format ...... "

Artist: "I modified the page and sent it to you ...... "

Programmer: "No, you can't change it yourself? "

What is Velocity. With a very simple tag or logic control, we can "embed" Our data into the page designed by the artist. We can ask the artist to use "<! -- Data 1 --> "and other things are marked, and we only need to care about whether the data is correctly output. Page adjustment is basically irrelevant to us. Of course, if you are interested in an artist or a new employee in the group, you can master the Velocity usage within one day or even half a day. Why not? You know, most websites frequently adjust the page display, which is not as simple as the Example we usually use for demonstration.

To use the custom view engine, we need to find the "injection" location. During the previous process analysis, the target -- ControllerBuilder has been locked.

public class GlobalApplication : System.Web.HttpApplication
{
  protected void Application_Start(object sender, EventArgs e)
  {
    ControllerBuilder.Current.SetControllerFactory(new NVelocityContrillerFactory());
  }
}

NVelocityContrillerFactory can directly inherit from DefaultControllerFactory. In our override method, assign the generated Controller. ViewEngine to our custom view engine.

public class NVelocityContrillerFactory : DefaultControllerFactory
{
  protected override IController GetControllerInstance(Type controllerType)
  {
    var controller = base.GetControllerInstance(controllerType);
    (controller as Controller).ViewEngine = new NVelocityViewEngine();
    return controller;
  }
}

Next, we will start to write our own view engine.

Public class NVelocityViewEngine: IViewEngine
{
Static NVelocityViewEngine ()
{
Velocity. SetProperty ("resource. loader", "file ");
Velocity. SetProperty ("file. resource. loader. path", HttpContext. Current. Server. MapPath ("~ "));
Velocity. SetProperty ("file. resource. loader. cache", true );
// Velocity. SetProperty ("file. resource. loader. modificationCheckInterval", 10L); // seconds
Velocity. SetProperty ("input. encoding", "UTF-8 ");
Velocity. Init ();
}
Public void RenderView (ViewContext viewContext)
{
Var locator = new NVelocityViewLocator () as IViewLocator;
Var templatePath = locator. GetViewLocation (viewContext, viewContext. ViewName );
Var template = Velocity. GetTemplate (templatePath );
Var context = new VelocityContext ();
If (viewContext. ViewData! = Null)
{
Var properties = viewContext. ViewData. GetProperties (); // The extension method to obtain all attributes.
Foreach (var item in properties. Keys)
{
Context. Put (item, properties [item]);
}
}
Template. Merge (context, viewContext. HttpContext. Response. Output );
}
}

In addition to initializing NVelocity in static structures, ViewData is also processed. Of course, we recommend that you use NVelocityViewLocator to search for template files from multiple directories.

public class NVelocityViewLocator : ViewLocator
{
  public NVelocityViewLocator()
  {
    this.ViewLocationFormats = new[] 
    {
      "/Views/{1}/{0}.vm",
      "/Views/{1}/{0}.htm",
      "/Views/{1}/{0}.html",
      "/Views/Shared/{0}.vm",
      "/Views/Shared/{0}.htm",
      "/Views/Shared/{0}.html",
    };
    this.MasterLocationFormats = new string[0];
  }
}

We can support more extensions ~~~~~

Okay. Test it.

public class HomeController : Controller
{
  public void Index()
  {
    this.RenderView("index", new { Title = "TemplateEngine Test", Name = "Tomcat" });
  }
}

~/View/Home/index.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/....dtd">

 <title>$Title</title>

<body>
  Hello, $Name!
</body>

Output result

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/....dtd">

  <title>TemplateEngine Test</title>

<body>
  Hello, Tomcat!
</body>

 

Forget one important thing. NVelocity can be found in Castle MonoRail's bag.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.