Text Templating

Introduction

ABP Framework provides a simple, yet efficient text template system. Text templating is used to dynamically render contents based on a template and a model (a data object):

Template + Model =renderer=> Rendered Content

It is very similar to an ASP.NET Core Razor View (or Page):

RAZOR VIEW (or PAGE) + MODEL ==render==> HTML CONTENT

You can use the rendered output for any purpose, like sending emails or preparing some reports.

Example

Here, a simple template:

  1. Hello {{model.name}} :)

You can define a class with a Name property to render this template:

  1. public class HelloModel
  2. {
  3. public string Name { get; set; }
  4. }

If you render the template with a HelloModel with the Name is John, the rendered output is will be:

  1. Hello John :)

Template rendering engine is very powerful;

  • It is based on the Scriban library, so it supports conditional logics, loops and much more.
  • Template content can be localized.
  • You can define layout templates to be used as the layout while rendering other templates.
  • You can pass arbitrary objects to the template context (beside the model) for advanced scenarios.

Source Code

Get the source code of the sample application developed and referred through this document.

Installation

It is suggested to use the ABP CLI to install this package.

Using the ABP CLI

Open a command line window in the folder of the project (.csproj file) and type the following command:

  1. abp add-package Volo.Abp.TextTemplating

Manual Installation

If you want to manually install;

  1. Add the Volo.Abp.TextTemplating NuGet package to your project:
  1. Install-Package Volo.Abp.TextTemplating
  1. Add the AbpTextTemplatingModule to the dependency list of your module:
  1. [DependsOn(
  2. //...other dependencies
  3. typeof(AbpTextTemplatingModule) //Add the new module dependency
  4. )]
  5. public class YourModule : AbpModule
  6. {
  7. }

Defining Templates

Before rendering a template, you should define it. Create a class inheriting from the TemplateDefinitionProvider base class:

  1. public class DemoTemplateDefinitionProvider : TemplateDefinitionProvider
  2. {
  3. public override void Define(ITemplateDefinitionContext context)
  4. {
  5. context.Add(
  6. new TemplateDefinition("Hello") //template name: "Hello"
  7. .WithVirtualFilePath(
  8. "/Demos/Hello/Hello.tpl", //template content path
  9. isInlineLocalized: true
  10. )
  11. );
  12. }
  13. }
  • context object is used to add new templates or get the templates defined by depended modules. Used context.Add(...) to define a new template.
  • TemplateDefinition is the class represents a template. Each template must have a unique name (that will be used while you are rendering the template).
  • /Demos/Hello/Hello.tpl is the path of the template file.
  • isInlineLocalized is used to declare if you are using a single template for all languages (true) or different templates for each language (false). See the Localization section below for more.

The Template Content

WithVirtualFilePath indicates that we are using the Virtual File System to store the template content. Create a Hello.tpl file inside your project and mark it as “embedded resource“ on the properties window:

hello-template

Example Hello.tpl content is shown below:

  1. Hello {{model.name}} :)

The Virtual File System requires to add your files in the ConfigureServices method of your module class:

  1. Configure<AbpVirtualFileSystemOptions>(options =>
  2. {
  3. options.FileSets.AddEmbedded<TextTemplateDemoModule>("TextTemplateDemo");
  4. });
  • TextTemplateDemoModule is the module class that you define your template in.
  • TextTemplateDemo is the root namespace of your project.

Rendering the Template

ITemplateRenderer service is used to render a template content.

Example: Rendering a Simple Template

  1. public class HelloDemo : ITransientDependency
  2. {
  3. private readonly ITemplateRenderer _templateRenderer;
  4. public HelloDemo(ITemplateRenderer templateRenderer)
  5. {
  6. _templateRenderer = templateRenderer;
  7. }
  8. public async Task RunAsync()
  9. {
  10. var result = await _templateRenderer.RenderAsync(
  11. "Hello", //the template name
  12. new HelloModel
  13. {
  14. Name = "John"
  15. }
  16. );
  17. Console.WriteLine(result);
  18. }
  19. }
  • HelloDemo is a simple class that injects the ITemplateRenderer in its constructor and uses it inside the RunAsync method.
  • RenderAsync gets two fundamental parameters:
    • templateName: The name of the template to be rendered (Hello in this example).
    • model: An object that is used as the model inside the template (a HelloModel object in this example).

The result shown below for this example:

  1. Hello John :)

Anonymous Model

While it is suggested to create model classes for the templates, it would be practical (and possible) to use anonymous objects for simple cases:

  1. var result = await _templateRenderer.RenderAsync(
  2. "Hello",
  3. new
  4. {
  5. Name = "John"
  6. }
  7. );

In this case, we haven’t created a model class, but created an anonymous object as the model.

PascalCase vs camelCase

PascalCase property names (like UserName) is used as camelCase (like userName) in the templates.

Localization

It is possible to localize a template content based on the current culture. There are two types of localization options described in the following sections.

Inline localization

Inline localization uses the localization system to localize texts inside templates.

Example: Reset Password Link

