Added mg_template() function