fluid icon indicating copy to clipboard operation
fluid copied to clipboard

Added `StringComparer` property to `TemplateContext`

Open gumbarros opened this issue 1 year ago • 14 comments

This PR allows case insensitivity at templates, making more easy to non-programmers to use variables and functions.

gumbarros avatar Jul 12 '24 21:07 gumbarros

@gumbarros check my last commit

hishamco avatar Jul 14 '24 00:07 hishamco

@gumbarros check my last commit

LGTM

gumbarros avatar Jul 14 '24 00:07 gumbarros

@sebastienros anything else to add to this?

hishamco avatar Jul 14 '24 00:07 hishamco

Thanks for a quick fix :)

hishamco avatar Jul 14 '24 00:07 hishamco

Any plans to merge this 👀?

gumbarros avatar Jul 26 '24 16:07 gumbarros

I'm going to be out for 20 days next week and won't be able to make any changes for this PR. I would also like to use this feature this week if possible.

gumbarros avatar Jul 30 '24 16:07 gumbarros

@sebastienros any feedback on this or shall I merge?

hishamco avatar Aug 02 '24 21:08 hishamco

This is too intrusive (in the ctor). Can this StringComparer be yet another property of the options/context classes, be inherited like the other properties?

And the new property should be named ModelNamesComparer? The default value should be Ordinal obviously.

sebastienros avatar Aug 02 '24 21:08 sebastienros

@gumbarros

if you can't do the changes @hishamco or myself should be able to do it.

sebastienros avatar Aug 02 '24 21:08 sebastienros

To be honest it was as it's seen in https://github.com/sebastienros/fluid/pull/681/commits/b99e0f54853fbebf42101758f24388917e2ac32f

If you like how it was I will revert the latest changes considering the name that you suggested

hishamco avatar Aug 02 '24 23:08 hishamco

To be honest it was as it's seen in https://github.com/sebastienros/fluid/commit/b99e0f54853fbebf42101758f24388917e2ac32f

Did I actually ask the opposite, or was it done on your own thoughts?

sebastienros avatar Aug 03 '24 00:08 sebastienros

It added as property at the beginning but I saw the property appears twice in different places that's why I suggested to pass the values through constructor

hishamco avatar Aug 03 '24 00:08 hishamco

In you case that's what you mean, it's pretty common to see the properties duplicated and cloned from TemplateOptions and TemplateContext. But this is done this way for anything that can alter the behavior or the rendering, to have defaults or options to share across template content instances without having to set it each time.

sebastienros avatar Aug 03 '24 00:08 sebastienros

I see if the linked commit is fine for you I will revert the latest changes then merge unless you need to add something else

hishamco avatar Aug 03 '24 00:08 hishamco

Hi guys, any news on merging this 👀?

gumbarros avatar Sep 17 '24 23:09 gumbarros

Updated it. Should be simpler, and it helped me realize that this property can't be changed dynamically as it is used by an inner collection.

In Fluid 3.0 we might want to use the same comparer for both internal properties and model properties. (note for myself)

sebastienros avatar Sep 18 '24 01:09 sebastienros

When will this PR be released? I've been waiting for so long. And I think the GetValueAsync function from FluidValue should be updated in the same way as this case

nguyenngocanhpro avatar Oct 14 '24 07:10 nguyenngocanhpro

@nguyenngocanhpro do you want to submit a PR for GetValueAsync? I can ship it right after that.

sebastienros avatar Oct 14 '24 20:10 sebastienros

With this PR (tested using v2.24.0), should we still be able to use case-sensitive variables if the TemplateOptions ModelNamesComparer is set to StringComparer.Ordinal?

I was under the impression that Shopify Liquid still supports this behavior (example):

 {% assign case = "lower" %} 
 {% assign CASE = "upper" %} 
 case = {{ case }} <!-- expect lower -->
 CASE = {{ CASE }} <!-- expect upper -->

Here's a unit test:

[Fact]
public void ShouldUseTemplateOptionsStringComparerWithCaseSensitive()
{
   var options = new TemplateOptions { ModelNamesComparer = StringComparer.Ordinal };
   var context = new TemplateContext(options);
   context.SetValue("case", "lower");
   context.SetValue("CASE", "upper");
   context.SetValue("Case", "mixed");

   Assert.Equal("lowerupper", context.GetValue("case").ToStringValue() + context.GetValue("CASE").ToStringValue());
}

Results:

Actual: "mixedmixed"

nairdo avatar May 19 '25 18:05 nairdo

The link you provided is from liquidjs, not shopify, here is the result from Shopify (https://shopify.dev/docs/api/liquid)

image

sebastienros avatar May 19 '25 19:05 sebastienros

@sebastienros Right, sorry about using liquidjs, I just mainly wanted to confirm that Fluid should still allow case sensitivity. I'll open this as a new issue.

nairdo avatar May 19 '25 20:05 nairdo

I just wanted to be sure it was also the behavior in shopify. I also create issues on liquidjs because it doesn't follow the standard so I wanted to point it out.

sebastienros avatar May 19 '25 22:05 sebastienros

Here is the PR fixing the bug: https://github.com/sebastienros/fluid/pull/802

sebastienros avatar May 19 '25 22:05 sebastienros