Assuming you need to send an email to a user to reset her/his password. Here, the template content:

  1. [{{L "ResetMyPassword" model.name}}](/en/abp/latest/{{model.link}})

L function is used to localize the given key based on the current user culture. You need to define the ResetMyPassword key inside your localization file:

  1. "ResetMyPasswordTitle": "Reset my password",
  2. "ResetMyPassword": "Hi {0}, Click here to reset your password"

You also need to declare the localization resource to be used with this template, inside your template definition provider class:

  1. context.Add(
  2. new TemplateDefinition(
  3. "PasswordReset", //Template name
  4. typeof(DemoResource) //LOCALIZATION RESOURCE
  5. ).WithVirtualFilePath(
  6. "/Demos/PasswordReset/PasswordReset.tpl", //template content path
  7. isInlineLocalized: true
  8. )
  9. );

That’s all. When you render this template like that:

  1. var result = await _templateRenderer.RenderAsync(
  2. "PasswordReset", //the template name
  3. new PasswordResetModel
  4. {
  5. Name = "john",
  6. Link = "https://abp.io/example-link?userId=123&token=ABC"
  7. }
  8. );

You will see the localized result:

  1. <a title="Reset my password" href="https://abp.io/example-link?userId=123&token=ABC">Hi john, Click here to reset your password</a>

If you define the default localization resource for your application, then no need to declare the resource type for the template definition.

Multiple Contents Localization

Instead of a single template that uses the localization system to localize the template, you may want to create different template files for each language. It can be needed if the template should be completely different for a specific culture rather than simple text localizations.

Example: Welcome Email Template

Assuming that you want to send a welcome email to your users, but want to define a completely different template based on the user culture.

First, create a folder and put your templates inside it, like en.tpl, tr.tpl… one for each culture you support:

multiple-file-template

Then add your template definition in the template definition provider class:

  1. context.Add(
  2. new TemplateDefinition(
  3. name: "WelcomeEmail",
  4. defaultCultureName: "en"
  5. )
  6. .WithVirtualFilePath(
  7. "/Demos/WelcomeEmail/Templates", //template content folder
  8. isInlineLocalized: false
  9. )
  10. );
  • Set default culture name, so it fallbacks to the default culture if there is no template for the desired culture.
  • Specify the template folder rather than a single template file.
  • Set isInlineLocalized to false for this case.

That’s all, you can render the template for the current culture:

  1. var result = await _templateRenderer.RenderAsync("WelcomeEmail");

Skipped the modal for this example to keep it simple, but you can use models as just explained before.

Specify the Culture

ITemplateRenderer service uses the current culture (CultureInfo.CurrentUICulture) if not specified. If you need, you can specify the culture as the cultureName parameter:

  1. var result = await _templateRenderer.RenderAsync(
  2. "WelcomeEmail",
  3. cultureName: "en"
  4. );

Layout Templates

Layout templates are used to create shared layouts among other templates. It is similar to the layout system in the ASP.NET Core MVC / Razor Pages.

Example: Email HTML Layout Template

For example, you may want to create a single layout for all of your email templates.

First, create a template file just like before:

  1. <!DOCTYPE html>
  2. <html lang="en" xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta charset="utf-8" />
  5. </head>
  6. <body>
  7. {{content}}
  8. </body>
  9. </html>
  • A layout template must have a {{content}} part as a place holder for the rendered child content.

The register your template in the template definition provider:

  1. context.Add(
  2. new TemplateDefinition(
  3. "EmailLayout",
  4. isLayout: true //SET isLayout!
  5. ).WithVirtualFilePath(
  6. "/Demos/EmailLayout/EmailLayout.tpl",
  7. isInlineLocalized: true
  8. )
  9. );

Now, you can use this template as the layout of any other template:

  1. context.Add(
  2. new TemplateDefinition(
  3. name: "WelcomeEmail",
  4. defaultCultureName: "en",
  5. layout: "EmailLayout" //Set the LAYOUT
  6. ).WithVirtualFilePath(
  7. "/Demos/WelcomeEmail/Templates",
  8. isInlineLocalized: false
  9. )
  10. );

Global Context

ABP passes the model that can be used to access to the model inside the template. You can pass more global variables if you need.

An example template content:

  1. A global object value: {{myGlobalObject}}

This template assumes that that is a myGlobalObject object in the template rendering context. You can provide it like shown below:

  1. var result = await _templateRenderer.RenderAsync(
  2. "GlobalContextUsage",
  3. globalContext: new Dictionary<string, object>
  4. {
  5. {"myGlobalObject", "TEST VALUE"}
  6. }
  7. );

The rendering result will be:

  1. A global object value: TEST VALUE

Replacing the Existing Templates

It is possible to replace a template defined by a module that used in your application. In this way, you can customize the templates based on your requirements without changing the module code.

Option-1: Using the Virtual File System

The Virtual File System allows you to override any file by placing the same file into the same path in your project.

Example: Replace the Standard Email Layout Template

ABP Framework provides an email sending system that internally uses the text templating to render the email content. It defines a standard email layout template in the /Volo/Abp/Emailing/Templates/Layout.tpl path. The unique name of the template is Abp.StandardEmailTemplates.Layout and this string is defined as a constant on the Volo.Abp.Emailing.Templates.StandardEmailTemplates static class.

