diff --git a/libs/FuManchu/Parser/SyntaxTree/Block.cs b/libs/FuManchu/Parser/SyntaxTree/Block.cs index 06a4232..8237c30 100644 --- a/libs/FuManchu/Parser/SyntaxTree/Block.cs +++ b/libs/FuManchu/Parser/SyntaxTree/Block.cs @@ -23,7 +23,7 @@ public class Block : SyntaxTreeNode public Block(BlockBuilder source) : this( source.Type, source.Name, source.Children, source.Descriptor, - source.IsPartialBlock, source.IsPartialBlockContent) + source.IsPartialBlock, source.IsImplicitPartialBlockContent) { source.Reset(); } diff --git a/libs/FuManchu/Renderer/BlockRenderer.cs b/libs/FuManchu/Renderer/BlockRenderer.cs index d60eed7..84060fe 100644 --- a/libs/FuManchu/Renderer/BlockRenderer.cs +++ b/libs/FuManchu/Renderer/BlockRenderer.cs @@ -21,7 +21,7 @@ public override void Render(Block target, RenderContext context, TextWriter writ { Block parametersBlock = target; - if (target.Type == BlockType.Tag) + if (target.Type == BlockType.Tag || target.Type == BlockType.PartialBlock || target.Type == BlockType.PartialBlockContent) { // The arguments will be provided by the TagElement instance. parametersBlock = (Block)target.Children.First(); diff --git a/libs/FuManchu/Renderer/PartialBlockRenderer.cs b/libs/FuManchu/Renderer/PartialBlockRenderer.cs index 3251e23..042301b 100644 --- a/libs/FuManchu/Renderer/PartialBlockRenderer.cs +++ b/libs/FuManchu/Renderer/PartialBlockRenderer.cs @@ -70,19 +70,35 @@ protected override void Render(Block block, Arguments? arguments, Map? maps, Ren { using (var scope = context.BeginScope(model)) { + if (maps is { Count: >0 }) + { + scope.ScopeContext.SetParameters(maps); + } + context.Service.RunPartial(name, scope.ScopeContext, writer); } } - else if (maps is { Count: > 0 }) + //else if (maps is { Count: > 0 }) + //{ + // using (var scope = context.BeginScope(maps)) + // { + // context.Service.RunPartial(name, scope.ScopeContext, writer); + // } + //} + else { - using (var scope = context.BeginScope(maps)) + if (maps is { Count: > 0 }) { - context.Service.RunPartial(name, scope.ScopeContext, writer); + using (var scope = context.BeginScope(context.TemplateData.Model)) + { + scope.ScopeContext.SetParameters(maps); + context.Service.RunPartial(name, scope.ScopeContext, writer); + } + } + else + { + context.Service.RunPartial(name, context, writer); } - } - else - { - context.Service.RunPartial(name, context, writer); } } diff --git a/libs/FuManchu/Renderer/RenderContext.cs b/libs/FuManchu/Renderer/RenderContext.cs index 14378d4..e9629fc 100644 --- a/libs/FuManchu/Renderer/RenderContext.cs +++ b/libs/FuManchu/Renderer/RenderContext.cs @@ -1,4 +1,5 @@ -// This work is licensed under the terms of the MIT license. + +// This work is licensed under the terms of the MIT license. // For a copy, see . namespace FuManchu.Renderer; @@ -16,7 +17,8 @@ namespace FuManchu.Renderer; /// public class RenderContext { - private readonly Map _variables = new Map(); + readonly Map _variables = new Map(); + Map _parameters = new Map(); /// /// Initializes a new instance of the class. @@ -103,6 +105,23 @@ public RenderContextScope BeginScope(object? model) return @default; } + /// + /// Gets the parameter with the given name. + /// + /// The parameter name. + /// [Optional] The default value for the parameter. + /// The parameter value. + public object? GetParameter(string name, object? @default) + { + object? value; + if (_variables.TryGetValue(name, out value)) + { + return value; + } + + return @default; + } + /// /// Resolves the value represented by the given span. /// @@ -274,6 +293,11 @@ public RenderContextScope BeginScope(object? model) expression = expression.Substring(5); } + if (_parameters.TryGetValue(expression, out var parameterValue)) + { + return parameterValue; + } + var modelMetadata = ExpressionMetadataProvider.FromStringExpression(expression, templateData, context.ModelMetadataProvider); if (modelMetadata == null || !modelMetadata.Valid) { @@ -295,4 +319,23 @@ public void SetVariable(string name, object value) { _variables[name] = value; } + + /// + /// Sets the parameter with the given name. + /// + /// The name of the variable. + /// The variable value. + public void SetParameter(string name, object value) + { + _parameters[name] = value; + } + + /// + /// Sets all parameters for the current context. + /// + /// The parameter map. + public void SetParameters(Map parameters) + { + _parameters = parameters ?? new(); + } } diff --git a/libs/FuManchu/Renderer/ZoneBlockRenderer.cs b/libs/FuManchu/Renderer/ZoneBlockRenderer.cs index 4ee0e45..a932716 100644 --- a/libs/FuManchu/Renderer/ZoneBlockRenderer.cs +++ b/libs/FuManchu/Renderer/ZoneBlockRenderer.cs @@ -44,7 +44,7 @@ protected override void Render(Block block, Arguments? arguments, Map? maps, Ren if (context.ParentRenderContext is not null) { - return TryGetZone(name, context); + return TryGetZone(name, context.ParentRenderContext); } return (null, null); diff --git a/tests/FuManchu.Tests/Content/Addresses.partial.hbs b/tests/FuManchu.Tests/Content/Addresses.partial.hbs index e736fe7..655dee7 100644 --- a/tests/FuManchu.Tests/Content/Addresses.partial.hbs +++ b/tests/FuManchu.Tests/Content/Addresses.partial.hbs @@ -2,7 +2,7 @@ {{>content}} {{#if billing}} - {{#>Column}} + {{#>Column width="25%"}} {{>content}} {{>Address title="Billing Address" address=billing}} {{/content}} @@ -10,7 +10,7 @@ {{/if}} {{#if shipping}} - {{#>Column}} + {{#>Column width="75%"}} {{>content}} {{>Address title="Shipping Address" address=shipping}} {{/content}} @@ -18,4 +18,4 @@ {{/if}} {{/content}} -{{/Row}} \ No newline at end of file +{{/Row}} diff --git a/tests/FuManchu.Tests/Content/Column.partial.hbs b/tests/FuManchu.Tests/Content/Column.partial.hbs index d199e6b..6226236 100644 --- a/tests/FuManchu.Tests/Content/Column.partial.hbs +++ b/tests/FuManchu.Tests/Content/Column.partial.hbs @@ -1,5 +1,5 @@ - +
{{ - \ No newline at end of file + diff --git a/tests/FuManchu.Tests/Verification/ComplexDocumentFacts.CanVerifyComplexDocument.verified.txt b/tests/FuManchu.Tests/Verification/ComplexDocumentFacts.CanVerifyComplexDocument.verified.txt index e1893f3..3e6acda 100644 --- a/tests/FuManchu.Tests/Verification/ComplexDocumentFacts.CanVerifyComplexDocument.verified.txt +++ b/tests/FuManchu.Tests/Verification/ComplexDocumentFacts.CanVerifyComplexDocument.verified.txt @@ -262,25 +262,19 @@ - +
Billing Address
- Matthew Abbott,
- 1 Somestreet Lane,
- - Somewhere,
- Co. Somewhere,
- AA11 1AA,
United Kingdom -
+ @@ -290,6 +284,7 @@ +

Booked with: Jo