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

Introspection of parsed template using visitor #705

Open
knightmeister opened this issue Oct 20, 2024 · 2 comments
Open

Introspection of parsed template using visitor #705

knightmeister opened this issue Oct 20, 2024 · 2 comments

Comments

@knightmeister
Copy link

Hi,

I raised #578 and submitted a PR that received no comment and it looks like the advice now is to use the Visitor supports that's been recently added.

There is an example in the readme but it doesn't demonstrate what I need to do.

I need to walk the template and find instances of a given tag (in my case, {% layout '[id]' %}).

I want to be able to extract both the tag name (layout) and the input ID.

How do I do that using a Visitor?

Starting with a contrived example:

static void Main(string[] args)
{
    var template = new MyParser().Parse("{% layout 'layoutid' %}");
    var v = new Visitor();
    v.VisitTemplate(template);
}

public class Visitor : AstVisitor
{
    public override Statement Visit(Statement statement)
    {
        return base.Visit(statement);
    }
}

The Statement method breakpoints but I'm unable to access anything on the statement that is passed in. The only method is WriteToAsync and there are no properties.

How do I extract the statement name and passed parameter?

Cheers

@sebastienros
Copy link
Owner

I will check that. From a brief look at the source the layout tag is of type ParserTagStatement. Maybe filtering statements for this type and then casting would allow you to access the expression it links to. Not sure if this type is public yet.

@knightmeister
Copy link
Author

Thanks
Can't cast to ParserTagStatement<Fluid.Ast.Expression> because the ParserTagStatement is not exposed publicly.

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

2 participants