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 >> if statement in html export
http://www.eastgate.com/Tinderbox/forum//YaBB.cgi?num=1376022644

Message started by Steve Scott on Aug 9th, 2013, 12:30am

Title: if statement in html export
Post by Steve Scott on Aug 9th, 2013, 12:30am

Hi,

I'm trying to get the hang of if statements in an html export:

I have consulted this page: http://www.acrobatfaq.com/atbref5/index/ExportCodes/ExportCodes-FullListing/ifcondition.html, but I am a little confused.

I am exporting a series of attributes such as due dates, URL, point values, etc.  

If there is an attribute that has a URL, the html page would look something like:

Assignment Title: Reflection on Tinderbox
Assignment URL:  www.yahoo.com
Points: 10 points

If there is no attribute that has a URL, the html page would look something like:

Assignment Title: Reflection on Tinderbox
Points: 10 points

I have tried things like:
^if(Name(^value $AssignmentURL))^ Assignment URL: ^value( $AssignmentURL )^

To get to the point point, is there a "then" part to if statements?  In other words, if the value for AssignmentURL, then include it in the export.  If not, omit the line.

Hope this makes sense.  Thanks for any help.

Steve

Title: Re: if statement in html export
Post by Mark Anderson on Aug 9th, 2013, 3:11am

This works for me (tested in TB v5.12.1):

Code:
<p class="asgndetails">
<span class="asgntitle">Assignment Title:</span> ^value($AssignmentTitle)^<br>
^if(^value($AssignmentURL)^)^<span class="asgntitle">Assignment URL:</span> <a href="^value($AssignmentURL)^">^value($AssignmentURL)^</a><br>
^endIf^<span class="asgntitle">Points:</span>^value($AssignmentPoints)^ point^if($AssignmentPoints!=1)^s^endIf^
</p>

We test the URL using a 'short' form Boolean test (i.e. one returning a simple true/false answer). Asking ^if(^value($AssignmentURL)^)^ is asking TB "does this note have a non-default value for attribute $AssignmentURL?". The previous links discusses what default means in this context. There is also a negative version of the test.

These Boolean tests work with both the export ^if(query)^ and action code if(query) conditional statement queries. Confusion may arise when looking at old code examples pre-dating v5.x, as export code used to have slightly different conventions for 'if' queries, because query language differed from action code. Since v5.x query code has been subsumed into action code and any conditional statement query is written in action code.

Tidy whitespace management output in export requires slightly messier looking export code. To avoid leaving a blank line in the HTML put the ^endIf^ statement at the beginning of the next line. That way a line return for the URL line is only emitted if the URL is present and exported.

If you're using a URL field, it makes sense to leverage hypertext and link, so I've done so in the code by wrapping the URL in an <a> element.

You could use <b> tags for bolding the titles but a <span> and CSS seems a more modern and flexible way to go.

Title: Re: if statement in html export
Post by Mark Anderson on Aug 9th, 2013, 3:31am

^if(Name(^value $AssignmentURL))^ Assignment URL: ^value( $AssignmentURL )^

It's worth noting some unintended errors in the above code, which likely helped cause the original confusion.

^if(Name. TB will probably interpret Name as $Name, but if you mean to refer to the 'Name' attribute value don't omit the $-prefix. TB might cope only because of undocumented legacy support for very old documents before the $ was introduced. Today, in v5+, assume the prefix is mandatory for an attribute reference.

^if(Name(^value $AssignmentURL))^. This is why I suggest you always use closing ^, unless you know better; even then use a closing ^. Why? If you don't you force TB to guess where export codes finish (a source line break is always a finish, FWIW). You also omitted the opening parenthesis of ^value()^. Therefore:

^if(Name(^value $AssignmentURL))^

should be:

^if($Name(^value($AssignmentURL)^)^

But although the above is now valid code, it means "if the note title [value of this note's AssignmentURL attribute] exists?". You meant to ask "does this note's AssignmentURL attribute have a value?". The answer to the latter is in my previous post. Now a few more minor observations…

^value( $AssignmentURL )^. White space inside export codes' parentheses is ignored, so that's fine. Note that if inserting string literal spaces in output, don't forget to quote them: " ".

Lastly, as mentioned, the end of the line closes codes so the ^if^ output is terminated. Still, I think it better practice to put an explicit ^endIf^ at the end of the conditional output. It helps TB understand your intent and it helps you understand what you meant when you revisit it several years later. I find it helps to embrace constraint, even if you do type a few more characters of code.

If you don't understand any of the rules of export code usage, do please ask (suggest in a separate thread) - I'm sure you won't be the only one.

Title: Re: if statement in html export
Post by Steve Scott on Aug 9th, 2013, 7:48am

Thank you once again, Mark for your guidance.  The code works perfectly.  The ^endIf^ does help me to understand where the command stops.  I'm making progress thanks to you!

Title: Re: if statement in html export
Post by Mark Anderson on Aug 9th, 2013, 9:31am

Great. Happy to help.

If you look at aTbRef's source TBX (here) and how it exports you can see how you can make the document export support resources like the CSS file (it could as easily be some JavaScript.

Images. In aTbRef's context, I don't embed images in documentation TBXs (where maps aren't pertinent) so as not to bloat the TBX size. However, you can see in the aTbRef export templates how I link to an image per note/page (via an attribute. If you want more, images per note then an inline macro is the way to go. Something like, this "ImgTag":

Macro Code

Code:
<img src="$1"^if($2)^ width="$2"^endif^^if($3)^ height="$3"^endif^ alt="$4" title="$4"^if($5)^ id="$5"^endif^^if($6)^ class="$6"^endif^>


Inline usage in $Text, where the image is to appear in HTML, usage is like:

^do("ImgTag","^root^images/mypic.png","200","50","my logo","logo","pix")^

Which exports as:

<img src="../../images/mypic.png" width="200" height="50" alt="my logo" title="my logo" id="logo" class="pix">

Note how only the file name/path and alt tag are mandatory, the other inputs can be left blank and still work:

^do("ImgTag","^root^images/mypic.png",,,"my logo",,)^

gives a minimal valid <img> tag:

<img src="../../images/mypic.png" alt="my logo" title="my logo">

Anyway, there are few more export ideas.

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.