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

strict_variables throws exception on checking if variable exists #1034

Open
ptoews opened this Issue Sep 28, 2018 · 2 comments

Comments

Projects
None yet
2 participants
@ptoews

ptoews commented Sep 28, 2018

I'm using a jekyll layout where I want to include content of pages, depending on whether it exists.
Apparently, it is supposed to be done like described in #89
In my case it would look as follows:

{% assign title = page.title %}
{% if page.name %}
    {% assign title = page.name %}
{% endif %}
<title> {{ title }} | {{ site.title }} </title>

But since I have strict_variables set to true, which is really useful for development, Liquid throws an exception on building (undefined variable name included).

In my opinion, strict_variables should not throw exceptions for cases where the undefined variable is checked for existance.

@ptoews

This comment has been minimized.

Show comment
Hide comment
@ptoews

ptoews Sep 28, 2018

I've found a workaround for this case, which works only because page is a hash:

{% assign title = page.title %}
{% for key in page %}
    {% if key == "name" %}
        {% assign title = page.name %}
    {% endif %}
{% endfor %}

ptoews commented Sep 28, 2018

I've found a workaround for this case, which works only because page is a hash:

{% assign title = page.title %}
{% for key in page %}
    {% if key == "name" %}
        {% assign title = page.name %}
    {% endif %}
{% endfor %}
@pushrax

This comment has been minimized.

Show comment
Hide comment
@pushrax

pushrax Oct 1, 2018

Member

This is a really good point, thanks for opening the issue. Some options:

  • your suggestion of using if as a key existence operator in strict_variables mode
  • new filter ( | has: 'name') or operator (if defined page.name or if page has name) to check existence of a key
  • make this work more like a Ruby hash: allow access to missing keys but raise if accessing a key on a missing value
Member

pushrax commented Oct 1, 2018

This is a really good point, thanks for opening the issue. Some options:

  • your suggestion of using if as a key existence operator in strict_variables mode
  • new filter ( | has: 'name') or operator (if defined page.name or if page has name) to check existence of a key
  • make this work more like a Ruby hash: allow access to missing keys but raise if accessing a key on a missing value
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment