Thanks for sharing. I’ve fixed the formatting of the code samples and fixed some errors . The erroneous code syntax likely works due to support for lecagy code in old TBX. Way back, export/action code has far fewer options and could be written pretty much in free-text. As the code has got more capable/complex the scope for mis-parsing intent grows.
In the case of export code, I’d recommend to learners that they close export codes with a ^ as it makes clear to yourself if not the app that you understand where codes start/stop. In export code I’ve learned to quote paths to templates, notwithstanding action code practice, but that may be because at some point is did matter. I did a bit more correction here:
^if(^children^)
^if($HTMLDontExport=False)^
In queries the equality test is ==, a = only works on a legacy basis. Also boolean values are case-sensitive true and false. My hunch is the code works here on a legacy call back but with coercion of type value there is a possibility of “False” coercing to a true. Why? A false string would be empty, so
“False” is a string with content so, true. This is why it is a good idea to use the current correct syntax against the day when legacy support doesn’t ‘just work’ as expected. 
On a general note, thanks for taking the time to share your workflow description.