Do the following steps to replace the template file with your own;

1) Add a new file into the same location (/Volo/Abp/Emailing/Templates/Layout.tpl) in your project:

replace-email-layout

2) Prepare your email layout template:

  1. <!DOCTYPE html>
  2. <html lang="en" xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta charset="utf-8" />
  5. </head>
  6. <body>
  7. <h1>This my header</h1>
  8. {{content}}
  9. <footer>
  10. This is my footer...
  11. </footer>
  12. </body>
  13. </html>

This example simply adds a header and footer to the template and renders the content between them (see the Layout Templates section above to understand it).

3) Configure the embedded resources in the .csproj file

  • Add Microsoft.Extensions.FileProviders.Embedded NuGet package to the project.
  • Add <GenerateEmbeddedFilesManifest>true</GenerateEmbeddedFilesManifest> into the <PropertyConfig>...</PropertyConfig> section of your .csproj file.
  • Add the following code into your .csproj file:
  1. <ItemGroup>
  2. <None Remove="Volo\Abp\Emailing\Templates\*.tpl" />
  3. <EmbeddedResource Include="Volo\Abp\Emailing\Templates\*.tpl" />
  4. </ItemGroup>

This makes the template files “embedded resource”.

4) Configure the virtual file system

Configure the AbpVirtualFileSystemOptions in the ConfigureServices method of your module to add the embedded files into the virtual file system:

  1. Configure<AbpVirtualFileSystemOptions>(options =>
  2. {
  3. options.FileSets.AddEmbedded<BookStoreDomainModule>();
  4. });

BookStoreDomainModule should be your module name, in this example code.

Be sure that your module (directly or indirectly) depends on the AbpEmailingModule. Because the VFS can override files based on the dependency order.

Now, your template will be used when you want to render the email layout template.

Option-2: Using the Template Definition Provider

You can create a template definition provider class that gets the email layout template and changes the virtual file path for the template.

Example: Use the /MyTemplates/EmailLayout.tpl file instead of the standard template

  1. using Volo.Abp.DependencyInjection;
  2. using Volo.Abp.Emailing.Templates;
  3. using Volo.Abp.TextTemplating;
  4. namespace MyProject
  5. {
  6. public class MyTemplateDefinitionProvider
  7. : TemplateDefinitionProvider, ITransientDependency
  8. {
  9. public override void Define(ITemplateDefinitionContext context)
  10. {
  11. var emailLayoutTemplate = context.GetOrNull(StandardEmailTemplates.Layout);
  12. emailLayoutTemplate
  13. .WithVirtualFilePath(
  14. "/MyTemplates/EmailLayout.tpl",
  15. isInlineLocalized: true
  16. );
  17. }
  18. }
  19. }

You should still add the file /MyTemplates/EmailLayout.tpl to the virtual file system as explained before. This approach allows you to locate templates in any folder instead of the folder defined by the depended module.

Beside the template content, you can manipulate the template definition properties, like DisplayName, Layout or LocalizationSource.

Advanced Features

This section covers some internals and more advanced usages of the text templating system.

Template Content Provider

ITemplateRenderer is used to render the template, which is what you want for most of the cases. However, you can use the ITemplateContentProvider to get the raw (not rendered) template contents.

ITemplateContentProvider is internally used by the ITemplateRenderer to get the raw template contents.

Example:

  1. public class TemplateContentDemo : ITransientDependency
  2. {
  3. private readonly ITemplateContentProvider _templateContentProvider;
  4. public TemplateContentDemo(ITemplateContentProvider templateContentProvider)
  5. {
  6. _templateContentProvider = templateContentProvider;
  7. }
  8. public async Task RunAsync()
  9. {
  10. var result = await _templateContentProvider
  11. .GetContentOrNullAsync("Hello");
  12. Console.WriteLine(result);
  13. }
  14. }

The result will be the raw template content:

  1. Hello {{model.name}} :)
  • GetContentOrNullAsync returns null if no content defined for the requested template.
  • It can get a cultureName parameter that is used if template has different files for different cultures (see Multiple Contents Localization section above).

Template Content Contributor

ITemplateContentProvider service uses ITemplateContentContributor implementations to find template contents. There is a single pre-implemented content contributor, VirtualFileTemplateContentContributor, which gets template contents from the virtual file system as described above.

You can implement the ITemplateContentContributor to read raw template contents from another source.

Example:

  1. public class MyTemplateContentProvider
  2. : ITemplateContentContributor, ITransientDependency
  3. {
  4. public async Task<string> GetOrNullAsync(TemplateContentContributorContext context)
  5. {
  6. var templateName = context.TemplateDefinition.Name;
  7. //TODO: Try to find content from another source
  8. return null;
  9. }
  10. }

Return null if your source can not find the content, so ITemplateContentProvider fallbacks to the next contributor.

Template Definition Manager

ITemplateDefinitionManager service can be used to get the template definitions (created by the template definition providers).

See Also