move code to before_template