If there are "1M reasons why you can't use a template," and "1M reasons why you can't use CGI," what is there to say? How are we supposed to help you debug this? You are building a complex thing that is one-of-a-kind by your choice, and then one day you will move on and there will be no one in the world to maintain it. Your successor will scrap it all and start again.