|[ LiB ]|
A Dreamweaver MX 2004 template fits within the ordinary definition of the word template in that it is a page that serves as a pattern for others like itself. Even those who don't use Dreamweaver templates might create a template page and just resave it with a new name for each page of that type that is needed. However, Dreamweaver templates take the concept further, adding a measure of power and ease: The pages made from a template can be updated automatically, all at once, just by updating the template itself.
Therefore, a Dreamweaver template is an HTML page, but a special type of HTML page. Special code inserted by Dreamweaver makes it possible for the template and its child pages (the HTML pages made from the parent template) to be connected in such a way that an update to the template updates all child pages.
The basic procedure for working with templates is simple: Create a template page, including editable and noneditable regions ; use the template to create one or more child pages, or apply the template to existing pages; update the template, and let Dreamweaver automatically update all child pages.
A Dreamweaver template is an HTML document that has the .dwt filename extension and that lives in a folder called Templates in a site's Local Root folder. The file's name, minus its extension, is its template name. You can view and manage the templates for a site with the Templates category of the Assets panel, shown in Figure 20.1. Each template must belong to a particular site and can be used only from within that site.
Don't move or rename the Templates folder! And don't move any template files out of this folder, or Dreamweaver won't recognize them as templates anymore!
In addition to their HTML content, templates contain special markup embedded in HTML comments. This code is in Dreamweaver's template language and looks something like this:
<!-- TemplateBeginEditable name="doctitle" -->
There are several ways to create new templates, all of them equally effective. To create a new template, do one of the following:
In the Assets panel (select Window > Assets), choose the Templates category and click the New Template button at the bottom of the panel (see Figure 20.1). This adds a new .dwt file to your Templates folder and gives you the chance to rename it. To edit the template, select it in the panel and click the Edit Template button.
Choose File > New and, in the New Document dialog box, choose Template > HTML Template from the category lists (see Figure 20.2). This creates and opens a new, empty file. The first time you save the document, instead of displaying a standard Save dialog box, Dreamweaver asks you what template name the file should have and what site it should belong to. The template is automatically stored in the Templates folder for that site with the .dwt filename extension.
You can also turn any HTML document into a template by opening it and choosing File > Save As Template, or by going to the Common category of the Insert bar and choosing Make Template from the template objects (see Figure 20.3). Dreamweaver asks you to name the template and saves it in the Templates folder as a .dwt file.
When you build child pages from a template, all parts of the template are locked except those that you (or Dreamweaver) have specifically defined as editable. You generally want to leave things such as the title banner, navigation bar, and footer locked so that they remain the same on all child pages. You'll probably want to unlock the content area by declaring it to be an editable region so that each child page can have its own unique content. Dreamweaver automatically makes the page title an editable region.
To see how editable regions are defined, open any template page, go to Code view, and find the page title area. You'll see this:
<!-- TemplateBeginEditable name="doctitle" --> <title>Untitled Document</title> <!-- TemplateEndEditable -->
The opening and closing lines surround the editable area. The name of this editable region is doctitle .
Any region in a template can be made editable by surrounding it with this code and assigning it a unique name. To define a region as editable, do one of the following:
Put the cursor inside a table cell , empty paragraph, layer, or other region. From the template objects in the Insert bar, choose the Editable Region object. When Dreamweaver prompts you, give the region a unique name. You can get the same result by right-clicking inside a region and choosing New Editable Region from the context menu.
Select the tag (such as td or table ) from the Tag Selector or in Design view and choose Insert > Template Objects > Editable Region.
If you prefer to do things manually, go to Code view, find the region you want to define as editable, and surround it with the code listed earlier. To create an editable region within a heading, for instance, you type something like this:
<h1><!-- TemplateBeginEditable name="main heading" --> <!-- TemplateEndEditable --></h1>
Line breaks aren't crucial to the effect, so break lines wherever you like.
After you've created the region, it appears in Design view with a colored label. It appears in the Tag Selector as <mmtemplate:editable> . You can select the region for editing by clicking the label or the Tag Selector entry. The Property inspector then lets you change its name (the only property of editable regions).
Note that it's entirely legal to assign a block-level text element, such as a p , li , or h1 tag, to be an editable region. If you do so, every time you save the template, Dreamweaver warns youas if you've done something stupid. You can safely ignore these warnings.
When you define the template, you can put any content you like inside the editable regionsmaybe a helpful bit of text such as [title goes here]. Because this text is inside an editable region, you can overwrite it in any child page. Dummy text like this can help you visualize the design of the finished pages as you're working.
After you've defined one or more templates, you can create new pages based on those templates or apply the templates to existing pages.
To create a new document based on a template, choose File > New. In the New Document dialog box, bring the Templates tab to the front. Choose the site you're working on, and the template you want to base your page on, and click OK to create the child page (see Figure 20.4).
To apply a template to an existing document, open the document. From the Templates category of the Assets panel, select the template you want to apply. Either click the Apply button or drag the template to the Document window. If the page has any content, Dreamweaver needs to either put that content into editable regions in the template or throw the content away. You see a dialog box indicating what content exists in the current document body and document head (see Figure 20.5). As long as any regions are set to <Not Resolved>, you won't be allowed to continue. You must select each region and, in the Move Content to New Region drop-down, choose one of the following:
Nowhere to delete the region's content
Head for any existing document head content (keywords, scripts, style sheets, and so on)
One of the named editable regions to put the content in that region
After you've created your child page, you can save it as normal (select File > Save) to any folder in your site. You can add or edit content only in the editable regions. While you're in Design view, the Tag Selector displays only tags within editable regions. Each editable region itself is marked as <mminstance:editable> . If you examine the document in Code view, you'll see that all noneditable code (code outside the editable regions) appears in gray. An HTML comment at the top of the page, and another at the bottom, indicate that this page is a template instance , or a child page based on a particular template:
<!-- InstanceBegin template="/Templates/main.dwt" codeOutsideHTMLIsLocked="false" --> <html> etc </html> <!-- InstanceEnd -->
Figure 20.6 shows a typical child page being edited.
Within the document's head section are two editable regions: the head region for head content such as meta tags, and the doctitle region for the page title. In Code view, they look like this (shown in bold):
<head> <!-- InstanceBeginEditable name="doctitle" --> <title>My Web Page</title> <!-- InstanceEndEditable --> <meta http-equiv="Content-Type content="text/html; charset= iso-8859-1"> <!-- InstanceBeginEditable name="head" --> <script src="myscripts.js"></script> <!-- InstanceEndEditable --> </head>
If you're in Design view, use the View Options submenu to display head content. You see a blue box surrounding the page title, as shown in Figure 20.7. That's the doctitle region. To add other head content, such as keywords or other meta tags, you have two choices:
Click anywhere inside the doctitle box in the Head Content bar, and use the Insert bar or menu to add head objects as normal. This puts the code for your meta tags and so forth inside the head tag in the HTML (so they'll work fine in the browser), but within the doctitle editable region. They don't belong there.
If you want your head content inserted inside the head editable region (which is where it logically belongs!), you have to be a bit sneakier because the head editable region doesn't appear in the Head Content bar until you place some content there. Switch to Code view and place the insertion point between the opening and closing head region tags:
<!-- InstanceBeginEditable name="head" --> <!-- InstanceEndEditable -->
Then use the Insert bar or menu to insert a head object. After you do this, the Head Content bar in the Document window shows a second blue box, representing the head region, as shown in Figure 20.7. From now on, you can place the cursor in that box and insert more head content.
A template isn't a straitjacket. You can open any child page in an editor other than Dreamweaver and change the locked code if you want to. But the next time the original template is updated, Dreamweaver overwrites your changes. If you want to divorce the child from its template parent, you can open the file in another program and manually delete the template comments, or you can open the file in Dreamweaver and choose Modify > Templates > Detach From Template. All locked regions are then editable, but the page no longer updates when the parent template updates.
The glory of Dreamweaver templates is that they're updatable. As soon as you've created a template, you can open the template file and edit any content in the page's locked regions. Your changes are immediately applied to all child pages. To open a template for editing, you can select it in the Templates category of the Assets panel and click the Edit Template button (see Figure 20.1), or find it in the Templates folder in the Site panel and double-click to open it (as you would any file). When the template is open, you can make any changes you like to the areas that are not editable regions. You can also add, rename, or delete editable regions. Every time you save the template, Dreamweaver offers to update all child pages. If you've renamed or deleted any regions, and the template has child pages, Dreamweaver asks how content in those child pages should be disposed of. You can choose to reassign the child pages' content to a different editable region or to delete the content.
If you have more than a few child pages based on your template, updating might take a few moments. But you don't have to update every time you save. If you are in the (good!) habit of saving often, you can save time by updating only once, at the end of your editing session.
When it's time to upload your site files to a web server, remember that you don't have to upload the template files themselves (the .dwt files in the Templates folder)just the child pages. Two uploading habits can be helpful when working with templates:
Use the Synchronize command (Site > Synchronize) to determine which files need uploading after editing. This guarantees that, after you've edited a template, all updated child pages get uploaded.
Enable cloaking for your site in the Cloaking category of the Site Definition dialog box. Cloak the Templates folder by right-clicking it in the Site panel and choosing Cloaking > Cloak . Dreamweaver warns you that this doesn't work for batch site operations, but it stops the folder from being uploaded during synchronization.
Editable regions and automatic updates are just the beginning of what you can do with Dreamweaver templates. Thanks to the template language (those special HTML comments in templates and child pages), you can use templates to generate complex websites that include optional content, editable tag attributes, repeating regions, nested templates, and more!
An optional region is a piece of document content that exists in the template. You can choose to hide or show it independently in each child page. Optional regions are great if, for instance, some of the pages based on your template are long enough to require a Back to Top button and others aren't, if some pages require Next or Previous links and others don't, and so forth.
To define an area of your template as optional, open the template file and do the following:
Check your document in Code view after doing this, and you'll see that Dreamweaver has added two sets of template instructions. In the head section, a template parameter is defined, named whatever you called your optional region, with a type of boolean and a value of true or false . Here's an example:
<!-- TemplateParam name="back to top" type="boolean" value="true" -->
Your optional content is surrounded by more template code, like this:
<!-- TemplateBeginIf cond="_document['back to top']" --> <a href="#top">Back to top</a> <!-- TemplateEndIf -->
This conditional statement determines whether the content should be included in the child page, based on the value of the parameter defined in the head.
In Design view, the optional region is marked with a colored label. In the Tag Selector, it's indicated as <mmtemplate:if> . If you select the region, the Property inspector lets you change its name. Or you can click the Edit button to reopen the Optional Region dialog box and change its default settings.
As with all template code, you are free to move these optional region delimiters if you discover they don't surround exactly the right content.
As soon as your template defines an optional region, you can determine on a page-by-page basis whether the content shows. To do this, open the child page and choose Modify > Template Properties. The dialog box that appears lists any optional regions, along with a true/false value, as shown in Figure 20.8. Select the region name and select or deselect the Show [region name] check box to show or hide the region on this page. If you choose to hide the optional region, and then you check your document in Code view, you'll see that the code for this region is gone! If you choose to show it, and then check Code view, the code is back!
By default, optional regions aren't editable. They contain locked content (such as Back to Top or Next/Previous links). You can, however, create an optional region that contains an editable region.
To insert an optional/editable region, open your template file, go to the Template objects in the Insert bar, and choose Editable Optional Region. If you already have an optional region, and you want to make it editable, just put the insertion point inside the optional region and choose the Editable Region object from the Insert bar. No matter how you do it, Dreamweaver puts an editable region inside an optional region. In Design view, it appears as two colored labels nested one inside the other, or two tags in the Tag Selector. In Code view, it appears like this:
<!-- TemplateBeginIf cond="_document[' myRegionName ']" --> <!-- TemplateBeginEditable name="EditRegion4" --> [content goes here] <!-- TemplateEndEditable --> <!-- TemplateEndIf --></td>
When editing your child pages, you can show or hide the specified content (indicated here by [content goes here] ). If it's showing, you can change those contents.
Editable attributes in templates let you specify any attribute to any tag in the template's locked regions. They also allow this attribute to be controlled by the child page. Sound esoteric? It's actually amazingly useful! With editable attributes, you can insert a consistently placed and sized logo in a locked area of the page while allowing child pages to assign a different src attribute (so that a different logo can appear in different pages). You can include consistent Next and Previous links on every page while allowing child pages to control where the links go by making the href attribute editable. And you can do a lot more. Your imagination is the only limit.
To specify an attribute as editable in a template, open the template and do the following:
After you've assigned the editable attribute, if you select the tag and examine the Property inspector, or go to Code view and examine the tag's HTML, you see that Dreamweaver has inserted a template parameter in place of your attribute. Here's an example:
<img name="logo" src="@@(logo_src)@@" width="112" height="30" />
If you go to Code view and look at the end of the head section of your document, where Dreamweaver defines template parameters, you'll find an additional line of code that looks something like this:
<!-- TemplateParam name="logo_src" type="URL" value="../images/jewelry_logo.gif" -->
What's happening here? The code in the head section defines the logo_src parameter, with a type of URL , and assigns it a default value. The code in your tag tells Dreamweaver to insert that parameter in place of a specific attribute.
Dreamweaver doesn't interpret the parameter while you're viewing the template in Design view. So if you've used a parameter to specify an image source, for instance, you see a broken image icon as you work. When you get to the child page, however, Dreamweaver substitutes a real value for the parameter, and the page displays properly.
To edit the attribute in a child page, open the child page and choose Modify > Template Properties. The dialog box shown in Figure 20.9 appears. All editable attributes and their default values are listed. Select the attribute you want to change, enter a new value for it, and click OK to close the dialog box. Note that Dreamweaver gives more help for some parameter types than others. If your parameter type is color, the Template Parameters dialog box includes a color picker. But for URL-type parameters, you must enter the relative or absolute URL manuallythere's no Browse button. (Note also that relative URLs must be relative to the template file, not the child page.)
If you check a child page in Code view after setting your editable attributes, you'll see that the tag attribute now has a standard value, in place of the template parameter. In the document's head section, however, the template parameter definition is still present.
With repeating regions, you can define a chunk of page content that appears one or more times in any given child page. For instance, you can create a one-row table in the template. In one child page you could repeat that row to create a five-row table, and in another child page you could repeat the row to create a two-row table. Or your template could define one subheading and text paragraph as a repeating region so that some child pages could have several subheading/text units and others would have only one. Repeating regions are most useful when they contain editable regions so that each repeated table row or subheading can have its own unique content.
To define one or more editable regions as a repeating region, open a template file and do the following:
If you examine your document in Code view at this point, you'll see that Dreamweaver has added template code to indicate the beginning and end of the repeating region, as well as code for each editable region. Your code might look something like this:
<ul> <!-- TemplateBeginRepeat name=" gadget-list " --> <li><!-- TemplateBeginEditable name=" gadget-item " --> anything goes here <!-- TemplateEndEditable --></li> <!-- TemplateEndRepeat --> </ul>
This example defines a repeating list item. You wouldn't want the repeating region code defined outside the ul tags, or you'd create multiple lists in the child pages instead of multiple list items in one list.
Using repeating regions in child pages is easy and fun. When you open a child page whose parent template contains a repeating region, and view it in Design view, you see a colored Repeat label with the name of the region, followed by a little set of control icons for adding, removing, and rearranging repeating entries (see Figure 20.10). To work with the repeating region, do the following:
The code for the repeating region in the child page includes indications of where each iteration begins and ends:
<ul> <!-- InstanceBeginRepeat name=" gadget-list " --> <!-- InstanceBeginRepeatEntry --> <li><!-- InstanceBeginEditable name=" gadget-item " --> Gadget <!-- InstanceEndEditable --></li> <!-- InstanceEndRepeatEntry --> <!-- InstanceBeginRepeatEntry --> <li><!-- InstanceBeginEditable name=" gadget-item " --> Doodad <!-- InstanceEndEditable --></li> <!-- InstanceEndRepeatEntry --> <!-- InstanceEndRepeat --> </ul>
Probably the most common use of repeating regions in web pages is to build a table in which one or more rows repeats and every cell contains an editable region. Price lists, company directories, and a whole host of other layout tasks work very well with this kind of repeating table structure. And guess what? Dreamweaver provides you with a special object just for creating this structure.
To create a repeating table, open a template document and follow these steps:
In the child pages based on this template, you use the repeating table exactly like you would use any repeating region.
Nested templates let you define templates based on other templates, and then create child pages based on those templates. What?! It might sound daunting, but if you're willing to put a little extra thought into it, nested templates can make even extensive and complex websites easier to create and maintain. For instance, suppose you have an overall layout that you want all the pages in your site to have. That's your main template. But you also want some pages in your hierarchysecond- or third-level pages, perhapsto have additional page elements. Or you want an overall layout for all pages, but pages in different sections of your website should have additional requirements. You can define one main template for the entire site and then additional templates for each section, or for each level of the hierarchy, that are based on the main template.
To create a nested template, do the following:
Congratulations! You have created a template based on a template. But the fun doesn't really begin until you learn how to handle editable regions, attributes, and other template features across your multiple generations of templates.
You don't have to stop with one generation of nesting. It's possible to nest templates inside nested templates, as many generations deep as you like.
When you nest templates, editable regions always begin in the parent template. As shown in Figure 20.12, the parent template defines one or more editable regions. The nested template can then lock part of any of these regions, so the child page retains only part of it as editable. If the nested template does not specify which portion of the parent's editable region should be locked, the entire region is passed to the child page as editable.
When building the main template, you must keep in mind that you'll be defining editable regions for two purposes: some to be inherited by the nested template and locked there, and some to be passed down through the nested template to the child pages.
When building the nested template, keep in mind that if you do nothing to one of the parent template's editable regions, it is passed to the child page as an editable region. To lock part of one of the parent template's editable regions, define another editable region within it. For instance, a layout table in a parent template might have a row defined as an editable region; in the nested template, one of the cells in that row can be defined as an editable region, which automatically locks all other cells in the row. Figure 20.13 shows this happening in Design view. The code for the editable row looks like this:
<!-- InstanceBeginEditable name="edit-row" --> <tr> <td>this content will be locked in child pages</td> <td><!-- TemplateBeginEditable name="edit-cell" -->this content will be editable in child pages <!-- TemplateEndEditable --></td> </tr> <!-- InstanceEndEditable -->
The edit-row region is defined in the parent template; the edit-cell region is defined in the nested template.
Dreamweaver uses color-coded highlights to keep track of which editable regions have been defined where. By default, the labels for all editable regions defined in a parent template are aqua (#66CCCC); labels for editable regions defined in a nested template are gold (#FFCC33). If you don't like these colors, you can change them in the Preferences/Highlighting dialog box (select Edit > Preferences).
What if you want a child template to lock an entire editable region from the parent template instead of locking part of it by making another part editable? You can't do this, but you can create an empty editable region in the child template to accomplish your goal. In the nested template, place the insertion point anywhere in the parent's editable region, and create a new editable region. Then delete all contents from this editable region.
Optional regions, editable attributes, and any other template parameter that appears in the Template Properties dialog box can also be used with nested templates. As with editable regions, any template parameter defined in a parent template can be passed to nested templates to control, or can be passed directly to the child pages. Parameters defined in parent templates for control by child pages are called pass-through parameters , because they pass through the nested template.
To set up pass-through parameters in your nested template setup, do the following:
After you've defined the pass-through parameter, each child page you create based on the nested template can set the parameter independent of all other pages. Just open the child page and choose Modify > Template Properties. Select the parameter, and set its value.
This exercise builds and formats a small website using basic and advanced template features. Before you start, download the chapter_20 folder from the book's website at www.peachpit.com to your hard drive. Define a site called Chapter 20, with this folder as the local root folder.
Start by opening the layout.html file and examining its contents. This file holds the layout you'll use for the site, including a layout table and navigation banner, and a link to a CSS style sheet (see Figure 20.15).
Make sure you're dragging the main template from the Assets panel and not the Site panel. If you drag from the Site panel to the Document window, it just opens the main.dwt file. If you drag from the Assets panel, it applies the template to the open document.
Templates and regions are defined with opening and closing tags that look like this (as defined in the template):
<!-- TemplateBeginEditable name="doctitle" --> <!-- TemplateEndEditable -->
or this (as used in a child page):
<!-- InstanceBeginEditable name="doctitle" --> <!-- InstanceEndEditable -->
These tags take a little more typing than your average HTML tag, but they follow the basic HTML syntax of <tag attribute="value"> , and they can contain simple scripting constructs such as conditionals and parameters. Table 20.1 lists the main tags used in defining template-based pages and their editable regions.
<!--InstanceBegin template="/Templates/ techsupport.dwt" codeOutsideHTMLIsLocked= "false" --> <!-- InstanceEnd -->
Surrounds the html element for a child page based on a template.
template : The root-relative URL of the DWT file this page is based on codeOutsideHTMLIsLocked : True or false, determining whether server-side code added outside the html tag is controlled by the template ( true ) or the child page ( false )
<!-- TemplateBeginEditable name="myStuff" --> <!-- TemplateEndEditable -->
Surrounds an editable region (in the template).
name: The name assigned to the region. Should be unique and easily identifiable.
<!-- InstanceBeginEditable name="myStuff" --> <!-- InstanceEndEditable -->
Surrounds an editable region (in the child page).
Template parameters work like variables in a scripting language. They're declared in the document head and can be used anywhere in the document. Dreamweaver uses them to set optional regions, editable attributes, and other template constructs, but you can use them for much more than this if you know the rules.
Parameters are defined by a statement in the template head, like this:
<!-- TemplateParam name="myParameter" type="color" value="red" -->
In the template's child page, the definition statement changes to look like this:
<!-- InstanceParam name="myParameter" type="color" value="red" -->
Like variables in a scripting language, template parameters can be of various data types, for storing different kinds of information. These types are Boolean (true/false), text, number, color, and URL. The parameter's name is how it can be referred to throughout the template; its value is the information stored in it. The general idea is to define a parameter in the template document and allow each child page to assign its own unique value.
After a parameter has been defined, it can be called on anywhere in the document by using an expression, like this:
It works like this: Anywhere in the template document that the expression is present, the child page substitutes the parameter's value. So if the template page includes the following expression in its code:
a child page has code like this in its place:
Whatever color is specified in the child page's parameter definition is used here.
For this expression syntax to work properly, your parameter names must not include spaces or other special characters .
Dreamweaver automatically creates parameters and expressions every time you define an editable attribute in your template. But you can also create your own parameters, for use any way you like, using this same syntax. Imagine, for instance, that you have one template governing product display pages for several categories of products. You could define a category parameter by entering the following code in the template head:
<!-- TemplateParam name="category" type="text" value="default" -->
You could cause that category name to appear at the top of each page by entering an expression like this:
You have to enter the parameter definition by hand using Code view. But you can create the expression in Design view by just typing in @@ (category) @@ and formatting it. When Dreamweaver notices that you've entered an expression here, it displays an expression placeholder where you typed your text, as shown in Figure 20.21.
When you open a child page based on this template, you can change the parameter's value by manually changing the definition statement in Code view or by choosing Modify > Template Properties. The category parameter appears in the dialog box, ready to accept a new value.
When you're working with nested templates, and you want to define a parameter in the main template that will be controllable by child pages (and not by the nested template), do so by opening the child template and adding a passthrough attribute to the parameter definition with a value of true :
<!-- TemplateParam name="myParameter" type="text" value="default" passthrough="true" -->
When using a passthrough parameter in an expression in the child template, use this syntax:
Any time you tinker with code, there's the possibility that things will go wrong (typos, syntax goofs, and so on). To make sure that your code is error-free after manual editing, choose Modify > Templates > Check Template Syntax. You'll get either an error message or a clean bill of coding health.
In scripting, a conditional statement indicates that something should happen only if a certain condition is met. Conditional statements generally involve testing the value of variables or parameters. Every time you work with optional regions in your Dreamweaver templates, you're working with conditional statements. As soon as you know your way around conditional syntax in template language, you can start creating advanced optional regions.
The syntax for a conditional statement in template language looks like this:
<!-- TemplateBeginIf cond=" [true or false code] " -->
The cond attribute must have as its value an expression that evaluates to true or false. For instance, if the head section of the template defines a parameter like this:
<!-- TemplateParam name="myParameter" type="boolean" value= "true" -->
the conditional statement can test this parameter:
<!-- TemplateBeginIf cond="myParameter" -->
If the head section defines a parameter that isn't itself a Boolean, the conditional statement can use various comparison operators to determine if the optional content should display:
<!-- TemplateParam name="myParameter" type="text" value="HATS" --> <!-- TemplateBeginIf cond="myParameter=='HATS'" -->
Table 20.2 lists common comparison operators that can be used in conditional statements.
myText == 'HATS' myNumber == 3
myNumber > 3
Greater than or equal to
myNumber >= 3
myNumber < 10
Less than or equal to
myNumber <= 10
Two expressions, both of which are true
myNumber > 3 && myNumber < 10
Two expressions, at least one of which is true
myText == 'HATS' myText == 'SHOES'
As discussed earlier in the chapter, when you create a basic optional region using the Optional Region object, Dreamweaver defines a template parameter of the Boolean type and then uses a conditional statement that tests this parameter to determine whether the optional region should show:
<!-- TemplateParam name="myBoolean" type="boolean" value= "true" --> <!-- TemplateBeginIf cond="myBoolean" -->
But you can create more advanced optional regions using other kinds of parameters and conditionals:
<!-- TemplateParam name="category" type="text" value="HATS" --> <!-- TemplateBeginIf cond="category=='HATS'" -->
You'd have to work in Code view to define the parameter shown here. But you can create the optional region without working in Code view by using the advanced settings for the Optional Region object. Do it this way:
Click OK to close the dialog box.
Standard conditionals lend themselves to things that either are or aren't true (is the category equal to JEWELRY?). But sometimes, you might have a parameter that can have any of several values, and you want to display different page content for each value (if the category is JEWELRY, do this; if the category is HATS, do this; and so on). For such occasions, the template language has a special kind of conditional called a MultipleIf statement. The syntax looks like this:
<!-- TemplateBeginMultipleIf --> <!-- TemplateBeginIfClause cond=" [test 1] " --> optional content 1 <!-- TemplateEndIfClause --> <!-- TemplateBeginIfClause cond=" [test 2] " --> optional content 2 <!-- TemplateEndIfClause --> <!-- TemplateEndMultipleIf -->
For the first conditional, you might enter category=='HATS' and display a statement about hats or a picture of a hat as your optional content. For the second, you might enter category=='SHOES' and display some comparable shoe- related content. You can add as many conditional clauses as you like, each with its own set of optional content. The only downside is that the entire code must be entered in Code view because Dreamweaver doesn't provide an object for it.
condition ? true : false
For instance, if a sale is going on, you might want the prices to appear highlighted in yellow. You could create a template parameter like this:
<!-- TemplateParam name="sale" type="boolean" value="true" -->
You could create the table cell that holds each price like this:
<td bgcolor="@@(sale ? 'red' : 'white')@@">
You can enter the expression in Code view by typing the code shown here, or in Design view by entering the expression only (starting and ending with @@ ) in the appropriate input field of the Property Inspector.
When you're working with repeating regions, your expressions can take advantage of the template language's _repeat object and its properties to create some sophisticated results. Table 20.3 lists _repeat properties and the syntax for accessing them.
The numerical index of the current iteration, starting at 0
The total number of iterations in this child page
Specifies whether the current iteration is the first
Specifies whether the current iteration is the last
The _repeat object for the previous iteration (or an error if there is none)
The _repeat object for the next iteration (or an error if there is none)
The _repeat object for the enclosing repeating region (use this only when one repeating region is nested inside another)
What can you do with the _repeat object properties? By using them in expressions, you can construct conditional statements to hide or show optional content. For instance, what if you have a repeating region that consists of a subheading and paragraph, and you want to insert a horizontal rule between each subheading-paragraph combination, but no rule after the last one? To accomplish this, you put an hr tag after each repetition except the last one. You need a conditional statement that tests each time the region repeats, to see if this is the last iterationand if it is, that inserts an hr tag. The code for your repeating region would look like this:
<!-- TemplateBeginRepeat name="product_list" --> <h2> <!-- TemplateBeginEditable name="product_name" --> Product name goes here <!-- TemplateEndEditable --> </h2> <p> <!-- TemplateBeginEditable name="product_desc" --> Product description goes here <!-- TemplateEndEditable --> </p> <!-- TemplateBeginIf cond="!(_repeat._isLast)" --> <hr> <!-- TemplateEndIf --> <!-- TemplateEndRepeat -->
To create this effect in Design view, you can insert a Repeating Region object, and then, within the repeating area, insert an Optional Region object. In the Optional Region dialog box, bring the Advanced tab to the front, choose Enter expression, and type !(_repeat._isLast) in the input field. This expression uses the not operator (!) to ask if the current iteration is not the last one.
If you want your repeating entries to display numbers (like a numbered list), you can create an expression that displays the _index property:
@@(_repeat._index + 1)@@
(You add 1 to the _index each time because computer index values start from 0, and it looks a bit strange to have your numbering begin with 0.)
Finally, if you're using a table with repeating rows, you might want every other row in the table to be a different color. Enter the following code for the tr tag:
<tr bgcolor="@@(_repeat._index%2 ? 'orange' : 'white')@@">
This uses the modulo operator to test whether dividing the _index property by 2 will leave a remainder. If there is a remainder, the row is an odd-numbered row, and it is given an orange background. If not, the row is even-numbered and is given a white background.
If you've worked with data-driven web pages, using the Dreamweaver template language probably feels a lot like constructing pages in ColdFusion or one of the other application server languages. Dreamweaver template language lets you accomplish many of the same tasks as a server-side languageusing page-specific editable regions, repeating regions, optional regions for conditional content, and parameters to construct individual pages on-the-fly from a scripted originalbut without any server involvement. As you'll see in Chapter 23, "Further Dynamic Techniques," templates can also be used in conjunction with data-driven pages.
Is all this typing and memorizing syntax getting you down? If you hand-code templates frequently, it's a good idea to create snippets for the items you type most often. You can create a snippet folder specifically for template snippets and store block and wrap snippets in it. Figure 20.23 shows a few template snippets being created.
See the section "Code Snippets" in Chapter 27, "Writing Code in Dreamweaver," for more on this.
This exercise builds on the template site you started in Exercise 20.1, using optional regions and manually entered template parameters and expressions to construct a breadcrumb navigation system.
<!-- TemplateParam name="section" type="text" value= "sectiondefault" --> <!-- TemplateParam name="section_url" type="URL" value="#" --> <!-- TemplateParam name="page" type="text" value= "pagedefault" -->
<a href="../index.html">TechnoDude</a> > <!-- TemplateBeginIf cond="pagecrumb" --> <a href="@@(section_url)@@"> <!-- TemplateEndIf --> @@(section)@@ <!-- TemplateBeginIf cond="pagecrumb" --> </a> <!-- TemplateEndIf --> <!-- TemplateBeginIf cond="_document['pagecrumb']" -->> @@(page)@@ <!-- TemplateEndIf -->
|[ LiB ]|