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

Support single line liquid tags #409

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Fluid.Tests/ParserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -964,5 +964,15 @@ public void ShouldParseLiquidTagWithBlocks()
var rendered = template.Render();
Assert.Contains("WELCOME TO THE LIQUID TAG", rendered);
}

[Fact]
public void ShouldParseLiquidTagInline()
{
var source = @"{% liquid if cool echo 'cool' else echo 'not cool' endif %}";

Assert.True(_parser.TryParse(source, out var template, out var errors), errors);
var rendered = template.Render();
Assert.Contains("not cool", rendered);
}
}
}
15 changes: 6 additions & 9 deletions Fluid/Ast/BinaryExpressions/EndsWithBinaryExpression.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Fluid.Values;
using System.Threading.Tasks;

namespace Fluid.Ast.BinaryExpressions
{
Expand All @@ -9,21 +8,19 @@ public EndsWithBinaryExpression(Expression left, Expression right) : base(left,
{
}

public override async ValueTask<FluidValue> EvaluateAsync(TemplateContext context)
internal override FluidValue Evaluate(FluidValue left, FluidValue right)
{
var leftValue = await Left.EvaluateAsync(context);
var rightValue = await Right.EvaluateAsync(context);

if (leftValue is ArrayValue)
if (left is ArrayValue arrayValue)
{
var first = await leftValue.GetValueAsync("last", context);
return first.Equals(rightValue)
var values = arrayValue.Values;
var last = values.Length > 0 ? values[values.Length - 1] : NilValue.Instance;
return last.Equals(right)
? BooleanValue.True
: BooleanValue.False;
}
else
{
return leftValue.ToStringValue().EndsWith(rightValue.ToStringValue())
return left.ToStringValue().EndsWith(right.ToStringValue())
? BooleanValue.True
: BooleanValue.False;
}
Expand Down
2 changes: 1 addition & 1 deletion Fluid/Ast/MemberExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public override ValueTask<FluidValue> EvaluateAsync(TemplateContext context)

// Search the initial segment in the local scope first

FluidValue value = context.LocalScope.GetValue(initial.Identifier);
var value = context.LocalScope.GetValue(initial.Identifier);

// If it was not successful, try again with a member of the model

Expand Down
12 changes: 2 additions & 10 deletions Fluid/Ast/RangeExpression.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
using Fluid.Values;
using System.Threading.Tasks;

namespace Fluid.Ast
namespace Fluid.Ast
{
public class RangeExpression : Expression
public class RangeExpression
{
public RangeExpression(Expression from, Expression to)
{
Expand All @@ -14,10 +11,5 @@ public RangeExpression(Expression from, Expression to)
public Expression From { get; }

public Expression To { get; }

public override ValueTask<FluidValue> EvaluateAsync(TemplateContext context)
{
throw new System.NotImplementedException();
}
}
}
1 change: 0 additions & 1 deletion Fluid/FluidParserExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using Fluid.Ast;
using Fluid.Parser;
using Parlot.Fluent;
using System;
using System.Collections.Generic;
using System.IO;
Expand Down
1 change: 1 addition & 0 deletions Fluid/Parser/TagParsers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ public override bool Parse(ParseContext context, ref ParseResult<TagResult> resu

while (Character.IsWhiteSpace(cursor.Current))
{
newLineIsPresent = true;
cursor.Advance();
}

Expand Down