Coder Social home page Coder Social logo

stubble.extensions.loaders's People

Contributors

romanx avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Forkers

bubdm

stubble.extensions.loaders's Issues

Add loader for nested file paths

Add a file loader for nested file paths. Have them split on a delimiter for folder structures so it'd be like

  • Directory A
    • Directory B
      • MyFile.mustache

The identifier for the template becomes Directory A:Directory B:MyFile.

ASP.NET Core file loader

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();
            }
        }
    }
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.