## Using Master Templates

Templating System : Developer Guide : User Guide

Master templates dramatically simplify the task of maintaining a consistent look and feel across all the pages of a site (or section of a site). This document gives a brief overview of how to implement a master template.

### Design a Content Frame

Most web pages are laid out with a central content area where the actual unique content of the page is displayed. Surrounding the content area is a frame with common elements that are consistent from page to page:

Logo | Ad Banner
Navigation/Context Bar
Section Links | CONTENT AREA
Footer

Most sites use an HTML table to delineate the content area within the frame, allowing for the inclusion of a sidebar along with a header and footer. Sites that opt for a simpler layout may only have a header above and a footer below the content area.

The master template is typically highly dynamic. Menus, context bars and other navigational controls must change depending on the section of the site the user is browsing. A "Related Links" box would have to reflect the specific contents of the page. The master template may also be personalized for registered users to include their name and access to restricted areas of the site. Special formatting preferences may also be applied for registered users.

### Write the Master Template

A master template to implement the page layout shown above would have this basic structure:

>     <html><body><table width=100% cellspacing=0 cellpadding=0 border=0>

>     <tr>
>       <td><!-- LOGO --></td>
>       <td colspan=2><!-- AD BANNER --></td>
>     </tr>

>     <tr><td colspan=3><!-- NAVIGATION/CONTEXT BAR --></td></tr>

>     <tr>
>       <td><!-- SECTION LINKS --></td>
>       <td colspan=2>
>         <!-- CONTENT -->
>         <slave>
>       </td>
>     </tr>

>     <tr><td colspan=3><!-- FOOTER --></td></tr>

>     </table></body></html>

The only special feature of this master template is the `slave` tag, which marks the location of the content area. Note that the content is inserted into the master template as a single passage of HTML or plain text. The master template should always frame the content area within a `td` tag when using a table to specify the overall layout of the page. Page layouts that do not rely on tables often use `hr` tags to demarcate the content area from the header and footer.

### Write the Page Template(s)

A page template must include a `master` tag to specify that its output should be enclosed in a master template:

>     <master src="/templates/master">

>     <!--Begin layout of page content-->
>     <h3>@title@</h3>
>     <p>by @name@</p>
>     <p><b>@byline@</b>: @text</p>
>     ...

The `master` tag may be included anywhere in the body of the page template, although usually the top of the file is the best location for it.

### Adding Dynamic Elements to the Master Template

The master template may be associated with its own Tcl script, which may set data sources to support dynamic elements outside the main content area. For example, you might wish to include the user's name on every page to indicate that the site has been personalized. The Tcl script associated with the master template would include code like this:

>     set user_name [your_procedure_to_get_the_current_user_name]

The template would have a section like this:

>     <if @user_name@ nil>
>       <a href="/register.acs">Register Now!</a>
>     </if>
>     <else>
>       @user_name@ (<a href="/signout.acs">Sign Out</a>)
>     </else>

### Passing Property Values from the Page Template to Master Template

As mentioned above, in many cases the dynamic elements of the master template depend on whatever is appearing in the content area for a particular request. The `property` tag may be used in the page template to specify values that should be passed to the master template:

>     <master src="/templates/master">
>     <property name="title">@title@</property>

>     <!--Begin layout of page content-->
>     ...

In this case, the `property` tag establishes `title` as a data source for the master template. Properties are set as regular Tcl variables prior to executing the Tcl script associated with the master template. This allows the page template to pass an ID which the Tcl script associated with the master template may use to query for additional information.

templating@arsdigita.com