Skip to content

Commit

Permalink
Finalize the TextTemplateDemo
Browse files Browse the repository at this point in the history
  • Loading branch information
hikalkan committed May 13, 2020
1 parent 9ea5839 commit eab99a7
Show file tree
Hide file tree
Showing 8 changed files with 118 additions and 7 deletions.
27 changes: 22 additions & 5 deletions TextTemplateDemo/DemoTemplateDefinitionProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ public override void Define(ITemplateDefinitionContext context)
new TemplateDefinition(
name: "PasswordReset",
localizationResource: typeof(DemoResource)
)
.WithVirtualFilePath(
).WithVirtualFilePath(
"/Demos/PasswordReset/PasswordReset.tpl", //template content path
isInlineLocalized: true
)
Expand All @@ -29,13 +28,31 @@ public override void Define(ITemplateDefinitionContext context)
context.Add(
new TemplateDefinition(
name: "WelcomeEmail",
defaultCultureName: "en"
)
.WithVirtualFilePath(
defaultCultureName: "en",
layout: "EmailLayout" //Set the LAYOUT
).WithVirtualFilePath(
"/Demos/WelcomeEmail/Templates", //template content folder
isInlineLocalized: false
)
);

context.Add(
new TemplateDefinition(
"EmailLayout",
isLayout: true
).WithVirtualFilePath(
"/Demos/EmailLayout/EmailLayout.tpl", //template content path
isInlineLocalized: true
)
);

context.Add(
new TemplateDefinition("GlobalContextUsage")
.WithVirtualFilePath(
"/Demos/GlobalContext/GlobalContextUsage.tpl",
isInlineLocalized: true
)
);
}
}
}
9 changes: 9 additions & 0 deletions TextTemplateDemo/Demos/EmailLayout/EmailLayout.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
</head>
<body>
{{content}}
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
A global object value: {{myGlobalObject}}
31 changes: 31 additions & 0 deletions TextTemplateDemo/Demos/GlobalContext/GlobalContextUsageDemo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Volo.Abp.DependencyInjection;
using Volo.Abp.TextTemplating;

namespace TextTemplateDemo.Demos.GlobalContext
{
public class GlobalContextUsageDemo : ITransientDependency
{
private readonly ITemplateRenderer _templateRenderer;

public GlobalContextUsageDemo(ITemplateRenderer templateRenderer)
{
_templateRenderer = templateRenderer;
}

public async Task RunAsync()
{
var result = await _templateRenderer.RenderAsync(
"GlobalContextUsage",
globalContext: new Dictionary<string, object>
{
{"myGlobalObject", "TEST VALUE"}
}
);

Console.WriteLine(result);
}
}
}
25 changes: 25 additions & 0 deletions TextTemplateDemo/Demos/TemplateContent/TemplateContentDemo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System;
using System.Threading.Tasks;
using Volo.Abp.DependencyInjection;
using Volo.Abp.TextTemplating;

namespace TextTemplateDemo.Demos.TemplateContent
{
public class TemplateContentDemo : ITransientDependency
{
private readonly ITemplateContentProvider _templateContentProvider;

public TemplateContentDemo(ITemplateContentProvider templateContentProvider)
{
_templateContentProvider = templateContentProvider;
}

public async Task RunAsync()
{
var result = await _templateContentProvider
.GetContentOrNullAsync("Hello");

Console.WriteLine(result);
}
}
}
17 changes: 17 additions & 0 deletions TextTemplateDemo/Other/MyTemplateContentProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System.Threading.Tasks;
using Volo.Abp.DependencyInjection;
using Volo.Abp.TextTemplating;

namespace TextTemplateDemo.Other
{
public class MyTemplateContentProvider : ITemplateContentContributor, ITransientDependency
{
public async Task<string> GetOrNullAsync(TemplateContentContributorContext context)
{
var templateName = context.TemplateDefinition.Name;

//TODO: Try to find content from another source
return null;
}
}
}
8 changes: 8 additions & 0 deletions TextTemplateDemo/Program.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using System;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using TextTemplateDemo.Demos.GlobalContext;
using TextTemplateDemo.Demos.Hello;
using TextTemplateDemo.Demos.PasswordReset;
using TextTemplateDemo.Demos.TemplateContent;
using TextTemplateDemo.Demos.WelcomeEmail;
using Volo.Abp;

Expand All @@ -22,6 +24,8 @@ static async Task Main(string[] args)
var helloDemo = application.ServiceProvider.GetRequiredService<HelloDemo>();
var passwordResetDemo = application.ServiceProvider.GetRequiredService<PasswordResetDemo>();
var welcomeEmailDemo = application.ServiceProvider.GetRequiredService<WelcomeEmailDemo>();
var globalContextDemo = application.ServiceProvider.GetRequiredService<GlobalContextUsageDemo>();
var templateContentDemo = application.ServiceProvider.GetRequiredService<TemplateContentDemo>();

await helloDemo.RunAsync();
await helloDemo.RunWithAnonymousModelAsync();
Expand All @@ -31,6 +35,10 @@ static async Task Main(string[] args)
await welcomeEmailDemo.RunAsync("en");
await welcomeEmailDemo.RunAsync("tr");

await globalContextDemo.RunAsync();

await templateContentDemo.RunAsync();

Console.WriteLine();
Console.WriteLine("Press enter to exit...");
Console.ReadLine();
Expand Down
7 changes: 5 additions & 2 deletions TextTemplateDemo/TextTemplateDemo.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
Expand All @@ -8,8 +8,11 @@
<ItemGroup>
<EmbeddedResource Include="Demos\**\*.tpl" />
<EmbeddedResource Include="Localization\*.json" />
</ItemGroup>
</ItemGroup>

<ItemGroup>
<None Remove="Demos\GlobalContext\GlobalContextUsage.tpl" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Volo.Abp.Autofac" Version="2.7.0" />
Expand Down

0 comments on commit eab99a7

Please sign in to comment.