You can do that with the UI itself…
Here’s a screenshot:
http://aatospaja.fi/temp/template.jpg
When you set your fields to show up based on the template, they will automatically show up when you select the appropriate page template from the page settings.