Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question: Introspection of parsed template #578

Open
knightmeister opened this issue Jun 12, 2023 · 6 comments
Open

Question: Introspection of parsed template #578

knightmeister opened this issue Jun 12, 2023 · 6 comments

Comments

@knightmeister
Copy link

Is it possible to introspect tags within a parsed template?

I know that FluidTemplate contains a list of Statements but I don't see a way of viewing the tag name and arguments.

What I'm trying to do is look at the layout being used when rendering to do some things before giving it to Fluid to render.

i.e.

{% layout "mylayout.liquid" %}

I would like to be able to find the tag that is specific for layout and then get the value mylayout.liquid from that tag.

Thanks :)

@knightmeister knightmeister changed the title Introspection of parsed template Question: Introspection of parsed template Jun 12, 2023
@sebastienros
Copy link
Owner

Exactly what I am doing in a different branch, but tied to a specific feature so won't be merged soon.

When you have the list of Statements, try to find the implementation :

This tag is coming from here

I believe you are now familiar of what this code does. The issue that I am seeing is that it ultimately calls into

public void RegisterParserTag<T>(string tagName, Parser<T> parser, Func<T, TextWriter, TextEncoder, TemplateContext, ValueTask<Completion>> render)
and I see that the tag name is not kept in the statement node, so you would not be able to find "layout" among all the statements of type ParserTagStatement. I could suggest that you customize the FluidViewParser instance by calling RegisterExpressionTag("layout", ... again but returning a Statement type that would have the name of the tag in it.

Or, you could submit a PR that add the tag information to the current class (maybe event Statement) and you wouldn't have to do something custom in your own code. I think this would be a good addition.

@knightmeister
Copy link
Author

Appreciate the reply. I'm happy to have a crack at doing a PR for Statement.cs to add the name. Will let you know how I go.

@knightmeister
Copy link
Author

PR submitted for review

knightmeister added a commit to knightmeister/fluid that referenced this issue Jun 14, 2023
@MithrilMan
Copy link

Exactly what I am doing in a different branch, but tied to a specific feature so won't be merged soon.

@sebastienros
Is it maybe something related to the issue I opened here? #580

I was trying to understand the best approach to solve my problem and I thought that maybe I should use a custom parser tag but if you are working on a similar feature or something that could help solve my problem I could wait or fork your branch. But I can't find an updated branch so maybe it's private?

@knightmeister
Copy link
Author

@MithrilMan I have a PR open for this here #579
@sebastienros have you had a chance to look at my approach, if so any feedback is welcome.

@sebastienros
Copy link
Owner

I have finally added visitors, here is the PR. Please check the README for usage examples and let me know if that will work for you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants