in reply to Re: Re: Mixed-up output
in thread Mixed-up output

I think the second step is the one that's not clear. Here's how my solution works:
Read and print from TEMPLATE until <!-- MAIN -->. Read but don't print from TEMPLATE until <!-- ENDMAIN -->. Read and print everything from FORMTEMPLATE. Read and print everything that's left from TEMPLATE.
I figured that <!-- MAIN --> and <!-- ENDMAIN --> may contain some text that should be replaced by the form. The second loop just skips over all of that.