diff --git a/src/ManiaTemplates/Lib/MtTransformer.cs b/src/ManiaTemplates/Lib/MtTransformer.cs index 26e326d..155623b 100644 --- a/src/ManiaTemplates/Lib/MtTransformer.cs +++ b/src/ManiaTemplates/Lib/MtTransformer.cs @@ -57,7 +57,9 @@ public string BuildManialink(MtComponent rootComponent, string className, int ve var body = ProcessNode( XmlStringToNode(rootComponent.TemplateContent), _engine.BaseMtComponents.Overload(rootComponent.ImportedComponents), - rootContext + rootContext, + parentComponent: rootComponent, + isRootNode: true ); var template = new Snippet @@ -234,12 +236,12 @@ private string CreateSlotRenderMethod(MtComponent component, int scope, MtDataCo if (context.ParentContext != null) { - output.AppendLine(CreateLocalVariablesFromContext(context.ParentContext)); + output.AppendLine(CreateLocalVariablesFromContext(context.ParentContext, parentComponent?.Properties.Keys)); variablesInherited.AddRange(context.ParentContext.Keys); } output - .AppendLine(CreateLocalVariablesFromContext(context, variablesInherited)) + .AppendLine(CreateLocalVariablesFromContext(context, parentComponent?.Properties.Keys)) .AppendLine(_maniaTemplateLanguage.FeatureBlockEnd()) .AppendLine(slotContent) .AppendLine(_maniaTemplateLanguage.FeatureBlockStart()) @@ -270,7 +272,7 @@ private static string CreateLocalVariablesFromContext(MtDataContext context, /// Process a ManiaTemplate node. /// private string ProcessNode(XmlNode node, MtComponentMap availableMtComponents, MtDataContext context, - MtComponent? parentComponent = null) + MtComponent? parentComponent = null, bool isRootNode = false) { Snippet snippet = new(); @@ -312,7 +314,8 @@ private string ProcessNode(XmlNode node, MtComponentMap availableMtComponents, M component ), slotContents, - parentComponent + parentComponent, + isRootNode ); subSnippet.AppendLine(_maniaTemplateLanguage.FeatureBlockStart()) @@ -345,7 +348,8 @@ private string ProcessNode(XmlNode node, MtComponentMap availableMtComponents, M if (hasChildren) { subSnippet.AppendLine(1, - ProcessNode(childNode, availableMtComponents, currentContext)); + ProcessNode(childNode, availableMtComponents, currentContext, + parentComponent: parentComponent)); subSnippet.AppendLine(CreateXmlClosingTag(tag)); } @@ -420,7 +424,8 @@ private string ProcessComponentNode( MtComponentAttributes attributeList, string componentBody, IReadOnlyDictionary slotContents, - MtComponent? parentComponent = null + MtComponent? parentComponent = null, + bool isRootNode = false ) { foreach (var slotName in component.Slots) @@ -527,7 +532,7 @@ private string ProcessComponentNode( renderComponentCall.Append( $", __slotRenderer_{parentSlotName}: __slotRenderer_{parentSlotName}"); } - + foreach (var propertyName in parentComponent.Properties.Keys) { renderComponentCall.Append($",{propertyName}: {propertyName}"); @@ -537,11 +542,10 @@ private string ProcessComponentNode( { foreach (var parentSlotName in component.Slots) { - renderComponentCall.Append( - $", __slotRenderer_{parentSlotName}: () => DoNothing()"); + renderComponentCall.Append($", __slotRenderer_{parentSlotName}: () => DoNothing()"); } } - + renderComponentCall.Append(')'); i++; @@ -554,7 +558,7 @@ private string ProcessComponentNode( return renderComponentCall.ToString(); } - + /// /// Creates the method which renders the contents of a component. /// diff --git a/tests/ManiaTemplates.Tests/IntegrationTests/ManialinkEngineTest.cs b/tests/ManiaTemplates.Tests/IntegrationTests/ManialinkEngineTest.cs index 6ffa376..486156a 100644 --- a/tests/ManiaTemplates.Tests/IntegrationTests/ManialinkEngineTest.cs +++ b/tests/ManiaTemplates.Tests/IntegrationTests/ManialinkEngineTest.cs @@ -88,17 +88,20 @@ await Assert.ThrowsAsync(() => [Fact] public async void Should_Render_Component_Without_Content_For_Slot() { - var slotRecursionOuterTwoTemplate = await File.ReadAllTextAsync("IntegrationTests/templates/slot-recursion-outer-two.mt"); - var slotRecursionOuterTemplate = await File.ReadAllTextAsync("IntegrationTests/templates/slot-recursion-outer.mt"); - var slotRecursionInnerTemplate = await File.ReadAllTextAsync("IntegrationTests/templates/slot-recursion-inner.mt"); + var slotRecursionOuterTwoTemplate = + await File.ReadAllTextAsync("IntegrationTests/templates/slot-recursion-outer-two.mt"); + var slotRecursionOuterTemplate = + await File.ReadAllTextAsync("IntegrationTests/templates/slot-recursion-outer.mt"); + var slotRecursionInnerTemplate = + await File.ReadAllTextAsync("IntegrationTests/templates/slot-recursion-inner.mt"); var expected = await File.ReadAllTextAsync("IntegrationTests/expected/single-slot-unfilled.xml"); var assemblies = new[] { typeof(ManiaTemplateEngine).Assembly, typeof(ComplexDataType).Assembly }; - + _maniaTemplateEngine.AddTemplateFromString("SlotRecursionOuterTwo", slotRecursionOuterTwoTemplate); _maniaTemplateEngine.AddTemplateFromString("SlotRecursionOuter", slotRecursionOuterTemplate); _maniaTemplateEngine.AddTemplateFromString("SlotRecursionInner", slotRecursionInnerTemplate); - - var template = _maniaTemplateEngine.RenderAsync("SlotRecursionInner", new{}, assemblies).Result; + + var template = _maniaTemplateEngine.RenderAsync("SlotRecursionInner", new { }, assemblies).Result; Assert.Equal(expected, template, ignoreLineEndingDifferences: true); } @@ -110,15 +113,32 @@ public async void Should_Pass_Properties_To_Components_And_Slots() var testComponentTemplate = await File.ReadAllTextAsync("IntegrationTests/templates/component.mt"); var expected = await File.ReadAllTextAsync("IntegrationTests/expected/property-test.xml"); var assemblies = new[] { typeof(ManiaTemplateEngine).Assembly, typeof(ComplexDataType).Assembly }; - + _maniaTemplateEngine.AddTemplateFromString("PropertyTest", propertyTestTemplate); _maniaTemplateEngine.AddTemplateFromString("Wrapper", testWrapperTemplate); _maniaTemplateEngine.AddTemplateFromString("TestComponent", testComponentTemplate); - + var template = _maniaTemplateEngine.RenderAsync("PropertyTest", new { testVariable = "integration" }, assemblies).Result; Assert.Equal(expected, template, ignoreLineEndingDifferences: true); } + + [Fact] + public async void Should_Fill_All_Slots() + { + var baseTemplate = await File.ReadAllTextAsync("IntegrationTests/templates/slots/base.mt"); + var containerTemplate = await File.ReadAllTextAsync("IntegrationTests/templates/slots/container.mt"); + var windowTemplate = await File.ReadAllTextAsync("IntegrationTests/templates/slots/window.mt"); + var expected = await File.ReadAllTextAsync("IntegrationTests/expected/slots/manialink.xml"); + var assemblies = new[] { typeof(ManiaTemplateEngine).Assembly, typeof(ComplexDataType).Assembly }; + + _maniaTemplateEngine.AddTemplateFromString("Base", baseTemplate); + _maniaTemplateEngine.AddTemplateFromString("Container", containerTemplate); + _maniaTemplateEngine.AddTemplateFromString("Window", windowTemplate); + + var template = _maniaTemplateEngine.RenderAsync("Base", new { }, assemblies).Result; + Assert.Equal(expected, template, ignoreLineEndingDifferences: true); + } } \ No newline at end of file diff --git a/tests/ManiaTemplates.Tests/IntegrationTests/expected/slots/manialink.xml b/tests/ManiaTemplates.Tests/IntegrationTests/expected/slots/manialink.xml new file mode 100644 index 0000000..9bb5746 --- /dev/null +++ b/tests/ManiaTemplates.Tests/IntegrationTests/expected/slots/manialink.xml @@ -0,0 +1,10 @@ + + + + diff --git a/tests/ManiaTemplates.Tests/IntegrationTests/templates/slots/base.mt b/tests/ManiaTemplates.Tests/IntegrationTests/templates/slots/base.mt new file mode 100644 index 0000000..f190a0b --- /dev/null +++ b/tests/ManiaTemplates.Tests/IntegrationTests/templates/slots/base.mt @@ -0,0 +1,11 @@ + + + + + \ No newline at end of file diff --git a/tests/ManiaTemplates.Tests/IntegrationTests/templates/slots/container.mt b/tests/ManiaTemplates.Tests/IntegrationTests/templates/slots/container.mt new file mode 100644 index 0000000..005b411 --- /dev/null +++ b/tests/ManiaTemplates.Tests/IntegrationTests/templates/slots/container.mt @@ -0,0 +1,29 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/tests/ManiaTemplates.Tests/IntegrationTests/templates/slots/window.mt b/tests/ManiaTemplates.Tests/IntegrationTests/templates/slots/window.mt new file mode 100644 index 0000000..5a9a4c6 --- /dev/null +++ b/tests/ManiaTemplates.Tests/IntegrationTests/templates/slots/window.mt @@ -0,0 +1,25 @@ + + + + + + + + + \ No newline at end of file diff --git a/tests/ManiaTemplates.Tests/Lib/expected.tt b/tests/ManiaTemplates.Tests/Lib/expected.tt index f49dc93..a3a2dda 100644 --- a/tests/ManiaTemplates.Tests/Lib/expected.tt +++ b/tests/ManiaTemplates.Tests/Lib/expected.tt @@ -39,11 +39,11 @@ var __outerIndex1 = 0; foreach (int i in numbers) { var __index = __outerIndex1; if (enabled) { -Render_Component_MtContext2(__data: __data, x: (20 * __index), __slotRenderer_default: () => Render_Slot_3_default(__data: new CRoot_ForEachLoop1(__data){__index = __index, i = i}, __slotRenderer_default: () => DoNothing())); +Render_Component_MtContext2(__data: __data, x: (20 * __index), __slotRenderer_default: () => Render_Slot_3_default(__data: new CRoot_ForEachLoop1(__data){__index = __index, i = i},numbers: numbers,enabled: enabled)); } __outerIndex1++; } -Render_Component_MtContext2(__data: __data, __slotRenderer_default: () => Render_Slot_4_default(__data: __data, __slotRenderer_default: () => DoNothing())); +Render_Component_MtContext2(__data: __data, __slotRenderer_default: () => Render_Slot_4_default(__data: __data,numbers: numbers,enabled: enabled)); foreach(var maniaScriptRenderMethod in __maniaScriptRenderMethods){ maniaScriptRenderMethod(); } #> <#+ } -void Render_Slot_3_default(CRoot_ForEachLoop1 __data,Action __slotRenderer_default) { -var numbers = __data.numbers; -var enabled = __data.enabled; +void Render_Slot_3_default(CRoot_ForEachLoop1 __data,List numbers,boolean enabled = true) { var __index = __data.__index; var i = __data.i; var __outerIndex7 = 0; @@ -115,9 +113,7 @@ Render_Component_MtContext6(__data: __data, arg3: (new test())); <#+ } -void Render_Slot_4_default(CRoot __data,Action __slotRenderer_default) { -var numbers = __data.numbers; -var enabled = __data.enabled; +void Render_Slot_4_default(CRoot __data,List numbers,boolean enabled = true) { Render_Component_MtContext2(__data: __data, __slotRenderer_default: () => Render_Slot_8_default(__data: __data, __slotRenderer_default: () => DoNothing())); } #> \ No newline at end of file