Emit TemplateParsed events for templates loaded by render and include statements - #873
Conversation
…d render statements Co-authored-by: sebastienros <1165805+sebastienros@users.noreply.github.com>
|
@copilot Update FluidViewEngineOptions to use this new TemplateParsed callback instead of the one it has. Update existing usages to use the one in TemplateOptions now. Update the samples and documentation. |
…wEngineOptions Co-authored-by: sebastienros <1165805+sebastienros@users.noreply.github.com>
Done in commit 8e260eb. Changes made:
All 1327 tests pass. |
| services.AddMvc().AddFluid(options => | ||
| { | ||
| options.TemplateParsed = (path, template) => | ||
| options.TemplateOptions.TemplateParsed = (path, template) => |
There was a problem hiding this comment.
@copilot update this section so it's not specific to the view engine.
There was a problem hiding this comment.
Done in commit 480170a. Updated the section to cover all template parsing scenarios, not just ViewEngine. The documentation now shows examples for both ViewEngine usage and direct TemplateOptions usage with include/render statements.
| } | ||
| else if (context.Options.TemplateParsed != null) | ||
| { | ||
| // Template was retrieved from cache, but we still need to apply the callback |
There was a problem hiding this comment.
@copilot Templates retrieved from cache should not emit the vent. Only when they are first parsed.
There was a problem hiding this comment.
Fixed in commit 480170a. Removed the else if block that was invoking the callback for cached templates. Now the callback is only invoked when templates are first parsed (inside the main if block), not when retrieved from cache. Updated tests to verify the callback is called exactly once and the modified template is cached.
…; update docs Co-authored-by: sebastienros <1165805+sebastienros@users.noreply.github.com>
Updated section on using visitors with templates for clarity.
… statements (#873) * Initial plan * Add TemplateParsed callback support to TemplateOptions for include and render statements Co-authored-by: sebastienros <1165805+sebastienros@users.noreply.github.com> * Consolidate TemplateParsed to use TemplateOptions instead of FluidViewEngineOptions Co-authored-by: sebastienros <1165805+sebastienros@users.noreply.github.com> * Fix TemplateParsed to only invoke on first parse, not cache retrieval; update docs Co-authored-by: sebastienros <1165805+sebastienros@users.noreply.github.com> * Revise visitors section in README Updated section on using visitors with templates for clarity. --------- 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: Sébastien Ros <sebastienros@gmail.com> Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: ee9c8775-0354-41dd-8226-89a69c60241c
The
TemplateParsedcallback was only invoked for top-level templates in FluidViewEngine, but not for nested templates loaded via{% render %}and{% include %}statements in the core library. This prevented AST visitors from transforming nested templates.Example:
{# root.liquid #} {{ 1 | plus: 2 }} {% render 'inner' %} {# inner.liquid #} {{ 2 | plus: 2 }}Previously, applying a visitor via
TemplateParsedwould only transformroot.liquid(output:5\n4), notinner.liquid. Now both are transformed (output:5\n8).Changes
TemplateParsedDelegateandTemplateParsedpropertyTemplateParsedcallback when templates are first parsed, before cachingTemplateParsedDelegateandTemplateParsedproperty - now usesTemplateOptions.TemplateParsedinsteadTemplateOptions.TemplateParsedvia the options propertyThe callback is invoked only when templates are first parsed, not when retrieved from cache. The modified template is cached and reused on subsequent renders, improving performance. This consolidation provides a single, unified API for the
TemplateParsedcallback across both the ViewEngine and core library.TemplateParsedevents for templates called byrenderandincludestatements #872Original prompt
TemplateParsedevents for templates called byrenderandincludestatements #872💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.