11ty / eleventy

A simpler site generator. Transforms a directory of templates (of varying types) into HTML.

Home Page:https://www.11ty.dev/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

setLiquidOptions for jsTruthy seems not working

saiballo opened this issue · comments

commented

Operating system

ubuntu 22.04

Eleventy

2.0.0

Describe the bug

this is my .eleventy.js

module.exports = eleventyConfig => {

	eleventyConfig.setLiquidOptions({
		jsTruthy: true
	});

	return {
	  dir: {
		input: "src/view",
		output: "static"
	  }
	}};

I want to change behaviour of js truthy.

But I write this simple condition:

{% assign var = 0 %}

  {% if var == false %}
   condition ok!
  {% endif %}

it never appears!

Reproduction steps

No response

Expected behavior

No response

Reproduction URL

No response

Screenshots

No response

I think you'd need to try making a vanilla LiquidJS test case for this, but… I don't think that's how the jsTruthy option works. https://liquidjs.com/tutorials/options.html#jsTruthy

jsTruthy is used to use standard JavaScript truthiness rather than the Shopify.
it defaults to false. For example, when set to true, a blank string would evaluate to false with jsTruthy. With Shopify’s truthiness, a blank string is true.

So if I recall correctly, it's a flag to make Liquid be more JavaScript like instead of Shopify/Ruby like.
I'm not certain that 0 == false will be true with or without jsTruthy.
More LiquidJS docs at https://liquidjs.com/tutorials/truthy-and-falsy.html and CHANGELOG (added in 9.16.0).

In fact, here's my Eleventy test case (repo: https://github.com/pdehaan/11ty-2858):

<h1>jsTruthy={{ JS_TRUTHY }}</h1>

{%- assign empty = "" %}
{%- assign zero = 0 %}
{%- assign bool = false %}

{%- if empty == false %}
  [EMPTY1] condition ok!
{%- endif %}
{%- if not empty %}
  [EMPTY2] condition ok!
{%- endif %}

{%- if zero == false %}
  [ZERO1] condition ok!
{%- endif %}
{%- if not zero %}
  [ZERO2] condition ok!
{%- endif %}

{%- if bool == false %}
  [BOOL1] condition ok!
{%- endif %}
{%- if not bool %}
  [BOOL2] condition ok!
{%- endif %}

{%- if undef == false %}
  [UNDEF1] condition ok!
{%- endif %}
{%- if not undef %}
  [UNDEF2] condition ok!
{%- endif %}

And my output w/ jsTruthy=true and jsTruthy=false:

<h1>jsTruthy=false</h1>
  [BOOL1] condition ok!
  [BOOL2] condition ok!
  [UNDEF2] condition ok!

<h1>jsTruthy=true</h1>
  [EMPTY2] condition ok!
  [ZERO2] condition ok!
  [BOOL1] condition ok!
  [BOOL2] condition ok!
  [UNDEF2] condition ok!

So based on my quick check, the two differences when jsTruthy=true are:

[EMPTY2] condition ok!
[ZERO2] condition ok!

Where the test cases were:

{%- assign empty = "" %}
{%- assign zero = 0 %}

{%- if not empty %}
  [EMPTY2] condition ok!
{%- endif %}
{%- if not zero %}
  [ZERO2] condition ok!
{%- endif %}

So maybe you want to be checking against {% if not var %} instead of explicitly checking against Boolean false:

{% assign var = 0 %}

{% if not var %}
  condition ok!
{% endif %}
commented

Thank you for your check. In javascript this code:

if (0 == false) {
      console.log("condition ok")
    }

print the console message but not the same code in liquidjs:

{% if 0 == false %}
   condition ok
  {% endif %}

so, as you writed in your review, the only way is:

{% if not var %}
  condition ok!
{% endif %}

This is an automated message to let you know that a helpful response was posted to your issue and for the health of the repository issue tracker the issue will be closed. This is to help alleviate issues hanging open waiting for a response from the original poster.

If the response works to solve your problem—great! But if you’re still having problems, do not let the issue’s closing deter you if you have additional questions! Post another comment and we will reopen the issue. Thanks!