-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Comments
I've found a workaround for this case, which works only because
|
This is a really good point, thanks for opening the issue. Some options:
|
Do we have an update on this? |
Just adding my 👍 as we'd love to enable (thanks also for a great templating library!) |
Here is another 👍 as I was hoping to use strict_variables too to avoid dumb mistakes - but it doesn't work as existence check is used in not only my templates but also many themes. I would say to be backwards compatible it should be that |
cc @dylanahsmith @Thibaut IMO this is a bug, and fixing it would make adopting strict_variables possible in Shopify for new templates. Out of the options in #1034 (comment) what do you prefer, or do you have another suggestion? |
I think we need to decide whether we want to expose the concept of undefined as being distinct from Alternatively, we could decide we don't really want that concept of undefined and treat undefined as I think we should try to avoid mixing concepts for consistency, because it would lead to unexpected behaviour. For instance, if we treat the
I think this probably makes the most sense, since it will be conceptually consistent with how liquid works without strict_variables where there isn't a distinction between undefined and |
Defined and present (truthy/falsey) are two different things, IMHO. I would prefer to be able to check if:
while having To achieve this I had to jump through some hoops: I added a custom filter
And then used it in my template like so
while direct access to undefined keys still raises. |
Has a syntax been chosen? It seems like there are a lot of good ideas and one needs to be chosen and implemented. |
@pascalbetz, that custom filter looks exactly like what I'm after – until this issue is resolved, at least. I'm just wondering how you "install" such a custom filter into a Jekyll site. Do I need to create a whole repository, gem, bundle and all to host those 6 lines of code to say |
Sorry, no experience with Jekyll. |
This article on Jekyll plugins taught me that plugins can just be dumped in a |
I've found that the Liquid Example:
|
Looks like |
liquid/lib/liquid/condition.rb Lines 20 to 27 in e83b1e4
Though modifying the implementation of |
Re: docs, Using https://github.com/Shopify/liquid/wiki/Liquid-for-Designers#if--else |
I would also like to be able to check if a top-level variable is defined, not just keys on hashes or methods on drops.
|
This makes a few changes that I was too lazy to split into multiple commits: 1. Sets strict options on Liquid so that it complains when I misuse properties, rather than silently doing nothing. * This has the side effect of complaining about my sorting code. I'm not sure exactly how to write this as the 11ty examples do not work for me. Removed this for now, but I'll need to revisit in the future. 2. Passes through CSS so it can be used in the output. * This also has the side-effecting of requiring each template format to be explicitly stated, so we are limited to markdown and Liquid right now. 3. Added an `index.css` which includes all the required styles for the index page. 4. Added `base.css` which contains "sane" styles for every page (basically just removes annoying styles included by default). 5. Added `theme.css` to contain all the theme constants and assign them as variables on the root scope. 6. Pulled out the header and footer into different fragments with a class as an argument. * I can't quite figure out how to make the class argument optional as this seems to be impossible for top-level options: Shopify/liquid#1034 CSS files all import each other, which is done at runtime and requires many network requests. In the future I'll probably bundle the CSS together into top-level page files to remove the need for extra network requests. CSS variables are prefixed with `--dwac-*` ("Devel Without A Cause"), so they don't conflict with anything else. After a lot of trial and error I found that Liquid include templates can have variables passed in by simply assinging them. Passing data in the `{% include %}` statement itself does not seem to work for me. I wanted to keep the fragment CSS files alongside them in `_includes/`, but this is not served at runtime. Once I introduce the CSS bundler, I can revisit the organization of fragment-specific CSS files.
A brief reminder that this issue is Insanely Stupid (TM) and makes |
Use `contains` for explicit nested config check for one missing var See: Shopify/liquid#1034 (comment)
Use `contains` for explicit nested config check for one missing var See: Shopify/liquid#1034 (comment)
Use `contains` for explicit nested config check for one missing var See: Shopify/liquid#1034 (comment)
Use `contains` for explicit nested config check for one missing var See: Shopify/liquid#1034 (comment)
This should be really changed, it doesn't make any sense that you cannot check if a variable exists like this. |
Lol, I just read my own comment & I was clearly very annoyed by this back then. Hope you'll accept my apologies. I've implemented a simple variant of this in liquidjs now (see issue linked above) & it's working ok for me. Would be nice if it was a little bit smarter (you can't do OR or stuff atm & passing variables to functions has some weird semantics), but it's bearable and I have static checks. |
* Strict checking of variable existence is not enabled due to a long-standing bug: Shopify/liquid#1034
* Strict checking of variable existence is not enabled due to a long-standing bug: Shopify/liquid#1034
Lol, just found my own comment again on this. Please implement a check which doesn't raise exceptions :D (We are using drops btw!) It would be really cool if we could do some checks like in ruby pages&.topic1&.subtopic234 |
I'm not convinced that we need to add an operator. The correct behavior could be: if the variable exists, then Example, with the following payload: {
blue: undefined
} In the LiquidJS template with
|
imo:
Thus the following code would be exception-free. content = "Hello {% if last_name %}{{last_name}}{% else %}placeholder{% endif %}"
template = Liquid::Template.parse(content, error_mode: :strict)
template.render!({}, {strict_variables: true})
I'm surprised this behavior is not supported. |
The current "strict variables" checking strategy in Liquid Templates is somewhat unusable as one cannot even check for the existence of an object property without raising an error in strict mode. This is a known limitation, and actively being discussed upstream (liquid issue 1034). Several Jekyll template make use of checks like {% if page.mermaid %}, etc., preventing enforcement of strict mode. This patch adds a "sane" strict variable policy, which allows for such checks to succeed. Since this is new functionality specific to liqp, we keep the existing strict mode configuration unchanged. Shopify/liquid#1034
The current "strict variables" checking strategy in Liquid Templates is somewhat unusable as one cannot even check for the existence of an object property without raising an error in strict mode. This is a known limitation, and actively being discussed upstream (liquid issue 1034). Several Jekyll template make use of checks like {% if page.mermaid %}, etc., preventing enforcement of strict mode. This patch adds a "sane" strict variable policy, which allows for such checks to succeed. Since this is new functionality specific to liqp, we keep the existing strict mode configuration unchanged. Shopify/liquid#1034
The current "strict variables" checking strategy in Liquid Templates is somewhat unusable as one cannot even check for the existence of an object property without raising an error in strict mode. This is a known limitation, and actively being discussed upstream (liquid issue 1034). Several Jekyll template make use of checks like {% if page.mermaid %}, etc., preventing enforcement of strict mode. This patch adds a "sane" strict variable policy, which allows for such checks to succeed. Since this is new functionality specific to liqp, we keep the existing strict mode configuration unchanged. Shopify/liquid#1034
As @pushrax mentioned, the {% if site.keys contains "salutation" %}
{{ site.salutation }}
{% endif %} |
In the end we added our own tag for the top-level variables issue: module Templating
class IfAvailableTag < Liquid::Block
def initialize(tag_name, var_name, tokens)
super
@var_name = var_name
end
def render(context)
if context.find_variable(@var_name.strip, raise_on_not_found: false)
super
else
""
end
end
end
end
Liquid::Template.register_tag("ifavailable", Templating::IfAvailableTag) |
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:
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.
The text was updated successfully, but these errors were encountered: