Presenting Your Code to the Readers  

Lately, Phoenix asked me how to show template code to the readers so they could cut-and-paste right into their templates and use right away. Having some experiences on this, I'd like to share with you all, so you'll be able to avoid some traps along your way of sharing code with others.

This is how I prepare for a typical tutorial:

1. Code it up.

2. Cut and paste the finished hack code into Notepad.




3. Do a "replace" command, replacing 3 things:

* "<" replaced by "& l t ;" (one word, that's code for less than value)



* ">" replaced by "& g t ;" (one word, that's code for greater than value)




* hit "spacebar" one in the "find what" box, then key in the "replace with:" this text: "& n b s p ;" (one word for non-breaking space.) This replacement is required to make the code indented.



4. Now comes the hard part.

As you well know, a tag consists of an open less-than sign < and a closed greater-than sign. They always go hand in hand, I mean in pair, like that. But what if, in some cases as in a comparison statement, where only one is shown, like so:

for (var i = 0; i < json.feed.entry.length; i++) {


If your user cut the code from your article it won't work because Blogger stores data as xmls, and a single "<" will mess up the system. To overcome this, you have to show your user a less-than sign as "& a m p ; l t ;" (all as one word.) so what he'll see is "& l t ;" . Then Javascript will understand this as the same as the < sign.



After replacing like that, the code would look like this on the screen for your reader to see:


for (var i = 0; i &lt; json.feed.entry.length; i++) {


By now, the code would look like this:



and the result would look like this:



I hope this will help our potential Beta hackers, as well as our regular readers.

Cheers,