Last active
October 9, 2021 13:55
-
-
Save deanebarker/174bf177a7c393fdbaadfb44acf16273 to your computer and use it in GitHub Desktop.
Allows retrieval of the initial Fluid template using the declared IFileProvider
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// var parser = new FluidParser(); | |
// parser.ParseFromFile("deane"); | |
namespace DeaneBarker.Fluid | |
{ | |
public static class FluidParserExtensions | |
{ | |
public static IFluidTemplate ParseFromFile(this FluidParser parser, string path) | |
{ | |
if(TemplateOptions.Default.FileProvider == null) | |
{ | |
throw new Exception("You have to set TemplateOptions.FileProvider first."); | |
} | |
var fileProvider = TemplateOptions.Default.FileProvider; | |
var fileInfo = TemplateOptions.Default.FileProvider.GetFileInfo(path); | |
if (fileInfo == null || !fileInfo.Exists) | |
{ | |
throw new FileNotFoundException(path); | |
} | |
var template = ""; | |
using (var stream = fileInfo.CreateReadStream()) | |
using (var streamReader = new StreamReader(stream)) | |
{ | |
template = streamReader.ReadToEnd(); | |
} | |
return parser.Parse(template); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment