In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article is about how NVelocity content is generated. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
1. Several ways of content generation based on NVelocity
From the above illustration, we can see that the template generation of NVelocity includes three ways, one is from file to file or string, the other is from string to string, their respective processing methods are different, but they can parse the contents correctly.
In order to make better use of the features of NVelocity, we make a preliminary auxiliary class encapsulation of it.
/ / generate auxiliary class / public class NVelocityHelper {protected VelocityContext context;protected Template template;protected string templateFile;/// dictionary content for storing key values / private Dictionary KeyObjDict = new Dictionary () based on the template file of NVelocity / add a key object / value / public NVelocityHelper AddKeyValue (string key, object value) {if (! KeyObjDict.ContainsKey (key)) {KeyObjDict.Add (key, value);} return this;}.
The above AddKeyValue method is mainly used to add some variable objects that need to be bound to the page for the template engine, so that the contents of the page variable parameters can be parsed correctly.
In order to use the various features of NVelocity, we need to construct the relevant information of the template in the auxiliary class and set the relevant parameters.
/ initialize template engine / protected virtual void InitTemplateEngine () {try {/ / Velocity.Init (NVELOCITY_PROPERTY); VelocityEngine templateEngine = new VelocityEngine (); templateEngine.SetProperty (RuntimeConstants.RESOURCE_LOADER, "file"); templateEngine.SetProperty (RuntimeConstants.INPUT_ENCODING, "utf-8"); templateEngine.SetProperty (RuntimeConstants.OUTPUT_ENCODING, "utf-8") / / if the FILE_RESOURCE_LOADER_PATH attribute is set, the base path of the template file is based on the directory set, otherwise the default running directory templateEngine.SetProperty (RuntimeConstants.FILE_RESOURCE_LOADER_PATH, AppDomain.CurrentDomain.BaseDirectory); templateEngine.Init (); template = templateEngine.GetTemplate (templateFile);} catch (ResourceNotFoundException re) {string error = string.Format ("Cannot find template" + templateFile); LogTextHelper.Error (error); throw new Exception (error, re) } catch (ParseErrorException pee) {string error = string.Format ("Syntax error in template" + templateFile + ":" + pee.StackTrace); LogTextHelper.Error (error); throw new Exception (error, pee);}}
Before generating the content, you need to bind the relevant object properties to the context object of the template engine.
/ private void InitContext () {context = new VelocityContext (); foreach (string key in KeyObjDict.Keys) {context.Put (key, KeyObjDict [key]);}}
1) construct the corresponding file content according to the template file
/ create the output file according to the template and return the generated file path / public virtual string ExecuteFile () {string fileName = ""; if (template! = null) {string filePath = CheckEndBySlash (directoryOfOutput); fileName = filePath + fileNameOfOutput + fileExtension;if (! string.IsNullOrEmpty (filePath) & &! Directory.Exists (filePath)) {Directory.CreateDirectory (filePath);} / / LogTextHelper.Debug (string.Format ("Class file output path: {0}", fileName)); InitContext () Using (StreamWriter writer = new StreamWriter (fileName, false)) {template.Merge (context, writer);}} return fileName;}
2) construct the string content according to the template file
/ output string content according to template / public string ExecuteString () {InitContext (); System.IO.StringWriter writer = new System.IO.StringWriter (); template.Merge (context, writer); return writer.GetStringBuilder () .ToString ();}
3) construct string output according to the contents of the string
/ public string ExecuteMergeString (string inputString) {VelocityEngine templateEngine = new VelocityEngine (); templateEngine.Init (); InitContext (); System.IO.StringWriter writer = new System.IO.StringWriter (); templateEngine.Evaluate (context, writer, "mystring", inputString); return writer.GetStringBuilder (). ToString ();}
The above several ways to manipulate template output, the calling code is as follows.
Private void btnGenerateFile_Click (object sender, EventArgs e) {string tempalte = "Template/template.htm"; / / relative directory TestInfo info = new TestInfo (); info.Title = "test title"; info.Content = "test content, this is test content"; info.Datetime = DateTime.Now;NVelocityHelper adapter = new NVelocityHelper (tempalte) Adapter.AddKeyValue ("title", "This is a title"). AddKeyValue ("content", "This is a Content"). AddKeyValue ("datetime", System.DateTime.Now) .AddKeyValue ("TestInfo", info); adapter.FileNameOfOutput = "testTemplate"; string filePath = adapter.ExecuteFile (); if (! string.IsNullOrEmpty (filePath)) {Process.Start (filePath);}} private void btnGenerate_Click (object sender, EventArgs e) {string tempalte = "Template/template.htm"; / / relative directory TestInfo info = new TestInfo () Info.Title = "test title"; info.Content = "test content, this is test content"; info.Datetime = DateTime.Now;NVelocityHelper adapter = new NVelocityHelper (tempalte); adapter.AddKeyValue ("title", "This is a title"). AddKeyValue ("content", "This is a Content"). AddKeyValue ("datetime", System.DateTime.Now) .AddKeyValue ("TestInfo", info); this.txtCode.Text = adapter.ExecuteString () } private void btnMergeString_Click (object sender, EventArgs e) {System.Text.StringBuilder builder = new System.Text.StringBuilder (); builder.Append ("${Title}\ r\ n" + "$Content\ r\ n" + "$Digest\ r\ n" + "$Author\ r\ n" + "$Keyword\ r\ n" + "$DateTime\ r\ n"); NVelocityHelper adapter = new NVelocityHelper () Adapter.AddKeyValue ("Title", "title"). AddKeyValue ("Content", "content"). AddKeyValue ("Digest", "abstract"). AddKeyValue ("Author", "author"). AddKeyValue ("Keyword", "keyword"). AddKeyValue ("DateTime", DateTime.Now); this.txtCode.Text = adapter.ExecuteMergeString (builder.ToString ());}
2. Several application scenarios of template engine NVelocity
The above ways of manipulating template content can meet our application requirements in most cases, for example, we can define some custom content templates in the code generation tool, and then combine the metadata information of the database to achieve code generation operations with rich logic.
We can also realize the file generation operation of the article content according to the input content in some content management applications (such as article management). After this generation, we can directly use the file link address of the article.
Or generate specific pages based on the data information for set-up operations, such as the set-up processing in Winform.
Thank you for reading! This is the end of this article on "what are the ways of generating NVelocity content?". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 272
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.