Version 5.0.4
The expected/desired outcome is for content-footer to show on ALL pages except for the notebook, and home pages.
This code, doesn’t work. It shows the content-footer on every page including the notebook and home pages.
<?php if ($page->id() != 'notebook' || $page->id() != 'home'): ?>
<?php snippet('content-footer') ?>
<?php endif ?>
This code does work as expected, but feels “wrong” to me.
<?php if ($page->id() == 'notebook' || $page->id() == 'home'): ?>
<?php else: ?>
<?php snippet('content-footer') ?>
<?php endif ?>
The outcomes are the same whether I have this code in a layout, or isolated as the only code in a template. Also I know about $page->isHomePage() and !$page->isHomePage() they both work in this context the same as $page->id() == 'home' and $page->id() != 'home'
What am I doing wrong/missing?
Thanks in advance!
The correct syntax is:
<?php if ($page->id() !== 'notebook' && $page->id() !== 'home'): ?>
<?php snippet('content-footer') ?>
<?php endif ?>
The important part here is the && instead of ||.
This does work (thank you), but it doesn’t make sense. From my understanding && (and) means that both conditions must be true for it to return true. In this case there is never a case where the page→id() is both home and notebook. This is why I was using || (or) as it just needs one of the conditions to be true to return true, again from my understanding.
Can you expand on why && works here even though it seems it should not? Is this a Kirby quirk, or am I fundamentally misunderstanding the logical operators?
Well, I think the problem you have is the negation. Look at the example code you felt looked wrong:
<?php if ($page->id() == 'notebook' || $page->id() == 'home'): ?>
<?php else: ?>
<?php snippet('content-footer') ?>
<?php endif ?>
Well, this example is not good coding style, because you should not use “empty” conditions where you don’t do anything. But the code itself is perfectly valid.
Now if you want to negate the condition
<?php if ($page->id() == 'notebook' || $page->id() == 'home'): ?>
you not only have to replace the == with != but also always change the || to &&.
In plain English that would read: If the page id is not notebook AND also not home, then do something. I.e. album is not the same as notebook and not the same as home, so fine, let’s show the footer.
Thank you that makes perfect sense.