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

Ternary statement in partial evaluates as true for null values #10589

Closed
o1y opened this issue Aug 7, 2024 · 3 comments · Fixed by #10595
Closed

Ternary statement in partial evaluates as true for null values #10589

o1y opened this issue Aug 7, 2024 · 3 comments · Fixed by #10595
Labels

Comments

@o1y
Copy link
Contributor

o1y commented Aug 7, 2024

Bug description

When using a ternary statement within a partial to check if a value is set, the ternary expression is evaluated as true even if the value is null.

How to reproduce

  1. Add a select field select_field with some random options.
  2. Create an entry, but don't select a value from select_field
  3. Create a partial container.antlers.html with the following content
{{ select_field ? 'true' : 'false' }}
  1. In your template add the partial like so:
{{ partial:container :select_field="select_field" }}

This returns 'true' 🤯

When dumping select_field in the partial you'll see that this seems not correct:

{{ select_field | dump }}
Statamic\Fields\LabeledValue {#3116 ▼ // vendor/statamic/cms/src/Modifiers/CoreModifiers.php:613
  #value: null
  #extra: array:1 [▶]
}

Logs

No response

Environment

Environment
Application Name: Statamic
Laravel Version: 11.20.0
PHP Version: 8.3.9
Composer Version: 2.7.7
Environment: local
Debug Mode: ENABLED
URL: ternary.test
Maintenance Mode: OFF
Timezone: UTC
Locale: en

Cache
Config: NOT CACHED
Events: NOT CACHED
Routes: NOT CACHED
Views: CACHED

Drivers
Broadcasting: log
Cache: file
Database: sqlite
Logs: stack / single
Mail: log
Queue: sync
Session: file

Statamic
Addons: 0
Sites: 1
Stache Watcher: Enabled (auto)
Static Caching: Disabled
Version: 5.19.0 Solo

Installation

Fresh statamic/statamic site via CLI

Additional details

No response

@jasonvarga
Copy link
Member

I wish there was a magic __toBool() method like __toString().

@jasonvarga
Copy link
Member

We can probably make this work in Antlers though. 👌

@o1y
Copy link
Contributor Author

o1y commented Aug 7, 2024

We can probably make this work in Antlers though. 👌

This is already magically working with null values, but not when the ternary statement is used in a partial for some reason 🤷‍♂️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment