sebastienros/fluid

Element.first seems to be not working anymore

Closed this issue · 6 comments

Hi, I am using Fluid for writing Lucene queries for OrchardCore.
My problem is the following:
image
Prior to OrchardCore 1.5 which probably also brough a Fluid update to OC element.first was able to check whether a parameter is a string or an array as if it was a string .first evaluated to false.
This function seems to be not working anymore and I couldn't find a proper reason why checking fluid.

When giving a simple string as a parameter param.first returned false but now it errors out.

@sebastienros you might know something about this.

I found a solution.
Use element[0] instead of element.first

Seems that I was wrong and element[0] isn't working aswell.

first works correctly for strings and arrays. It seems the way to distinguish both is to use [0] as strings should return nothing. I will check this and fix it if it's wrong in Fluid.

Here is how it behaves in Shopify (the official source of truth)

image

Note that null is not a thing in Liquid. But by chance null being undefined, if will behave as you would expect by checking if they are both undefined. What you want is blank which will be true if it's empty ("") or undefined. So id != '' and id != null should be id != blank

@sebastienros Thanks for clarifying! For some reason in OC Fluid var[0] also brings back the first char of the string.
So what was var.first to determine whether you have an array or string is now something like this:

{% if var[0].size == 1 %}
Its a string
{%else%}
Its an array

So what I am saying is var[0] was evaluated to true when var was a string but I have experienced different behaviors using Liquid in Orchard and using it in an online compiler

Not sure it's clear, but I fixed it 1 hour ago. The fix is available on the MyGet feed if you want to try it.

{% if var[0] == blank %}
It's a string
{%else%}
It's an array