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:
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.
|