Should Fluid allow for the parsing of a set of non-tag statements?
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?
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.
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 '{%'
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.
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).
Could be an unrelated issue then, try with other statements to see if they are parsed. (there is another issue that mentions similar things)
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