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

Check if page.data.x is defined #382

Closed
gibfahn opened this issue Feb 22, 2018 · 6 comments
Closed

Check if page.data.x is defined #382

gibfahn opened this issue Feb 22, 2018 · 6 comments

Comments

@gibfahn
Copy link

gibfahn commented Feb 22, 2018

Is there a way to check whether a variable is defined?

I've tried some suggestions from Shopify/liquid#89 and Shopify/liquid#223, and haven't found something that works.

Trying something like:

{% if page.data.nav  %}
## Foo
{% endif %}

results in

Error: Failed to render content for "index.html"
Caused by: Rendering error: Invalid index `nav` for value 
`Object({"heading": Str("Gibson Fahnestock")})`
@epage
Copy link
Member

epage commented Feb 22, 2018

Check my post on #363

@epage epage closed this as completed Feb 22, 2018
@epage
Copy link
Member

epage commented Feb 22, 2018

Quick summary:

{% if page contains "title" %}
{% endif%}

and I plan to add back in {% if page.data.nav %} support in the future.

@gibfahn
Copy link
Author

gibfahn commented Feb 23, 2018

I tried that but couldn't get it to work:

{% if page.data contains nav  %}
## Foo
{% endif %}
Error: Failed to render content for "index.html"
Caused by: Rendering error: Object key not found: "nav"

EDIT: I didn't read what you wrote properly, sorry! If you quote the nav then it works.

@epage
Copy link
Member

epage commented Feb 23, 2018

So you've got it working now?

@gibfahn
Copy link
Author

gibfahn commented Feb 23, 2018

So you've got it working now?

Yes, got it working.

Not sure how you do it for a top-level variable though (equivalent of {% if nav %}), is that currently possible?

@epage
Copy link
Member

epage commented Feb 23, 2018

Not sure how you do it for a top-level variable though (equivalent of {% if nav %}), is that currently possible?

At the moment, you can't. I plan to add back in support for this.

The problem is previously, it treated any non-existent variable name as if it was nil. When migrating across a major breaking change in cobalt, I wanted people people to know when they were accessing the old name of a variable, so I made them all errors.

As time passes and I'm more confident people have migrated, I'll be adding a special case to the if tag so that if its just a variable name, it will return false if it does't exist.

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

2 participants