Refactored codgen_engine to use new template handling