Tuesday, January 29, 2013

Use of Extension Method in NVelocity

NVelocity is a .Net-based template engine. It permits anyone to use the simple yet powerful template language to reference objects defined in .Net code. NVelocity is a port of the excellent Apache Jakarta Velocity project for .NET platform. It is a very simple, easy to learn and extensible template engine.
Library: 
  • NVelocity.dll
  • NVelocityTemplateEngine.dll
Below example shows how to incorporate dynamic content from xml file to simple text file. Consider we have "customer.xml" and  "customer.vm" files as:

customer.vm
Loop :

List of Customer Details:
#foreach($customer in $helper.MakeCollection($root.testdocument.ListOfCustomerDetail.CustomerDetail))
UID: $customer.UID
Language: $customer.Language
BusinessUnit: $customer.BusinessUnit
#end

List of Leads:
#foreach($customer in $helper.MakeCollection($root.testdocument.ListOfLeadDetail.CustomerDetail))
UID: $customer.UID
Language: $customer.Language
BusinessUnit: $customer.BusinessUnit
#end

customer.xml
<?xml version="1.0" encoding="UTF-8"?>
<testdocument>
  <ListOfCustomerDetail>
    <firstname>hello</firstname>
    <CustomerDetail>
      <UID>1</UID>
      <Language>English</Language>
      <BusinessUnit>ITD</BusinessUnit>
    </CustomerDetail>
    <CustomerDetail>
      <UID>2</UID>
      <Language>English</Language>
      <BusinessUnit>Eolution</BusinessUnit>
    </CustomerDetail>
    <CustomerDetail>
      <UID>3</UID>
      <Language>English</Language>
      <BusinessUnit>Continuty</BusinessUnit>
    </CustomerDetail>
  </ListOfCustomerDetail>
  <ListOfLeadDetail>
  <firstname>hello</firstname>
    <CustomerDetail>
      <UID>1</UID>
      <Language>English</Language>
      <BusinessUnit>ITD</BusinessUnit>
    </CustomerDetail>
    <CustomerDetail>
      <UID>2</UID>
      <Language>English</Language>
      <BusinessUnit>Eolution</BusinessUnit>
    </CustomerDetail>
    <CustomerDetail>
      <UID>3</UID>
      <Language>English</Language>
      <BusinessUnit>Continuty</BusinessUnit>
    </CustomerDetail>
  </ListOfLeadDetail>
</testdocument>

Here, XML document contents are converted into Json Object. This Json object then registered with the NVelocity context.

Similarly to access the extension method inside the template, we have to register Helper class instance with the NVelocity context. We have to make helper class non-static for this.
using System;
using System.Globalization;
using System.IO;
using System.Xml;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using NVelocity;

namespace NvelocityTest
{
    class Program
    {
        static void Main(string[] args)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load("customer.xml");

            JObject jobject = JObject.Parse(JsonConvert.SerializeXmlNode(doc.DocumentElement).ToString());

            NVelocity.VelocityContext context = new NVelocity.VelocityContext();
            context.Put("helper", new Helper());
            context.Put("root", jobject);
          
            NVelocity.App.Velocity.Init();

            NVelocity.Template template = NVelocity.App.Velocity.GetTemplate("customer.vm");

            StringWriter writer = new StringWriter(CultureInfo.InvariantCulture);

            template.Merge(context, writer);

            Console.WriteLine(writer.ToString());

            Console.ReadKey();
        }
    }

    public class Helper
    {
        public JArray MakeCollection(JObject value)
        {
            string text = "[" + value.ToString() + "]";
         
            return JArray.Parse(text);
        }

        public JArray MakeCollection(JArray value)
        {
            string text = value.ToString();

            return JArray.Parse(text);
        }
    }
}

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.