The company's website needs some news, each news format is the same, but do not want to query every time, so want to save these news pages into static html, and then search to find this template engine, of course, other template engines can, such as: Razor, written by yourself manual replacement. And so on. NVelocity is an excellent project in Apache Jakarta Velocity. It has java version and. NET version. It is a very simple, easy to learn and extensible template engine, and supports. NET Core.
Variable references in NVelocity start with $and add the name of the variable. When using! They are empty strings, and the grammar starts with #.
grammar
1. Assignment Instruction #set
#set($book.title="test")
2. Conditional instruction #if/elseif/else
#if($books.Total>1)
$books.Total
#else
//no data
#end
3. Loop instruction # foreach
#foreach($book in $books) $book.Title #end Advanced foreach #foreach($i in $items) #each Text displayed at each [loop] #before Text displayed each time before the loop #after Text displayed each time after a loop #between Text displayed every [two] cycles #odd Odd number display #even Even number display #nodata Empty or no data #beforeall Display before loop #afterall Display after loop #end
4. Reference to Static Resource Directive #include
# include('jquery.js') inserts the current JS into the content as the current stream
5. Reference and parse the resource instruction # parse
#parse('temo.js');
//Unlike # include, if temp.js has NVelocity instructions, variables are processed and the results are inserted into the current stream.
6. Double execution even odd
#foreach(book in $books) #even <p>Two lines: $book.Title</p> #odd <p>Single line: $book.Title</p> #end
7. Relational operators
AND,OR and NOT Operators, corresponding to each other&&,||and!
#if($foo && $bar)
#end
C# Example
1. Reference to NVelocity in Nuget
2. Write template pages (Hellovelocity.vm, or any name suffix such as Hellovelocity.html)
<!DOCTYPE html> <html lang="en" xmlns="http://www.w3.org/1999/xhtml"> <head> <meta charset="utf-8" /> <title>$news.Title</title> </head> <body> <h3>$news.Title</h3> <div> <span>$news.Desc</span> </div> <div> $news.Content </div> </body> </html>
3. Writing background program
public static string TransformBooksToHtml(News news, string resourceTemplateName) { // Initialization Template Engine VelocityEngine ve = new VelocityEngine(); ve.Init(); // Get template files Template t = ve.GetTemplate(resourceTemplateName); // set variable VelocityContext ctx = new VelocityContext(); ctx.Put("news", news); // output StringWriter sw = new StringWriter(); t.Merge(ctx, sw); string message = sw.ToString(); sw.Dispose(); File.WriteAllText($@"{DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss")}.html", sw.ToString());//The path to file generation is optional return message; }
4. Program Call
News news = new News() { Title = $"{DateTime.Now} Journalism", Desc = "Descriptive information of news", Content = "Details of the news", CreateTime = DateTime.Now }; Console.WriteLine(MyNVelocity.TransformBooksToHtml(news, @"/NVelocityTest/Hellovelocity.html"));//The template path here is NVelocityTest Under the catalogue, it can be arbitrary.
5. View the generated files
<!DOCTYPE html> <html lang="en" xmlns="http://www.w3.org/1999/xhtml"> <head> <meta charset="utf-8" /> <title>2019/08/05 14:20:14 Journalism</title> </head> <body> <h3>2019/08/05 14:20:14 Journalism</h3> <div> <span>Descriptive information of news</span> </div> <div> Details of the news </div> </body> </html>
summary
NVelocity can be applied in many places, not only to generate html pages, but also to mail templates.