merged write and writeTemplate