Quick Start
- You can create a template from a file quickly with
Engine.LoadTemplate(...), or from the specified template content withEngine.CreateTemplate(...). - Assign values to the template with
ITemplate.Set(...). - Call
ITemplate.Render(...)to get the rendered result.
Example:
Template file default.html:
<p>Hello, $name</p>Back-end code:
var template = Engine.LoadTemplate("c:\\default.html");
template.Set("name","jntemplate");
var result = template.Render();Or:
var template = Engine.CreateTemplate("<p>Hello, $name</p>");
template.Set("name","jntemplate");
var result = template.Render();output:
<p>Hello, jntemplate</p>Note
You can get the generated text directly through the extension method template.Render(), or write it straight to an output stream with template.Render(TextWriter:writer), for example:
- In a console application
var template = Engine.CreateTemplate("<p>Hello, $name</p>");
template.Set("name","jntemplate");
template.Render(Console.Out);- In WebForms
var template = Engine.CreateTemplate("<p>Hello, $name</p>");
template.Set("name","jntemplate");
template.Render(Response.Output);An even more concise way
Since v2.2.5, JNTemplate supports the more concise Engine.Parse syntax, which is convenient in .NET Minimal APIs.
The supported object can be a dictionary IDictionary<string, object>, for example:
Sample code:
var result = Engine.Parse($"hello,$name", new Dictionary<string, object>{
{ "name","jntemplate"},
});Output:
hello,jntemplateOr an anonymous object:
Sample code:
var result = Engine.Parse($"hello,$UserName", new {
UserName = "jntemplate"
});Output:
hello,jntemplateOr any other custom class. By default a regular class can be accessed by its class name, while generic or other special classes can be accessed through $Model:
Sample code:
var result = Engine.Parse($"hello,$User.UserName", new User {
UserName = "jntemplate"
});If you prefer the early style, in which every object is accessed through $Model, you can achieve it with the following configuration:
Configuration code:
// Note: this only needs to be configured once globally; you can also rename it to any name you like
Engine.Configure(o => o.ModelName = "Model");