fluid icon indicating copy to clipboard operation
fluid copied to clipboard

Use html file directly as source in template

Open pulkitmbd opened this issue 4 years ago • 2 comments

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?

pulkitmbd avatar Jul 06 '21 07:07 pulkitmbd

I wrote this extension method that uses the same IFileProvider as include: https://gist.github.com/deanebarker/174bf177a7c393fdbaadfb44acf16273

parser.ParseFromFile("/path/to/file");

deanebarker avatar Oct 12 '21 17:10 deanebarker

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.

sebastienros avatar Oct 12 '21 17:10 sebastienros