What the app mainly does is generate HTML code and upload it to the client's website.
I'm almost finished with it except I hit a problem where I can't think up of the correct logic to tackle a certain issue.

Here's the thing:
I need to make certain generated codes get distributed to 3 columns of in an HTML page. I know that didn't explain much about what I want to be done so here's a visual representation.

There is an HTML code (template) that I put into a variable tTemplate.
Block 1 is always present in the template.
Block 2 is optionally present depending on a setting of the user.
Block A, Block B and Block C is always present.
Block A is a single line in the template as "[$BLOCK_A$]".
Block B is a single line in the template as "[$BLOCK_B$]".
Block C is a single line in the template as "[$BLOCK_C$]".
Now let's say I have a block of code called "CodeBlock 1".
Let's say I have 9 items similar to "CodeBlock 1".
That would make it 10 CodeBlocks.
I decided I would use the replace command to process it.
Suppose I use the command replace "[$BLOCK_A$]" with "CodeBlock 1" in tTemplate.
This would've easily worked.
But I need to evenly distribute all 10 CodeBlocks onto Block A, Block B and Block C.
An example of how I want the CodeBlocks to be distributed is displayed below.

Now what IF Block 2 from before isn't enabled by the user?
That would result to the middle column to be shorter than the other two.
To resolve that problem, I would need to make the CodeBlocks adjust accordingly to the loss of the Block 2.
An example is shown below if ever Block 2 wasn't present, the CodeBlocks are re-arranged.

If all these is still confusing you, please tell me what more information I can provide to make it clearer for you to understand. Thanks for answering and sorry for not knowing how to tackle this.
