fluid
fluid copied to clipboard
Use html file directly as source in template
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
var parser = new FluidParser();
TemplateOptions options = new TemplateOptions();
var fileProvider = new PhysicalFileProvider(Environment.CurrentDirectory);
options.FileProvider = fileProvider;
var model = new { Firstname = "Bill", Lastname = "Gates" };
var source = File.ReadAllText("EmailTemplate.html");
if (parser.TryParse(source, out var template, out var error))
{
var context = new TemplateContext(model, options);
Console.WriteLine(template.Render(context));
Console.WriteLine("Parsed successfully");
}
else
{
Console.WriteLine($"Error: {error}");
}
}
In this code instead of converting file into string can we directly use the html file as template?
I wrote this extension method that uses the same IFileProvider as include: https://gist.github.com/deanebarker/174bf177a7c393fdbaadfb44acf16273
parser.ParseFromFile("/path/to/file");
It's actually a feature that is provided by the "view" engine since it will also require extensibility for caching the files. Hence the template engine itself doesn't provide that out of the box.
Not closing until it's ready, currently improving it.