Add TemplateParsed callback for AST visitor integration in ViewEngine - #869
Conversation
Co-authored-by: sebastienros <1165805+sebastienros@users.noreply.github.com>
Co-authored-by: sebastienros <1165805+sebastienros@users.noreply.github.com>
|
@copilot To simplify the samples that are checking for |
…ify visitor usage Co-authored-by: sebastienros <1165805+sebastienros@users.noreply.github.com>
Done in commit 251952e. The tests and documentation have been updated to use the simplified pattern: options.TemplateParsed = (path, template) =>
{
var visitor = new MyCustomVisitor();
return visitor.VisitTemplate(template);
}; |
|
@copilot If a statement is altered by an AstRewriter then a CompositeFluidTemplate will fail because it's still renders the original templates and not the altered statements. Create a test to verify this assumption and then fix the implementation if necessary. Maybe use the same code as |
…luidTemplate Co-authored-by: sebastienros <1165805+sebastienros@users.noreply.github.com>
…#869) * Initial plan * Add TemplateParsed callback to FluidViewEngineOptions with tests Co-authored-by: sebastienros <1165805+sebastienros@users.noreply.github.com> * Add documentation for TemplateParsed callback in README Co-authored-by: sebastienros <1165805+sebastienros@users.noreply.github.com> * Refactor CompositeFluidTemplate to implement IStatementList and simplify visitor usage Co-authored-by: sebastienros <1165805+sebastienros@users.noreply.github.com> * Fix CompositeFluidTemplate to render using Statements property like FluidTemplate Co-authored-by: sebastienros <1165805+sebastienros@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: sebastienros <1165805+sebastienros@users.noreply.github.com> Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: ee9c8775-0354-41dd-8226-89a69c60241c
Implementation Complete: Support for AST Visitors/Rewriters in ViewEngine
Successfully added a callback property in ViewEngine options that allows users to update a template once it is parsed, before it is cached.
Plan Checklist:
TemplateParsedcallback toFluidViewEngineOptionsFluidViewRenderer.GetFluidTemplateAsync()to invoke the callback after parsingFluidMvcViewOptions(inherits automatically, no changes needed)Implementation Summary:
Added a
TemplateParsedcallback delegate toFluidViewEngineOptionsthat gets invoked after a template is parsed but before it's cached. This allows users to apply visitors/rewriters to modify templates as needed.Refactored
CompositeFluidTemplateto implementIStatementListby collecting all statements from inner templates. This simplifies the API - users no longer need special handling for CompositeFluidTemplate when applying visitors.Fixed
CompositeFluidTemplate.RenderAsync()to render using theStatementsproperty instead of iterating throughTemplates. This ensures consistency withFluidTemplateand properly handles altered statements. The implementation now uses the same optimized pattern asFluidTemplatewith synchronous fast path for better performance.The callback is invoked in
FluidViewRenderer.GetFluidTemplateAsync()after parsing but before caching, ensuring that:Changes Made:
TemplateParsedDelegateandTemplateParsedpropertyGetFluidTemplateAsync()to invoke the callback after parsing, before cachingIStatementListby collecting statements from inner templatesRenderAsync()to useStatementsproperty instead ofTemplatesFluidTemplatefor better performanceSimplified Usage Example:
The callback now works seamlessly with all template types, and CompositeFluidTemplate correctly renders altered statements.
Security Summary:
Original prompt
💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.