Using Scriban to Render Templates in .NET Core

Introduction

 
There are many scenes that need templates to render text, such as E-mail, SMS, etc.
 
Sending an SMS verification code is a very common scenario.  Login, Register or other operations may need it for verification, so there will be many templates for SMS verification code.
 
For example, here is a message after rendering:
  1. 1234 is your Login verification code.[INFO]  
The simple solution is to use string.Format
  1. // read this from database  
  2. var tplStr = @"{0} is your {1} verification code.[INFO]";  
  3.   
  4. // generate this value by some rule  
  5. var code = "0000";  
  6. var module = "Login";  
  7.   
  8. // render it  
  9. var res = string.Format(tplStr, code, module);  
As we can see in the above sample, there are two placeholders, 0 to specify the code, and 1 to specify the module.
 
What is going to happen when somebody modifies the value of tplStr to swap the two placeholders?
 
The result will be:
  1. Login is your 0000 verification code.[INFO]  
The user who receives this SMS will be confused.
 
In order to solve this problem, we should make the placeholders significant. 
 
The template engine can help us out. Here we take Scriban for example.
  

Scriban

 
Scriban is a fast, powerful, safe and lightweight text templating language and engine for .NET, with a compatibility mode for parsing liquid templates.
 
Using Scriban To Render Templates In .NET Core 
 
Visit its GITHUB Page for more information about Scriban.
 
Continuing with the above sample, how to finish the job with Scriban?
 
We also should define the template first.
  1. // read this from database  
  2. var tplStr = @"{{code}} is your {{module}} verification code.[INFO]";  
Not only is the template is very easy to understand, but so is the meaning of the two placeholders.
 
The next step is to parse the template text.
  1. // parse the template text  
  2. var tpl = Template.Parse(tplStr);  
At last, render the template with specify value.
  1. // generate this value by some rule  
  2. var code = "0000";  
  3. var module = "Login";  
  4.   
  5. // render it  
  6. var res = tpl.Render(new { code, module });  
The following is the complete code fragment:
  1. static void TemplateEngineVersion()  
  2. {  
  3.     // read this from database  
  4.     var tplStr = @"{{code}} is your {{opreation}} verification code.[INFO]";  
  5.   
  6.     // parse the template text  
  7.     var tpl = Template.Parse(tplStr);  
  8.       
  9.     // generate this value by some rule  
  10.     var code = "0000";  
  11.     var module = "Login";  
  12.   
  13.     // render it  
  14.     var res = tpl.Render(new { code, module });  
  15.   
  16.     Console.WriteLine("=== template engine version ===");  
  17.     Console.WriteLine(res);  
  18.     Console.WriteLine("=== template engine version ===");  
  19. }  
And the result for the samples:
 
Using Scriban To Render Templates In .NET Core
 

Summary

 
This article showed you that a template engine is helpful when we want to render something. It also introduced the basic usages of Scriban.
 
I hope this will help you!