stubbleorg / stubble.extensions.loaders Goto Github PK
View Code? Open in Web Editor NEWA variety of template loaders to extend Stubble
License: MIT License
A variety of template loaders to extend Stubble
License: MIT License
Add a file loader for nested file paths. Have them split on a delimiter for folder structures so it'd be like
The identifier for the template becomes Directory A:Directory B:MyFile
.
I wanted a template loader that integrated well with ASP.NET Core, so I wrote one myself.
It probably does not belong in this package, since it would add a dependency on ASP.NET Core packages. But since I like the Stubble project and it appears to currently be the only mustache template library available for .NET Core, I thought I would contribute a little back ๐
using System.Collections.Concurrent;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Options;
using Stubble.Core.Interfaces;
namespace TeamString.Admin.Email
{
/// <summary>
/// A template loader for Stubble that locates templates based on a base path relative to the
/// webapp content root. This loader integrates much better with ASP.NET Core apps than the
/// FileSystemLoader provided in Stubble extensions.
/// </summary>
public sealed class AspNetCoreTemplateLoader : IStubbleLoader
{
private readonly string templateBasePath;
private readonly ConcurrentDictionary<string, string> templateCache;
public AspNetCoreTemplateLoader(IOptions<StubbleOptions> options, IHostingEnvironment hostEnv)
{
this.templateBasePath = Path.Combine(hostEnv.ContentRootPath, options.Value.TemplateBasePath);
this.templateCache = new ConcurrentDictionary<string, string>();
}
private AspNetCoreTemplateLoader(string templateBasePath, ConcurrentDictionary<string, string> templateCache)
{
this.templateBasePath = templateBasePath;
this.templateCache = templateCache;
}
public IStubbleLoader Clone()
{
return new AspNetCoreTemplateLoader(templateBasePath, templateCache);
}
public string Load(string name)
{
string template = null;
if (!templateCache.TryGetValue(name, out template))
{
template = LoadTextFile(name);
templateCache.AddOrUpdate(name, template, (k, v) => template);
}
return template;
}
public ValueTask<string> LoadAsync(string name)
{
string template = null;
if (templateCache.TryGetValue(name, out template))
{
return new ValueTask<string>(template);
}
else
{
return new ValueTask<string>(LoadFromFileAsync(name));
}
}
// TODO: Move this method inside LoadAsync as a local function once we move to C# 7
// See: http://thebillwagner.com/Blog/Item/2016-03-02-C7FeatureProposalLocalFunctions
private async Task<string> LoadFromFileAsync(string name)
{
string template = await LoadTextFileAsync(name);
templateCache.AddOrUpdate(name, template, (k, v) => template);
return template;
}
private string GetFullPath(string templateName) => Path.Combine(templateBasePath, templateName);
private string LoadTextFile(string templateName) => File.ReadAllText(GetFullPath(templateName), Encoding.UTF8);
private async Task<string> LoadTextFileAsync(string templateName)
{
using (var file = new FileStream(GetFullPath(templateName), FileMode.Open, FileAccess.Read, FileShare.Read, bufferSize: 4096, useAsync: true))
using (var reader = new StreamReader(file, Encoding.UTF8, detectEncodingFromByteOrderMarks: false, bufferSize: 4096, leaveOpen: true))
{
return await reader.ReadToEndAsync();
}
}
}
}
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.