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

Support for one liner conditional statement using ternary operator. #632

Open
sanketdhoble opened this issue Jul 13, 2023 · 2 comments
Open

Comments

@sanketdhoble
Copy link

Does liquid js support one-liner conditional msg using ternary operator?
e.g: I want to do something like this but it doesn't work.

{{ data.age == 18 ? 'You are under 18' : 'You are over 18' }}
OR
{{ data.age == 18 ? data.newVar1 : data.newVar2 }}
@harttle
Copy link
Owner

harttle commented Jul 16, 2023

Unfortunately no. But I agree this would be a good feature, given all the discussions I see here: Shopify/liquid#236

While the implementation would be changing the fundamental syntax, expect this feature won't come anytime soon. I'll leave this issue open for people to track.

@pdehaan
Copy link
Contributor

pdehaan commented Jul 16, 2023

@sanketdhoble Not sure if it helps, but I've generally used a custom filter similar to this in the past:

import { Liquid } from "liquidjs";

const engine = new Liquid();

engine.registerFilter("ternary", (value, truthyValue, falsyValue) =>
  !!value ? truthyValue : falsyValue
);
engine
  .parseAndRender('{{ data.age < 18 | ternary: "You are under 18", "You are over 18" }}', { data: { age: 8 } })
  .then(console.log); // outputs 'You are under 18'

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

No branches or pull requests

3 participants