Jekyll liquid if statement confusion












1















I'm trying to make a if statement work according to my markdown post's liquid header where if a variable is set to true, do a thing, else and if it doesn't exist do the other thing. Just can't seem to get it to work properly.



I've tried changing the if statement to {% unless %}. trying different combos of != false and swapping the image code around.



{% capture banner %}{{ page.banner }}{{ post.banner }}{% endcapture %}
{% capture no-border %}{{ page.no-border }}{{ post.no-border }}{% endcapture %}
{% capture title %}{{ page.title }}{{ post.title }}{% endcapture %}

{% if banner != "" %}
{% if no-border == true %}
<img class="center no-border" src="{{ banner }}" alt="{{ title }}"/><br/>
{% else %}
<img class="center" src="{{ banner }}" alt="{{ title }}"/><br/>
{% endif %}
{% endif %}


I expected to see: if no-border is set to true in markdown file liquid portion, remove border of banner image.










share|improve this question





























    1















    I'm trying to make a if statement work according to my markdown post's liquid header where if a variable is set to true, do a thing, else and if it doesn't exist do the other thing. Just can't seem to get it to work properly.



    I've tried changing the if statement to {% unless %}. trying different combos of != false and swapping the image code around.



    {% capture banner %}{{ page.banner }}{{ post.banner }}{% endcapture %}
    {% capture no-border %}{{ page.no-border }}{{ post.no-border }}{% endcapture %}
    {% capture title %}{{ page.title }}{{ post.title }}{% endcapture %}

    {% if banner != "" %}
    {% if no-border == true %}
    <img class="center no-border" src="{{ banner }}" alt="{{ title }}"/><br/>
    {% else %}
    <img class="center" src="{{ banner }}" alt="{{ title }}"/><br/>
    {% endif %}
    {% endif %}


    I expected to see: if no-border is set to true in markdown file liquid portion, remove border of banner image.










    share|improve this question



























      1












      1








      1








      I'm trying to make a if statement work according to my markdown post's liquid header where if a variable is set to true, do a thing, else and if it doesn't exist do the other thing. Just can't seem to get it to work properly.



      I've tried changing the if statement to {% unless %}. trying different combos of != false and swapping the image code around.



      {% capture banner %}{{ page.banner }}{{ post.banner }}{% endcapture %}
      {% capture no-border %}{{ page.no-border }}{{ post.no-border }}{% endcapture %}
      {% capture title %}{{ page.title }}{{ post.title }}{% endcapture %}

      {% if banner != "" %}
      {% if no-border == true %}
      <img class="center no-border" src="{{ banner }}" alt="{{ title }}"/><br/>
      {% else %}
      <img class="center" src="{{ banner }}" alt="{{ title }}"/><br/>
      {% endif %}
      {% endif %}


      I expected to see: if no-border is set to true in markdown file liquid portion, remove border of banner image.










      share|improve this question
















      I'm trying to make a if statement work according to my markdown post's liquid header where if a variable is set to true, do a thing, else and if it doesn't exist do the other thing. Just can't seem to get it to work properly.



      I've tried changing the if statement to {% unless %}. trying different combos of != false and swapping the image code around.



      {% capture banner %}{{ page.banner }}{{ post.banner }}{% endcapture %}
      {% capture no-border %}{{ page.no-border }}{{ post.no-border }}{% endcapture %}
      {% capture title %}{{ page.title }}{{ post.title }}{% endcapture %}

      {% if banner != "" %}
      {% if no-border == true %}
      <img class="center no-border" src="{{ banner }}" alt="{{ title }}"/><br/>
      {% else %}
      <img class="center" src="{{ banner }}" alt="{{ title }}"/><br/>
      {% endif %}
      {% endif %}


      I expected to see: if no-border is set to true in markdown file liquid portion, remove border of banner image.







      html if-statement logic jekyll liquid






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 1 at 6:10







      Michael SanAngelo

















      asked Dec 31 '18 at 22:09









      Michael SanAngeloMichael SanAngelo

      86




      86
























          1 Answer
          1






          active

          oldest

          votes


















          0














          Your capture tag, without page.no-border or post.no-border, returns an empty string, which evaluates to true because all values in liquid are truthy, except for false and nil. Try this instead (or something like it) :



          {% capture banner %}{{ page.banner }}{{ post.banner }}{% endcapture %}
          {% capture title %}{{ page.title }}{{ post.title }}{% endcapture %}

          {% if page.banner or post.banner %}
          {% if page.no-border or post.no-border %}
          <img class="center no-border" src="{{ banner }}" alt="{{ title }}"/><br/>
          {% else %}
          <img class="center" src="{{ banner }}" alt="{{ title }}"/><br/>
          {% endif %}
          {% endif %}


          This is all untested and you could accomplish it a few different ways.



          Edit: Clarification



          Detailed Explanation:



          capture is a function. It evaluates whatever is inside, and stores it as a string in a variable. If the contents are nil (nothing), then it returns an empty string ("").



          In programming, every value is considered 'truthy' or 'falsy'. This means that (among other things) when placed in an if statement, truthy values will execute the statement and falsy values won't. Take an integer value of 1 for example; in most languages, this is a truthy value, so



          if 1
          puts 'hello world'
          end


          will print 'hello world' to the console. nil is generally a falsy value, so



          if nil
          puts 'hello world'
          end


          will do nothing.



          Exactly which values are truthy or falsy depend on the programming language. In Liquid, everything is truthy except for nil and false. capture always returns a string, and all strings, even empty ones, are truthy.



          If you write this:



          {% if "" %}
          <img class="center no-border" src="{{ banner }}" alt="{{ title }}"/><br/>
          {% else %}
          <img class="center" src="{{ banner }}" alt="{{ title }}"/><br/>
          {% endif %}


          You will always get the no-border version. Replace that if statement with if "true", or if true and you will get the same result.






          share|improve this answer


























          • well that seems to work. why wouldn't the capture tag work if it's supposed to pull from the same page and post tags?

            – Michael SanAngelo
            Jan 1 at 22:23











          • I added a detailed explanation to the answer. Hopefully it helps!

            – Robert Buchberger
            Jan 2 at 8:27











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53991783%2fjekyll-liquid-if-statement-confusion%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          Your capture tag, without page.no-border or post.no-border, returns an empty string, which evaluates to true because all values in liquid are truthy, except for false and nil. Try this instead (or something like it) :



          {% capture banner %}{{ page.banner }}{{ post.banner }}{% endcapture %}
          {% capture title %}{{ page.title }}{{ post.title }}{% endcapture %}

          {% if page.banner or post.banner %}
          {% if page.no-border or post.no-border %}
          <img class="center no-border" src="{{ banner }}" alt="{{ title }}"/><br/>
          {% else %}
          <img class="center" src="{{ banner }}" alt="{{ title }}"/><br/>
          {% endif %}
          {% endif %}


          This is all untested and you could accomplish it a few different ways.



          Edit: Clarification



          Detailed Explanation:



          capture is a function. It evaluates whatever is inside, and stores it as a string in a variable. If the contents are nil (nothing), then it returns an empty string ("").



          In programming, every value is considered 'truthy' or 'falsy'. This means that (among other things) when placed in an if statement, truthy values will execute the statement and falsy values won't. Take an integer value of 1 for example; in most languages, this is a truthy value, so



          if 1
          puts 'hello world'
          end


          will print 'hello world' to the console. nil is generally a falsy value, so



          if nil
          puts 'hello world'
          end


          will do nothing.



          Exactly which values are truthy or falsy depend on the programming language. In Liquid, everything is truthy except for nil and false. capture always returns a string, and all strings, even empty ones, are truthy.



          If you write this:



          {% if "" %}
          <img class="center no-border" src="{{ banner }}" alt="{{ title }}"/><br/>
          {% else %}
          <img class="center" src="{{ banner }}" alt="{{ title }}"/><br/>
          {% endif %}


          You will always get the no-border version. Replace that if statement with if "true", or if true and you will get the same result.






          share|improve this answer


























          • well that seems to work. why wouldn't the capture tag work if it's supposed to pull from the same page and post tags?

            – Michael SanAngelo
            Jan 1 at 22:23











          • I added a detailed explanation to the answer. Hopefully it helps!

            – Robert Buchberger
            Jan 2 at 8:27
















          0














          Your capture tag, without page.no-border or post.no-border, returns an empty string, which evaluates to true because all values in liquid are truthy, except for false and nil. Try this instead (or something like it) :



          {% capture banner %}{{ page.banner }}{{ post.banner }}{% endcapture %}
          {% capture title %}{{ page.title }}{{ post.title }}{% endcapture %}

          {% if page.banner or post.banner %}
          {% if page.no-border or post.no-border %}
          <img class="center no-border" src="{{ banner }}" alt="{{ title }}"/><br/>
          {% else %}
          <img class="center" src="{{ banner }}" alt="{{ title }}"/><br/>
          {% endif %}
          {% endif %}


          This is all untested and you could accomplish it a few different ways.



          Edit: Clarification



          Detailed Explanation:



          capture is a function. It evaluates whatever is inside, and stores it as a string in a variable. If the contents are nil (nothing), then it returns an empty string ("").



          In programming, every value is considered 'truthy' or 'falsy'. This means that (among other things) when placed in an if statement, truthy values will execute the statement and falsy values won't. Take an integer value of 1 for example; in most languages, this is a truthy value, so



          if 1
          puts 'hello world'
          end


          will print 'hello world' to the console. nil is generally a falsy value, so



          if nil
          puts 'hello world'
          end


          will do nothing.



          Exactly which values are truthy or falsy depend on the programming language. In Liquid, everything is truthy except for nil and false. capture always returns a string, and all strings, even empty ones, are truthy.



          If you write this:



          {% if "" %}
          <img class="center no-border" src="{{ banner }}" alt="{{ title }}"/><br/>
          {% else %}
          <img class="center" src="{{ banner }}" alt="{{ title }}"/><br/>
          {% endif %}


          You will always get the no-border version. Replace that if statement with if "true", or if true and you will get the same result.






          share|improve this answer


























          • well that seems to work. why wouldn't the capture tag work if it's supposed to pull from the same page and post tags?

            – Michael SanAngelo
            Jan 1 at 22:23











          • I added a detailed explanation to the answer. Hopefully it helps!

            – Robert Buchberger
            Jan 2 at 8:27














          0












          0








          0







          Your capture tag, without page.no-border or post.no-border, returns an empty string, which evaluates to true because all values in liquid are truthy, except for false and nil. Try this instead (or something like it) :



          {% capture banner %}{{ page.banner }}{{ post.banner }}{% endcapture %}
          {% capture title %}{{ page.title }}{{ post.title }}{% endcapture %}

          {% if page.banner or post.banner %}
          {% if page.no-border or post.no-border %}
          <img class="center no-border" src="{{ banner }}" alt="{{ title }}"/><br/>
          {% else %}
          <img class="center" src="{{ banner }}" alt="{{ title }}"/><br/>
          {% endif %}
          {% endif %}


          This is all untested and you could accomplish it a few different ways.



          Edit: Clarification



          Detailed Explanation:



          capture is a function. It evaluates whatever is inside, and stores it as a string in a variable. If the contents are nil (nothing), then it returns an empty string ("").



          In programming, every value is considered 'truthy' or 'falsy'. This means that (among other things) when placed in an if statement, truthy values will execute the statement and falsy values won't. Take an integer value of 1 for example; in most languages, this is a truthy value, so



          if 1
          puts 'hello world'
          end


          will print 'hello world' to the console. nil is generally a falsy value, so



          if nil
          puts 'hello world'
          end


          will do nothing.



          Exactly which values are truthy or falsy depend on the programming language. In Liquid, everything is truthy except for nil and false. capture always returns a string, and all strings, even empty ones, are truthy.



          If you write this:



          {% if "" %}
          <img class="center no-border" src="{{ banner }}" alt="{{ title }}"/><br/>
          {% else %}
          <img class="center" src="{{ banner }}" alt="{{ title }}"/><br/>
          {% endif %}


          You will always get the no-border version. Replace that if statement with if "true", or if true and you will get the same result.






          share|improve this answer















          Your capture tag, without page.no-border or post.no-border, returns an empty string, which evaluates to true because all values in liquid are truthy, except for false and nil. Try this instead (or something like it) :



          {% capture banner %}{{ page.banner }}{{ post.banner }}{% endcapture %}
          {% capture title %}{{ page.title }}{{ post.title }}{% endcapture %}

          {% if page.banner or post.banner %}
          {% if page.no-border or post.no-border %}
          <img class="center no-border" src="{{ banner }}" alt="{{ title }}"/><br/>
          {% else %}
          <img class="center" src="{{ banner }}" alt="{{ title }}"/><br/>
          {% endif %}
          {% endif %}


          This is all untested and you could accomplish it a few different ways.



          Edit: Clarification



          Detailed Explanation:



          capture is a function. It evaluates whatever is inside, and stores it as a string in a variable. If the contents are nil (nothing), then it returns an empty string ("").



          In programming, every value is considered 'truthy' or 'falsy'. This means that (among other things) when placed in an if statement, truthy values will execute the statement and falsy values won't. Take an integer value of 1 for example; in most languages, this is a truthy value, so



          if 1
          puts 'hello world'
          end


          will print 'hello world' to the console. nil is generally a falsy value, so



          if nil
          puts 'hello world'
          end


          will do nothing.



          Exactly which values are truthy or falsy depend on the programming language. In Liquid, everything is truthy except for nil and false. capture always returns a string, and all strings, even empty ones, are truthy.



          If you write this:



          {% if "" %}
          <img class="center no-border" src="{{ banner }}" alt="{{ title }}"/><br/>
          {% else %}
          <img class="center" src="{{ banner }}" alt="{{ title }}"/><br/>
          {% endif %}


          You will always get the no-border version. Replace that if statement with if "true", or if true and you will get the same result.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Jan 2 at 8:26

























          answered Jan 1 at 17:50









          Robert BuchbergerRobert Buchberger

          17617




          17617













          • well that seems to work. why wouldn't the capture tag work if it's supposed to pull from the same page and post tags?

            – Michael SanAngelo
            Jan 1 at 22:23











          • I added a detailed explanation to the answer. Hopefully it helps!

            – Robert Buchberger
            Jan 2 at 8:27



















          • well that seems to work. why wouldn't the capture tag work if it's supposed to pull from the same page and post tags?

            – Michael SanAngelo
            Jan 1 at 22:23











          • I added a detailed explanation to the answer. Hopefully it helps!

            – Robert Buchberger
            Jan 2 at 8:27

















          well that seems to work. why wouldn't the capture tag work if it's supposed to pull from the same page and post tags?

          – Michael SanAngelo
          Jan 1 at 22:23





          well that seems to work. why wouldn't the capture tag work if it's supposed to pull from the same page and post tags?

          – Michael SanAngelo
          Jan 1 at 22:23













          I added a detailed explanation to the answer. Hopefully it helps!

          – Robert Buchberger
          Jan 2 at 8:27





          I added a detailed explanation to the answer. Hopefully it helps!

          – Robert Buchberger
          Jan 2 at 8:27




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53991783%2fjekyll-liquid-if-statement-confusion%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

          SQL update select statement

          'app-layout' is not a known element: how to share Component with different Modules