Skip to content

Commit 541fed9

Browse files
authored
Merge pull request #22 from geeklearningio/release/0.5.0
Release/0.5.0
2 parents 4a5c03c + 5bdf081 commit 541fed9

35 files changed

+340
-366
lines changed

GeekLearning.Templating.sln

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,12 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
3-
# Visual Studio 14
4-
VisualStudioVersion = 14.0.25420.1
3+
# Visual Studio 15
4+
VisualStudioVersion = 15.0.26430.6
55
MinimumVisualStudioVersion = 10.0.40219.1
6-
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "GeekLearning.Templating", "src\GeekLearning.Templating\GeekLearning.Templating.xproj", "{369E8A80-AC62-4F8E-9CE3-AE5E6A35925E}"
7-
EndProject
8-
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "GeekLearning.Templating.BasicSample", "samples\GeekLearning.Templating.BasicSample\GeekLearning.Templating.BasicSample.xproj", "{45A8B12E-ED81-4D4B-A784-23DB221EBA6F}"
9-
EndProject
106
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "samples", "samples", "{E0BAC0FB-2A34-4E10-A2D2-4E587E50A47A}"
117
EndProject
128
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{432F4069-691C-498A-8017-81E41CBF0CCD}"
139
EndProject
14-
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "GeekLearning.Templating.Handlebars", "src\GeekLearning.Templating.Handlebars\GeekLearning.Templating.Handlebars.xproj", "{E72256FA-DFF3-4487-AA94-E6D5BD1BDD2D}"
15-
EndProject
16-
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "GeekLearning.Templating.Mustache", "src\GeekLearning.Templating.Mustache\GeekLearning.Templating.Mustache.xproj", "{515925E3-4359-4079-A24C-E7367560C95F}"
17-
EndProject
1810
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "items", "items", "{A75429E6-34A4-43EC-AA7E-6DD4FFAF8704}"
1911
ProjectSection(SolutionItems) = preProject
2012
.gitattributes = .gitattributes
@@ -25,6 +17,14 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "items", "items", "{A75429E6
2517
README.md = README.md
2618
EndProjectSection
2719
EndProject
20+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "GeekLearning.Templating", "src\GeekLearning.Templating\GeekLearning.Templating.csproj", "{369E8A80-AC62-4F8E-9CE3-AE5E6A35925E}"
21+
EndProject
22+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "GeekLearning.Templating.BasicSample", "samples\GeekLearning.Templating.BasicSample\GeekLearning.Templating.BasicSample.csproj", "{45A8B12E-ED81-4D4B-A784-23DB221EBA6F}"
23+
EndProject
24+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "GeekLearning.Templating.Handlebars", "src\GeekLearning.Templating.Handlebars\GeekLearning.Templating.Handlebars.csproj", "{E72256FA-DFF3-4487-AA94-E6D5BD1BDD2D}"
25+
EndProject
26+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "GeekLearning.Templating.Mustache", "src\GeekLearning.Templating.Mustache\GeekLearning.Templating.Mustache.csproj", "{515925E3-4359-4079-A24C-E7367560C95F}"
27+
EndProject
2828
Global
2929
GlobalSection(SolutionConfigurationPlatforms) = preSolution
3030
Debug|Any CPU = Debug|Any CPU

NuGet.Config

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<configuration>
3+
<packageSources>
4+
<add key="GL Unstable" value="https://www.myget.org/F/gl-unstable/api/v3/index.json" />
5+
</packageSources>
6+
</configuration>

README.md

Lines changed: 129 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,135 @@
55

66
# gl-dotnet-templating
77

