-
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
if condition on empty string #223
Comments
You can use the |
Hi, thank you for your response ! |
Well, I just tested : {% if my_variable == blank %} and {% if my_variable.blank? %} And none of them behave as it should :( |
What is the exact output of this?
This will result in one of the following:
Also, to make your logic match up, you'll either want to change Sorry if I wasn't clear -- the |
Shit, that might be a bug. "blank?" is not a Ruby thing but Rails. If you use Jekyll, that might not work. Not sure though. |
True. # same
"".blank? #=> true
"".empty? #=> true
# different
" ".blank? #=> true
" ".empty? #=> false |
I can't reproduce the bug. This:
works for me. |
Do you want to test on the variable being the empty string or do you want to test on it being defined? Try testing for |
WOW lot of passionate talking thank you. my_variable is {% if my_variable == blank %}blank{% else %}"{{ my_variable }}"{% endif %} gave me : my_variable is "" and my_variable is {% if my_variable == empty %}empty{% else %}"{{ my_variable }}"{% endif %} gave me the same exactly. I want to test an empty (not even a space) or unset string my_variable is {% if my_variable == nil %}nil{% else %}"{{ my_variable }}"{% endif %} works fine when the variable is not define. But I still only can test empty string like that : my_variable is {% if my_variable == %}blank or not set{% else %}"{{ my_variable }}"{% endif %} Thanks |
Well my bad ! my_variable is {% if my_variable == %}blank or not set{% else %}"{{ my_variable }}"{% endif %} does not work with empty string. It just behave exactly like nil testing. |
How about |
Excellent ! It works ! Thank you very much ! To let you know : My problem is solved thank you ! |
In case anyone comes across this thread, I had to add 1 more piece to the conditional to make it work:
|
The |
It's a 17 monthes old issue. Maybe it's fixed now. |
none of this really works in jekyll 2.5.4
|
@apla Have you tried the Jekyll 3 beta? Jekyll 2 is still using Liquid 2. |
Is there a / what is the difference between |
Github pages still use Jekyll 2.4.0. Would be great, if this issue would be fixed. |
In meantime I use following workaround: |
What is the way to do this in Jekyll 2? Edit: I just check if the variable exists |
I have been using |
I'm running Jekyll 3.0.1 and had to implement the "capture" workaround proposed by onsitediary-blog-de on April 12. I tried other solutions mentioned in this thread with no luck. |
I found that if the variable has not been set, then I get no value back on the size calculation. To get around this I turned it into a truthy test which seems to be working fine. I'm running Jekyll 3.1.1 {% if component.status.size %} |
In case anyone is still having this issue, try comparing against undefined. I've run into this with page.previous / page.next variables, and was able to solve it by comparing if it's undefined.
|
However, it sounds like the variable was actually |
@edwinwright 's comment works for me on jekyll as well (3.1.6). Thanks! In my case I was trying to find all files but exclude those without a front matter tag defined, in other words, filter out posts with an empty string like Nil, 0, undefined, and blank did not work.
|
Null, nil, empty, blank, undefined; possibilities are many, answers are æthereal and perhaps esoteric. |
|
|
I have a collection and am trying to use
Again, I'm working with pages in a collection, not posts. |
To summarize, here's a table of the boolean value of various values (jekyll 3.7.3, liquid 4.0.0):
Generated with statements like
|
I'm moving my blog from Jekyll 2.5.3 (Liquid 2.6.1) to Jekyll 3.8.0 (Liquid 3.0). In my template, there was an
Now, Liquid complains about that:
See the new code (references are this issue and Checking if a variable is set in Jekyll):
To make things even more verbose, parentheses are not permitted by Liquid, so necessarily I need two Does one believe that change was for good? If so, why? |
@vinyanalista that require 'liquid'
source = <<-LIQUID
!true = {% if !true_var %}true{% else %}false{% endif %}
!false = {% if !false_var %}true{% else %}false{% endif %}
LIQUID
output = Liquid::Template.parse(source).render('true_var' => true, 'false_var' => false)
puts output outputs
It works the same way on 2.6.1, so doesn't appear to be changed in liquid. I think the only difference is that jekyll is using the strict parser now which is catching your syntax error. |
|
It's very, very difficult to check a string for emptiness with Liquid. Shopify/liquid#223 (comment)
* Restructure DS website's information architecture - Removes main nav and moves all nav elements to left-hand sidebar - Changes nav categories - Removes four netlify cms collections that were based on categories and replaces them with a single one called "Pages" - Adds a netlify cms collection called "Navigation" that lets users edit the side nav - Moves special pages like the homepage and the help page to a new collection called "Special pages" - Updates tests and Jekyll/netlify metadata files as appropriate https://GHE/CFPB/el-camino/issues/251 * Update website documentation and how-to-edit-stuff guide * Remove the sample component page from the 'component' section * Only show source tabs for fields with content It's very, very difficult to check a string for emptiness with Liquid. Shopify/liquid#223 (comment) * Add sidebar to homepage * Put anchor tags on own lines Otherwise there's a space between the last word and the final period. I could just put the last anchor tag on its own line but I don't want to alienate it and make it feel uncomfortable. * Fix BEM structure of side nav * Properly check for empty sub categories in side nav
I came here thinking I had a problem detecting blank or empty strings. When, in fact - I actually had a blank record in my
I tried doing something like this to detect the "blank" ID:
But the
Lesson learned: never have a blank record with a stray |
{ % if settings.custom_message % } |
Awesome, thanks for sharing guys and gals. This worked for me when I am looping through paginator.posts. I had a display bug when the author data was empty.
|
|
include.category is ""
include.category is "blank" I ended up with:
Which works as expected but totally looks broken, but works. |
Hello, I was just wanting to test if my_variable was an empty string.
I'm using Liquid through Jekyll if it can help.
I tried :
{% if my_variable == '' %}
I'm not empty
{% endif %}
but no way.
In fact I have to do like this
{% if my_variable == %}
I'm not empty
{% endif %}
It sound ugly to me so I let you know.
By the way it's the same behaviour using case condition.
Thank you for your job on this rocking project.
The text was updated successfully, but these errors were encountered: