Understanding Template Expressions


Template parameters and template expressions go hand-in-hand. Whenever an editable attribute is added to a page, both a template parameter like this:

 <!-- TemplateParam name="embargoID" type="text" value="" --> 

and a template expression like the attribute value in this code are needed:

 <h2 > 

Not all template expressions require a corresponding template parameter, however. Later in this chapter, you'll set up a template expression within a repeating region; the expression changes according to the current rowand never references a template parameter.

It's important that you have a clear understanding of the difference between a template parameter and a template expression. Earlier in this chapter, a template parameter was described as a page-restricted variable. Similarly, a template expression can be thought of as a page-based calculation. Template parameters require the manual changing of a variable (through the Template Properties dialog box); in contrast, template expressions allow changes to the Web page to occur in an automated fashion.

What kinds of calculations are possible in template expressions? Dreamweaver includes its own template-based programming language, which is somewhat similar to JavaScript. Like JavaScript expressions, template expressions can be evaluated to concatenate strings or calculate numbers. A wide range of operators are available for template expressions, including all those in the following table:

Template Expression Operators

Operator

Description

+

Add

Subtract

X

Multiply

/

Divide

%

Modulus (calculates remainders)

<

Less than

<=

Less than or equal to

>

Greater than

>=

Greater than or equal to

==

Equal to (comparison)

!=

Not equal to (comparison)

=

Equal (assignment)

&&

Boolean AND

||

Boolean OR

?

Conditional


Let's look at some common examples of template expressions. Assume that you have defined a template parameter named pageName with the value of tutorial. A basic template expression could be used in template code like this:

 <p>Current Page: @@(pageName)@@</p> 

When Dreamweaver displays or writes out the same code in a child page, you see this:

 <p>Current Page: tutorial</p> 

If you reference a defined template parameter in a template expression, you get the value of that template parameter.

You can also use template expressions to create a new filename using string concatenation, like this:

 src="/books/2/712/1/html/2/@@(pageName + '2' + '.htm')@@" 

This code results in an evaluated attribute of src="/books/2/712/1/html/2/tutorial2.htm". Take this idea a step further and create another template parameter, pageNumber, with a value of 2, and this template expression becomes possible:

 src="/books/2/712/1/html/2/@@(pageName + pageNumber + '.htm')@@" 

This template expression result is the same as before (src="/books/2/712/1/html/2/tutorial2.htm"), but it opens the door to creating a number-based filename by adding or subtracting a number:

 src="/books/2/712/1/html/2/@@(pageName + (pageNumber + 1) + '.htm')@@" src="/books/2/712/1/html/2/@@(pageName + (pageNumber - 1) + '.htm')@@" 

These two expressions would give you two different filenames: tutorial3.htm and tutorial1.htm. As you'll see later in this chapter, this type of template expression has a direct application for building linear navigation systems.

Another operator often used in template expressions is the conditional operator. The conditional operatoralso known as the ternary operatorevaluates an initial statement; if the statement is true, one value is returned; if it is false, another value is returned. For example, the following template expression (which uses the question mark and colon conditional operators) would set the src attribute to tutorialLogo.gif if the value of the template parameter pageName were tutorial. If the pageName value were anything else, the src would be set to mainLogo.gif.

 src="/books/2/712/1/html/2/@@(pageName=='tutorial' ? tutorialLogo.gif : mainLogo.gif)@@" 

Note

The same templating engine found in Dreamweaver is embedded in Contribute. Any template expression created in Dreamweaver works as expected in Contribute.


So far, you've seen template expressions written in the parenthetical shorthand format; that is, @@(pageName)@@. The same expression can also be written in HTML comment format:

 <!-- TemplateExpr expr="pageName"--> 

Although the two formats appear widely different, they are interchangeable. Personally, I prefer to use the parenthetical format for two reasons. First, it's much shorter and easier to insert within other code. Second, it has the advantage of being displayed in Design view with its own symbol, a small block within a pair of @@ signs. The template expression symbol is visible only at design-time when Invisible Elements are enabled.

Passing Template Expressions Through in Nested Templates

You might have noticed an extra option in Dreamweaver's Template Properties dialog box. Whenever a template parameter is selectedin addition to the field and other controls for changing that parameter's valueanother option offers to "Allow nested templates to control this." If you enable that option, the phrase "pass through" appears in parentheses as the template parameter's value. This pass through option allows you to control whether the parameter is set in the nested template or available to be altered in the child pages.

Let's look at an example. Assume that two template parameters are set up on a master template page: pageName and deptName. A child page is created from this template and then saved as a templatethe process for making a nested template. In this nested template, the Template Properties dialog box is opened and deptName is set to Maternity, whereas the pass through option is selected for pageName. For all child pages generated from this nested template, only one template parameter will be available to be changed: pageName.

The pass through option gives every Dreamweaver designer the ability to incorporate the power of template expressions in nested templates as well as standard templates.

If you're inserting template expressions directly into the code, a slightly different syntax is required to pass it through. To pass through a template expression like this:

 @@(pageName)@@ 

use this syntax:

 @@@(pageName)@@@ 

This syntax ensures that the template expression is not evaluated in the nested template, but will be in the child page created from the nested template.




Design and Deploy Websites with Macromedia Dreamweaver MX 2004 and Contribute 3(c) Training from the Source
Design and Deploy Websites with Macromedia Dreamweaver MX 2004 and Contribute 3: Training from the Source
ISBN: 032128884X
EAN: 2147483647
Year: 2006
Pages: 130
Authors: Joseph Lowery

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net