8-
Coming Soon!
8+
This templating library is an abstraction above various templating engines and our
9+
[storage library](https://github.com/geeklearningio/gl-dotnet-storage). It handles loading
10+
from any location supported by our storage library. It will also transparently cache templates
11+
for you.
912

10-
#Templating Abstraction
13+
It has support for partials, files prefixed with an underscore `_` will be considered as partials
14+
and loaded as such.
1115

12-
#Handlebars provider
16+
## Getting started
1317

14-
#Mustache.js provider
18+
In your project.json add required dependencies :
19+
```
20+
"GeekLearning.Storage.FileSystem": "0.5.0",
21+
22+
"GeekLearning.Templating": "0.5.0",
23+
"GeekLearning.Templating.Handlebars": "0.5.0",
24+
```
25+
26+
Then add required settings in your `appsettings.json` file. In this example, we will
27+
use FileSystem provider to configure a storage provider which will load files from
28+
a `Templates` folder relative to Application Root. This could be configured to use
29+
an Azure Container instead (see storage documentation).
30+
31+
```json
32+
"Storage": {
33+
"Stores": {
34+
"Templates": {
35+
"Provider": "FileSystem",
36+
"Parameters": {
37+
"Path": "Templates"
38+
}
39+
}
40+
}
41+
}
42+
```
43+
44+
We will then create an `EmailTemplates` class. It will be based on `TemplateCollectionBase`
45+
which eliminates most of the boilerplate code required to load and execute a template.
46+
47+
```csharp
48+
public class EmailTemplates : TemplateCollectionBase
49+
{
50+
public EmailTemplates(IStorageFactory storageFactory, ITemplateLoaderFactory templateLoaderFactory) : base("Templates", storageFactory, templateLoaderFactory)
51+
{
52+
53+
}
54+
55+
public Task<string> ApplyInvitationTemplate(InvitationContext context)
56+
{
57+
return this.LoadAndApplyTemplate("invitation", context);
58+
}
59+
60+
public Task<string> ApplyInvitation2Template(InvitationContext context)
61+
{
62+
return this.LoadAndApplyTemplate("invitation2", context);
63+
}
64+
65+
public Task<string> ApplyInvitation3Template(InvitationContext context)
66+
{
67+
return this.LoadAndApplyTemplate("SubDir/invitation", context);
68+
}
69+
}
70+
```
71+
72+
Then in your `Startup.cs` file add required dependencies and configuration to the DI container.
73+
74+
```csharp
75+
services.AddMemoryCache();
76+
services.AddStorage().AddFileSystemStorage(HostingEnvironment.ContentRootPath).AddAzureStorage();
77+
services.AddTemplating().AddMustache().AddHandlebars();
78+
services.AddScoped<EmailTemplates>();
79+
80+
services.Configure<StorageOptions>(Configuration.GetSection("Storage"));
81+
```
82+
83+
## Templating Abstraction
84+
85+
Our abstraction is composed of a few interface and a base class to reduce boilerplate code when
86+
working with templates.
87+
88+
First interface is `ITemplateLoaderFactory`.
89+
90+
```csharp
91+
public interface ITemplateLoaderFactory
92+
{
93+
ITemplateLoader Create(IStore store);
94+
}
95+
```
96+
97+
You can use this interface to get an `ITemplateLoader` for a specific `IStore` (see storage documentation)
98+
99+
```csharp
100+
public interface ITemplateLoader
101+
{
102+
Task<ITemplate> GetTemplate(string name);
103+
}
104+
```
105+
106+
Once you have an `ITemplateLoader` interface you can retrieve a specific template by is name.
107+
If it's not already loaded, it will be read from the `IStore`.
108+
109+
```csharp
110+
public interface ITemplate
111+
{
112+
string Apply(object context);
113+
}
114+
```
115+
116+
A `ITemplate` reference can be executed on a specific `context` using `Apply` method.
117+
118+
## Handlebars provider
119+
120+
`Handlebars` provider allows you to load `.hbs` templates
121+
(see [Handlebars.js](http://handlebarsjs.com/)). It relies on Handlebars.Net [port](https://github.com/rexm/Handlebars.Net).
122+
123+
This is our recommanded option as it compiles templates for better performance.
124+
125+
## Mustache.js provider
126+
127+
`Mustache.js` provider is a mustache provider allowing you to load `.mustache` templates. It is based on
128+
[mustache sharp](https://github.com/jehugaleahsa/mustache-sharp) work. As we've go no answer
129+
from author, we are maintaining dotnet core support on our [own fork](https://github.com/sandorfr/mustache-sharp).
130+
131+
A good thing is it has no dependency on Emit or Expression so it might be usefull on some
132+
edge cases.
133+
134+
**Note that it does not support partials.**
135+
136+
## Roadmap
137+
138+
* Design and add Helpers support for Handlebars Engine.
139+
* Additional providers (haml through NHAML or NVelocity).

global.json

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1 @@
1-
{
2-
"projects": [ "src", "samples" ],
3-
"sdk": { "version": "1.0.0-preview2-003121" }
4-
}
1+
{ "projects": [ "src", "samples" ] }

samples/GeekLearning.Templating.BasicSample/Controllers/TestController.cs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Threading.Tasks;
5-
using Microsoft.AspNetCore.Mvc;
6-
7-
namespace GeekLearning.Templating.BasicSample.Controllers
1+
namespace GeekLearning.Templating.BasicSample.Controllers
82
{
3+
using Microsoft.AspNetCore.Mvc;
4+
using System.Threading.Tasks;
5+
96
[Route("api/[controller]")]
107
public class TestController : Controller
118
{
@@ -16,7 +13,6 @@ public TestController(EmailTemplates templates)
1613
this.templates = templates;
1714
}
1815

19-
// POST api/values
2016
[HttpGet]
2117
public async Task<string> Get([FromQuery]InvitationContext value)
2218
{
@@ -30,7 +26,6 @@ public async Task<string> Get([FromQuery]InvitationContext value)
3026
}
3127
}
3228

33-
// POST api/values
3429
[HttpGet("2")]
3530
public async Task<string> Get2([FromQuery]InvitationContext value)
3631
{

samples/GeekLearning.Templating.BasicSample/EmailTemplates.cs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
1-
using GeekLearning.Storage;
2-
using System;
3-
using System.Collections.Generic;
4-
using System.Linq;
5-
using System.Threading.Tasks;
6-
7-
namespace GeekLearning.Templating.BasicSample
1+
namespace GeekLearning.Templating.BasicSample
82
{
3+
using Storage;
4+
using System.Threading.Tasks;
5+
96
public class EmailTemplates : TemplateCollectionBase
107
{
118
public EmailTemplates(IStorageFactory storageFactory, ITemplateLoaderFactory templateLoaderFactory) : base("Templates", storageFactory, templateLoaderFactory)
129
{
13-
1410
}
1511

1612
public Task<string> ApplyInvitationTemplate(InvitationContext context)
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netcoreapp1.1</TargetFramework>
5+
<PreserveCompilationContext>true</PreserveCompilationContext>
6+
<AssemblyName>GeekLearning.Templating.BasicSample</AssemblyName>
7+
<OutputType>Exe</OutputType>
8+
<PackageId>GeekLearning.Templating.BasicSample</PackageId>
9+
<RuntimeFrameworkVersion>1.1.1</RuntimeFrameworkVersion>
10+
<PackageTargetFallback>$(PackageTargetFallback);portable-net45+win8</PackageTargetFallback>
11+
</PropertyGroup>
12+
13+
<ItemGroup>
14+
<None Update="wwwroot\**\*;Views">
15+
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
16+
</None>
17+
</ItemGroup>
18+
19+
<ItemGroup>
20+
<ProjectReference Include="..\..\src\GeekLearning.Templating\GeekLearning.Templating.csproj" />
21+
<ProjectReference Include="..\..\src\GeekLearning.Templating.Handlebars\GeekLearning.Templating.Handlebars.csproj" />
22+
<ProjectReference Include="..\..\src\GeekLearning.Templating.Mustache\GeekLearning.Templating.Mustache.csproj" />
23+
</ItemGroup>
24+
25+
<ItemGroup>
26+
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="1.1.3" />
27+
<PackageReference Include="Microsoft.AspNetCore.Server.IISIntegration" Version="1.1.2" />
28+
<PackageReference Include="Microsoft.AspNetCore.Server.Kestrel" Version="1.1.2" />
29+
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="1.1.2" />
30+
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="1.1.2" />
31+
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="1.1.2" />
32+
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="1.1.2" />
33+
<PackageReference Include="Microsoft.Extensions.Logging" Version="1.1.2" />
34+
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="1.1.2" />
35+
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="1.1.2" />
36+
<PackageReference Include="GeekLearning.Storage.FileSystem" Version="0.7.0-alpha0026" />
37+
<PackageReference Include="GeekLearning.Storage.Azure" Version="0.7.0-alpha0026" />
38+
</ItemGroup>
39+
40+
</Project>

samples/GeekLearning.Templating.BasicSample/GeekLearning.Templating.BasicSample.xproj

Lines changed: 0 additions & 19 deletions
This file was deleted.

samples/GeekLearning.Templating.BasicSample/InvitationContext.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Threading.Tasks;
5-
6-
namespace GeekLearning.Templating.BasicSample
1+
namespace GeekLearning.Templating.BasicSample
72
{
83
public class InvitationContext
94
{

samples/GeekLearning.Templating.BasicSample/Program.cs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.IO;
4-
using System.Linq;
5-
using System.Threading.Tasks;
6-
using Microsoft.AspNetCore.Hosting;
7-
using Microsoft.AspNetCore.Builder;
8-
9-
namespace GeekLearning.Templating.BasicSample
1+
namespace GeekLearning.Templating.BasicSample
102
{
3+
using Microsoft.AspNetCore.Builder;
4+
using Microsoft.AspNetCore.Hosting;
5+
using System.IO;
6+
117
public class Program
128
{
139
public static void Main(string[] args)

0 commit comments

Comments
 (0)