NVelocityView.cs :  » Web-Frameworks » MvcContrib » MvcContrib » ViewEngines » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Web Frameworks » MvcContrib 
MvcContrib » MvcContrib » ViewEngines » NVelocityView.cs
using System;
using System.Collections;
using System.IO;
using System.Web.Mvc;
using NVelocity;

namespace MvcContrib.ViewEngines{
  public class NVelocityView : IViewDataContainer, IView
  {
    private ViewContext _viewContext;
    private readonly Template _masterTemplate;
    private readonly Template _viewTemplate;

    public NVelocityView(Template viewTemplate, Template masterTemplate)
    {
      _viewTemplate = viewTemplate;
      _masterTemplate = masterTemplate;
    }

    public Template ViewTemplate
    {
      get { return _viewTemplate; }
    }

    public Template MasterTemplate
    {
      get { return _masterTemplate; }
    }

    public ViewDataDictionary ViewData
    {
      get { return _viewContext.ViewData; }
      set { throw new NotSupportedException(); }
    }

    public TempDataDictionary TempData
    {
      get { return _viewContext.TempData; }
      set { throw new NotSupportedException(); }
    }

    public void Render(ViewContext viewContext, TextWriter writer)
    {
      _viewContext = viewContext;
      VelocityContext context = CreateContext();


      bool hasLayout = _masterTemplate != null;
      if(hasLayout)
      {
        //Native NVelocity support for master/child template using #parse. No need to buffer the child template to a Stringwriter
        //which bypasses the response output... and will therefore cause any void child helper extension method
        //calls to fail to render in the correct location. #parse($childContent) must appear in the master template
        //See google group discussion thread: 
        //http://groups.google.com/group/mvccontrib-discuss/browse_thread/thread/0fc84d69db708322?hl=en 

        context.Put("childContent", _viewTemplate.Name);
        _masterTemplate.Merge(context, writer);
      }
      else
      {
        _viewTemplate.Merge(context, writer);
      }
    }

    private VelocityContext CreateContext()
    {
      var entries = new Hashtable(StringComparer.InvariantCultureIgnoreCase);
      if(_viewContext.ViewData != null)
      {
        foreach(var pair in _viewContext.ViewData)
        {
          entries[pair.Key] = pair.Value;
        }
      }
      entries["viewdata"] = _viewContext.ViewData;
      if(_viewContext.TempData != null)
      {
        foreach(var pair in _viewContext.TempData)
        {
          entries[pair.Key] = pair.Value;
        }
      }
      entries["tempdata"] = _viewContext.TempData;

      entries["routedata"] = _viewContext.RouteData;
      entries["controller"] = _viewContext.Controller;
      entries["httpcontext"] = _viewContext.HttpContext;

      CreateAndAddHelpers(entries);

      return new VelocityContext(entries);
    }

    private void CreateAndAddHelpers(Hashtable entries)
    {
      entries["html"] = entries["htmlhelper"] = new HtmlExtensionDuck(_viewContext, this);
      entries["url"] = entries["urlhelper"] = new UrlHelper(_viewContext.RequestContext);
    }
  }
}
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.