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

how to set default value for bool variable #1100

Closed
aimuke opened this issue May 10, 2019 · 7 comments
Closed

how to set default value for bool variable #1100

aimuke opened this issue May 10, 2019 · 7 comments

Comments

@aimuke
Copy link

aimuke commented May 10, 2019

{% assign varA = false %}
{{ varA | default: true }}

I want to get the value of varA, but, Liquiddefault filter will set the value only if “the left side is nil, false, or empty”. So I always get ture.
How sould I get the value of varA? thx.

@fw42
Copy link
Contributor

fw42 commented May 10, 2019

{{ varA }}?

@aimuke
Copy link
Author

aimuke commented May 11, 2019

thanks. @fw42 .

The variable varA may be empty which I want to set it to true. If I use {{ varA }}, I cannot get the default value.

@ashmaroli
Copy link
Contributor

Workarounds

  • If you just wanna render the value:
    {%- if varA == false -%}
      false
    {%- else -%}
      {{ varA | default: true }}
    {%- endif -%}
    
  • Or if you're just gonna use it in a conditional:
    {%- if varA -%}
      cookie!
    {%- else -%}
      no cookie!
    {%- endif -%}
    

@aimuke
Copy link
Author

aimuke commented May 12, 2019

thx, @ashmaroli

Yes, I can do it like this. thx.

@aimuke aimuke closed this as completed May 12, 2019
@CrazyOrr
Copy link

CrazyOrr commented Nov 8, 2020

The default filter

Allows you to specify a fallback in case a value doesn’t exist. default will show its value if the left side is nil, false, or empty.

This fallback rule shouldn't apply when a variable has a boolean value of false, which does "exist" and doesn't need to be fallbacked on.

@dylanahsmith
Copy link
Contributor

Looks like we are missing documentation on https://shopify.github.io/liquid/filters/default/ for the allow_false keyword argument to the default filter, although there is documentation for it in the source code

# Use `allow_false` when an input should only be tested against nil or empty and not false.
#
# Example:
# {{ product.title | default: "No Title", allow_false: true }}

@ashmaroli
Copy link
Contributor

@dylanahsmith The commit that introduced allow_false haven't been released yet. So, documenting it on https://shopify.github.io/liquid/filters/default/ is going to create confusion..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants