From 8966512f9d1653baee68eb3a438396cc245c9497 Mon Sep 17 00:00:00 2001 From: Nick Lund Stenroos-Dam Date: Thu, 22 Dec 2022 10:40:55 +0100 Subject: [PATCH 1/2] Added failing test for rendering inline blocks #524 --- .../ViewEngine/ViewEngineTests.cs | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/source/Handlebars.Test/ViewEngine/ViewEngineTests.cs b/source/Handlebars.Test/ViewEngine/ViewEngineTests.cs index 50ad1ea5..f6bcf5d5 100644 --- a/source/Handlebars.Test/ViewEngine/ViewEngineTests.cs +++ b/source/Handlebars.Test/ViewEngine/ViewEngineTests.cs @@ -73,6 +73,28 @@ public void CanLoadAViewWithALayoutInTheRoot() Assert.Equal("layout start\r\nThis is the body\r\nlayout end", output); } + [Fact] + public void CanRenderInlineBlocks() + { + // This sample is based on https://handlebarsjs.com/examples/partials/inline-blocks.html + + var files = new FakeFileSystem() + { + //Given a layout in a subfolder + { "partials/layout.hbs", "
\r\n{{> nav}}\r\n
\r\n
\r\n{{> content}}\r\n
"}, + + { "template.hbs", "{{#> layout}}\r\n{{#*inline \"nav\"}}\r\nMy Nav\r\n{{/inline}}\r\n{{#*inline \"content\"}}\r\nMy Content\r\n{{/inline}}\r\n{{/layout}}"} + }; + + //When a viewengine renders that view + var handleBars = Handlebars.Create(new HandlebarsConfiguration() { FileSystem = files }); + var renderView = handleBars.CompileView("template.hbs"); + var output = renderView(null); + + //Then the correct output should be rendered + Assert.Equal("
\r\nMy Nav\r\n
\r\n
\r\nMy Content\r\n
", output); + } + [Fact] public void CanLoadAViewWithALayoutWithAVariable() { From b2a73bdad6d2faaba7eb7f6ab396a5ff37f93d84 Mon Sep 17 00:00:00 2001 From: Nick Lund Stenroos-Dam Date: Thu, 22 Dec 2022 15:14:27 +0100 Subject: [PATCH 2/2] Inline blocks should not be additionally escaped --- source/Handlebars.Test/ViewEngine/ViewEngineTests.cs | 9 ++++++--- source/Handlebars/FileSystemPartialTemplateResolver.cs | 2 +- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/source/Handlebars.Test/ViewEngine/ViewEngineTests.cs b/source/Handlebars.Test/ViewEngine/ViewEngineTests.cs index f6bcf5d5..40182e12 100644 --- a/source/Handlebars.Test/ViewEngine/ViewEngineTests.cs +++ b/source/Handlebars.Test/ViewEngine/ViewEngineTests.cs @@ -83,16 +83,19 @@ public void CanRenderInlineBlocks() //Given a layout in a subfolder { "partials/layout.hbs", "
\r\n{{> nav}}\r\n
\r\n
\r\n{{> content}}\r\n
"}, - { "template.hbs", "{{#> layout}}\r\n{{#*inline \"nav\"}}\r\nMy Nav\r\n{{/inline}}\r\n{{#*inline \"content\"}}\r\nMy Content\r\n{{/inline}}\r\n{{/layout}}"} + { "template.hbs", "{{#> layout}}\r\n{{#*inline \"nav\"}}\r\n{{Text}}\r\n{{/inline}}\r\n{{#*inline \"content\"}}\r\nMy Content\r\n{{/inline}}\r\n{{/layout}}"} }; //When a viewengine renders that view var handleBars = Handlebars.Create(new HandlebarsConfiguration() { FileSystem = files }); var renderView = handleBars.CompileView("template.hbs"); - var output = renderView(null); + var output = renderView(new Dictionary + { + { "Text", "" } + }); //Then the correct output should be rendered - Assert.Equal("
\r\nMy Nav\r\n
\r\n
\r\nMy Content\r\n
", output); + Assert.Equal("
\r\n<My Nav>\r\n
\r\n
\r\nMy Content\r\n
", output); } [Fact] diff --git a/source/Handlebars/FileSystemPartialTemplateResolver.cs b/source/Handlebars/FileSystemPartialTemplateResolver.cs index 80bde51e..8b0338e9 100644 --- a/source/Handlebars/FileSystemPartialTemplateResolver.cs +++ b/source/Handlebars/FileSystemPartialTemplateResolver.cs @@ -27,7 +27,7 @@ public bool TryRegisterPartial(IHandlebars env, string partialName, string templ handlebarsTemplateRegistrations.RegisteredTemplates.AddOrReplace(partialName, (writer, o, data) => { - writer.Write(compiled(o, data)); + ((EncodedTextWriterWrapper)writer).Write(compiled(o, data), false); }); return true;