Last active
February 11, 2025 21:46
-
-
Save deanebarker/68b61596283dd6ce4126df2639e6efe3 to your computer and use it in GitHub Desktop.
Demostrates using a visitor to modify a Fluid template post-parse
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| void Main() | |
| { | |
| var source = "Hi {{ this_is_the_wrong_identifier }}!"; | |
| var template = new FluidParser().Parse(source); | |
| // This replaces "this_is_the_wrong_identifier" with "name" | |
| var visitor = new FixWrongIdentifier(); | |
| template = visitor.VisitTemplate(template); | |
| var context = new TemplateContext(); | |
| context.SetValue("name", "Deane"); | |
| template.Render(context).Dump(); | |
| } | |
| public class FixWrongIdentifier : AstRewriter | |
| { | |
| protected override Fluid.Ast.Expression VisitMemberExpression(Fluid.Ast.MemberExpression memberExpression) | |
| { | |
| if(memberExpression.Segments.First() is IdentifierSegment idSegment) | |
| { | |
| if(idSegment.Identifier == "this_is_the_wrong_identifier") | |
| { | |
| return new Fluid.Ast.MemberExpression(new IdentifierSegment("name")); | |
| } | |
| } | |
| return memberExpression; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment