Rewrite of template generator in PHP