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

Getting specified tags #483

Closed
yilmazbuhar opened this issue Mar 5, 2022 · 5 comments
Closed

Getting specified tags #483

yilmazbuhar opened this issue Mar 5, 2022 · 5 comments

Comments

@yilmazbuhar
Copy link

Is this posible to get all tag from specified template?

for example, i want to get all position tags and they values

<html>
	<body>
                {% partial '_Partial' %}
		<pre>
		
		</pre>
		<pre>
			{% position position1 %}
		</pre>
		<pre>
			{% position position2 %}
		</pre>
                {% rendersection footer %}
	</body>
</html>
@hishamco
Copy link
Collaborator

hishamco commented Mar 5, 2022

This is not HTML Parser, could you please tell what's the use case?

@yilmazbuhar
Copy link
Author

yilmazbuhar commented Mar 6, 2022

In the liquid template above, I want to use backend code that returns different data for each request path. This will be a structure like multiple render_body, rather than using a single render_body.
In order to be able to determine which position tag will run which backend code, I need to get the list of position tags in liquid. As you said, I can get these values by parsing, but if it's already stored somewhere, I wanted to get it from there.

@sebastienros
Copy link
Owner

You can cast the IFluidTemplate that is returned to a FluidTemplate instance, and have access to all statements. From there you should be able to filter the tags, check their name, and get the positions.
c.f. #482 and maybe @thomasrosdahl could help if you are stuck.

@thomasrosdahl
Copy link

Here's an example on how to recursively traverse the AST and handle specific statements.

`

    private void HandleStatements(IEnumerable<Statement> statements)
    {
        foreach (var statement in statements)
        {
            if (statement is TagStatement tagStatement)
            {
               ....Handle TagStatement
            }
            else if (statement is IfStatement ifStatement)
            {
                HandleStatements(ifStatement.ElseIfs?.SelectMany(e => e.Statements);
                HandleStatements(ifStatement.Else?.Statements);
                HandleStatements(ifStatement.Statements);
            }
            else if (statement is CaseStatement caseStatement)
            {
                HandleStatements(caseStatement.Whens.SelectMany(w => w.Statements));
                HandleStatements(caseStatement.Else?.Statements);
                HandleStatements(caseStatement.Statements);
            }
            else if (statement is UnlessStatement unlessStatement)
            {
                HandleStatements(unlessStatement.Else?.Statements);
                HandleStatements(unlessStatement.Statements);
            }
        }
    }

`

@yilmazbuhar
Copy link
Author

Thanks a lot

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

4 participants