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

Should Fluid allow for the parsing of a set of non-tag statements? #732

Open
deanebarker opened this issue Dec 12, 2024 · 6 comments
Open

Comments

@deanebarker
Copy link
Contributor

deanebarker commented Dec 12, 2024

I'm doing some work where Fluid statements are written without tags, and then injected into a {% liquid tag prior to being parsed:

var statements = @"
  assign name = 'Deane'
  echo name
";

var source = @$"
{% liquid
{statements}
%}}";

_parser.TryParse(source, out var template, out var error);

I'm wondering if there should be a ParseStatements method on FluidParser that does this internally rather than depend on the string manipulation?

@sebastienros
Copy link
Owner

The fact that these are in a liquid tag mutates the parser in a custom state. So it's not as easy as ParseStatements. Instead this should be doable by setting the flag manually in the FluidParseContext, and them parse the inner script as if it was a standard script.

@deanebarker
Copy link
Contributor Author

This didn't work, but how close did I get?

public static class FluidExtensions
{
  public static IFluidTemplate ParseStatements(this FluidParser parser, string statementString)
  {
    var context = new FluidParseContext(statementString)
    {
      InsideLiquidTag = true
    };
    parser.Grammar.TryParse(context, out var statements, out var parlotError);
    return new FluidTemplate(statements);
  }
}

Parse fails on 1,1, with:

An identifier was expected after '{%'

@sebastienros
Copy link
Owner

This is what I would have written. Try to add a blank line before the statements. Or even just a space (it wants to read one after the liquid tag).

If it doesn't work I will have to look into it.

@deanebarker
Copy link
Contributor Author

I actually did have whitespace around it, but that's not the problem, it seems.

var fluid = parser.ParseStatements(@"

assign name = 'Deane'
echo name

");

Just for giggles, I trimmed it prior to parsing. Now I stopped getting the error (so the whitespace was the problem?), but I didn't get any output.

When I dug into the resulting statements, I found there was only one: it had only parsed the AssignStatement. There was no trace of the echo statement (hence, no output).

image

@sebastienros
Copy link
Owner

Could be an unrelated issue then, try with other statements to see if they are parsed. (there is another issue that mentions similar things)

@deanebarker
Copy link
Contributor Author

It doesn't parse the last line, it seems. For example, if you end with an if statement (so the last line would be the endif) then...

{% endif %}' was expected

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