Problems, need help? Have a tip or advice? Post it here.
4 posts Page 1 of 1
I know this may sound odd but something is driving me a bit batty and I'd love to get an opinion.

When I use the if statement with k_template_name

Code: Select all
<li><a href="<cms:if k_template_name != 'about.php' ><cms:show k_site_link /></cms:if>#templatemo_about">About</a></li>


everything is fine. I look at the source code from the 'about' page (the template name being 'about.php') and see this:

Code: Select all
<a href="#templatemo_about" class="current">About</a>


But when I add the OR logic:

Code: Select all
<li><a href="<cms:if k_template_name != 'about.php'|| k_template_name != 'index.php' ><cms:show k_site_link /></cms:if>#templatemo_about">About</a></li>


I get this on both the index page and about page. I did use cms:dump to make 100% sure of the template names.
Code: Select all
<a href="http://mysite.com/#templatemo_about">About</a>


from cms:dump -
k_template_name: index.php
k_template_name: about.php

I'm wondering if my OR logic is formatted correctly. I've tried reversing the quote marks, I've added parenthesis to both sets.

I can't think of anything else to do so I'm reaching out. From the guide (http://docs.couchcms.com/tags-reference/if.html) I feel like I'm formatting it correctly. Any help? :roll:
|| Is fine, but there is seemingly no logic behind the code. What are you trying to achieve? It is not 100% clear. Your posted code works 100% up to the what's written.
Join COUCH:TALK channel here https://t.me/couchcms_chat
Ryazania — a framework to boost productivity with Add-ons viewtopic.php?f=2&t=13475
Support my efforts to help the community https://boosty.to/trendo/donate
@scratz, I'll try to explain the boolean logic behind the statement :)

You are trying to use boolean 'OR' for which the truth table is -
0 || 0 = 0
0 || 1 = 1
1 || 0 = 1
1 || 1 = 1

where the left hand side represents the two statements you are trying to combine and the right hand side represents the outcome.

OK, so now try to see what happens when, for example, you visit 'index.php'.
The first part of your statement evaluates to 1 because k_template_name is indeed not 'about.php' -
<cms:if k_template_name != 'about.php' || k_template_name != 'index.php' >

becomes
<cms:if 1 || k_template_name != 'index.php' >

while the second part evaluates to '0' because k_template_name *is* index.php - so the statement becomes
<cms:if 1 || 0 >

Now consulting the truth table, you can see that 1 || 0 is equal to 1 and therefore the <cms:if> statement executes and outputs the site link.

Now consider what happens when we visit 'about.php' -
<cms:if k_template_name != 'about.php' || k_template_name != 'index.php' >

becomes
<cms:if 0 || k_template_name != 'index.php' >

while the second part evaluates to '1' because k_template_name is definitely not index.php - so the statement becomes
<cms:if 0 || 1 >

Consult the truth table and you'll see that 0 || 1 is also equal to 1 and therefore the <cms:if> statement executes in this case also.

That should explain why your statement is not working he way you want.

OK now that, hopefully, you can see what is happening, try using boolean AND instead of OR. The truth table for AND being -
0 && 0 = 0
0 && 1 = 0
1 && 0 = 0
1 && 1 = 1

So suppose your statement is coded as -
Code: Select all
<cms:if k_template_name != 'about.php'   &&   k_template_name != 'index.php' >

Now consider what happens when you visit 'index.php' -
The first part of your statement evaluates to 1 because k_template_name is indeed not 'about.php' -
<cms:if k_template_name != 'about.php' || k_template_name != 'index.php' >

becomes
<cms:if 1 || k_template_name != 'index.php' >

while the second part evaluates to '0' because k_template_name *is* index.php - so the statement becomes
<cms:if 1 || 0 >

Now consulting the truth table, you can see that 1 || 0 is equal to 0 and therefore the <cms:if> statement does not execute.

Now consider what happens when we visit 'about.php' -
<cms:if k_template_name != 'about.php' || k_template_name != 'index.php' >

becomes
<cms:if 0 || k_template_name != 'index.php' >

while the second part evaluates to '1' because k_template_name is definitely not index.php - so the statement becomes
<cms:if 0 || 1 >

Consult the truth table and you'll see that 0 || 1 is also equal to 0 and therefore the <cms:if> statement does not execute in this case also

So, the solution to your problem is to use AND instead of OR.

Hope it helps.
I'm sorry it took so long to implement this solution. So many other fires to put out.
This seems to do the trick. Interesting, very very interesting... :oops:

Thank you KK for not only answering, but explaining the why behind it all. This is a great forum :mrgreen:
4 posts Page 1 of 1
cron