Templates part III - Using Master Templates

Thursday, December 13, 2007 by Niels Hartvig

Master Templates

A master template is the exact same thing as ordinary Templates and they're created in the same way as we created our "Home Page" template in the previous tutorials. The only difference is that a Master Template includes a bit of information about where Child Templates should be inserted. This is a very simple, but efficient way to ensure that you don't repeat yourself and only need to make modifications in one place. You can even nest multiple master templates for maximum flexibility.

For a start we'll create a Master template based on the markup in our "Home Page" template. Then we'll update our "Home Page" template to use our new master template and at last we'll do the same for our "Text Page" templates. So head over to the "Settings" section of umbraco and let's get started.

Creating a Master Template

To create a Master Template, simply right click "Templates" and choose create:

In the Dialog, call the new Master Template "Master" and click "Create:

As you can see in the tree view, a new template has been created, so let's copy the markup from our "Home Page" template to the master.

Click the "Home Page" template in the tree view and copy the markup (by either using the mouse or keyboard short-cuts: CTRL+A and CTRL+C):

Now, click the "Master" template in the tree view and paste the contents into the text area.

The next step is to make a little modification to our markup which will turn this template into a Master Template. We'll replace the with an instruction to insert a child template instead. That instruction is called "" and remember that you can always get a list of template elements by clicking the little blue help icon in the toolbar. Remember to click the "Save" icon and then your final markup should look like this:

Using the Master Template

The next step is to modify our "Home Page" template so it uses our Master Template. It's very simple, all you need to do is to choose the Master Template from the "Master Template" dropdown. Once that's done, we can remove all the markup from the "Home Page" template which now is stored in our Master Template. So edit the "Home Page" template, choose "Master" as the Master Template and modify the template markup so it looks like this (and remember to save):

As you can see our Home Page template is much cleaner and because we can repeat this technique on all the other templates we'll use on our site, future changes in the central markup will be very easy to maintain, as it's stored in one single template instead of being spread across all.

0 comments:

:a: :b: :c: :d: :e: :f: :g: :h: :i: :j: :k: :l: :m: :n:

Post a Comment