oformaniuk / Handlebars.Net

A real .NET Handlebars engine
MIT License
3 stars 1 forks source link

Layout when using TextWriter overloads #6

Closed fhogberg closed 4 years ago

fhogberg commented 4 years ago

Tried going to the stream version of Compile() and the following does not work. Should it work or is this not supported?

        [Fact]
        public void CanLoadAViewWithALayout()
        {
            //When a viewengine renders that view
            var handleBars = Handlebars.Create();
            handleBars.RegisterTemplate("somelayout", "layout start\r\n{ { { body} } }\r\nlayout end");
            var templateStream = new StringReader("{{!< somelayout}}This is the body");

            var renderView = handleBars.Compile(templateStream);
            var sb = new StringBuilder();
            var sw = new StringWriter(sb);
            renderView(sw, null);
            var output = sb.ToString();

            //Then the correct output should be rendered
            Assert.Equal("layout start\r\nThis is the body\r\nlayout end", output);
        }
oformaniuk commented 4 years ago

Unfortunately layout is only supported in CompileView scenario. For reference please see CanLoadAViewWithALayout. Unfortunately Stream API is not supported in CompileView.

It is possible to add support by extending ViewEngineFileSystem base class with something like this:

public virtual StringReader GetFileStream(string filename)
{
    return new StringReader(GetFileContent(filename));
}

and update usage at HandlebarsCompiler.CompileView to use GetFileStream instead of GetFileContent. Other implementation options could require breaking changes.

If this functionality is essential for you - you're welcome to create PR. If not I'd address this in next iteration.

fhogberg commented 4 years ago

Ok. Thanks for the info. If i get the time i try to make a PR for it.

fhogberg commented 4 years ago

8 PR Submitted

fhogberg commented 4 years ago

Perhaps the PR looks strange but what i needed was a Compile that returned a Action<TextWriter,object> and worked with templates. To read templates from a reader was never a concern.