Tinderbox User-to-User Forum (for formal tech support please email: info@eastgate.com)
http://www.eastgate.com/Tinderbox/forum//YaBB.cgi
Tinderbox Users >> Exporting from Tinderbox >> Conditional tests to export links
http://www.eastgate.com/Tinderbox/forum//YaBB.cgi?num=1220317706

Message started by Paul Walters on Sep 1st, 2008, 9:08pm

Title: Conditional tests to export links
Post by Paul Walters on Sep 1st, 2008, 9:08pm

In HTML export I want to include a div for inbound links to a note and a div for outbound links, conditioned on whether such links exist.  I am performing conditional tests in the export template, wrapped around the div code.

The test


Code:
^if(^exists(basicLinks)^)^


evaluates as true or false, depending upon whether there exists outbound basic links from a note.

whereas


Code:
^if(^exists(inboundBasicLinks)^)^


apparently always evaluates as true.  And so, that div appears in the export whether or not there are, in fact, inbound basic links.  (The same occurs when I test inboundLinkCount - apparently it is always true.)

Is there a better test?

Title: Re: Conditional tests to export links
Post by Mark Anderson on Sep 2nd, 2008, 3:22am

You're assessing the export code ^basicLinks^ & ^inboundBasicLinks^ but not using the carets.  Before looking at anything else, I'd change the code to:

Code:
if(^exists(^basicLinks^)^)^
...and...
^if(^exists(^inboundBasicLinks^)^)^

Otherwise, you may be assessing false positives/negatives.  There may still be a problem to overcome but it should be the right one. In learning TB I tripped over this a fair bit to start - the fact that code errors can still get coerced to a Boolean result, albeit a false one (no pun intended!).

On a general note for all those reading this, export codes names are case insensitive but TB usage custom is to write them intercapitalised to help distinguish them from action codes (normally all lowercase), attributes (normally both capitalised and intercapitalised), etc. Closing carets (^) on export codes are also optional but encouraged, especially during troubleshooting like this as they tell TB where the code ends rather than leaving the app to guess (almost always correctly, it should be said).

Title: Re: Conditional tests to export links
Post by Paul Walters on Sep 2nd, 2008, 5:30am

Well, I tried


Code:
^if(^exists(^basicLinks^)^)^ ...and... ^if(^exists(^inboundBasicLinks^)^)^


and nothing was exported.

Later, I found a note in the TB wiki from Eric Abrahamsen (at http://www.eastgate.com/wiki2/wiki.cgi?TestingIfConditions) suggesting the following tests, which worked for me too:


Code:
^if(basicLinks)^ ... and ... ^if(inboundBasicLinks)^

Title: Re: Conditional tests to export links
Post by Mark Anderson on Sep 2nd, 2008, 9:28am

Now I'm reminded, the very last paragraph of the wiki page you cited helps explain - if at all - that the issues is one of testing a condition exists vs a query where matches are found or not.  As the aTbRef page for ^exists()^ notes, this tests either:
  • existence of an item object.
  • a non-empty attribute value.
Of course, a populated Export code (for links) is neither of the above.  The 'error', insofar as there is one, is using the wrong test - i.e. ^exists^.  

The successful ^if(basicLinks)...etc. usage is employing (undocumented) situation whereby if the code has links then that fact is coerced to a 'true' Bolean result; note though the scope for false positives when relying on a feature not fully laid bare.  I've not time to build a full test rig but I think that your test ought to be ^if(^basicLinks^)...etc., and that luckily TB is figuring that you're trying to test the ^basicLinks^ export code and not something else. Interestingly, I note that cited wiki page's first problem was partly due to not using closing carets!

Again to help any lurkers wading through this the following are probably all parsed as the same thing by Tinderbox:
  • ^if(basicLinks)
  • ^if(^basicLinks)
  • ^if(^basicLinks^)
  • ^if(^basicLinks()^)
  • ... and doubtless other variations.  Where argument(s) to a code are optional, the brackets may be ommited.
In the case of basicLinks, the default list HTML is used if no arguments are provided. Simple to use...once you know how.

This reminds me that there's currently no good learner's article on coercion in if tests. It's something that's unintuitive to non-programming types.


Tinderbox User-to-User Forum (for formal tech support please email: info@eastgate.com) » Powered by YaBB 2.2.1!
YaBB © 2000-2008. All Rights Reserved.