Graphics
 

Dreamweaver CS5 : Establishing Optional Regions (part 2) - Evaluating template expressions

12/6/2011 5:17:28 PM

3. Evaluating template expressions

With optional regions, template expressions are either set explicitly or evaluated to true or false. Template expressions can also be used throughout the template to great effect. Here is a short list of what's possible with template expressions:

  • Alternate the background color of a row contained in a repeating region.

  • Automatically number each row in a repeating region.

  • List the total number of rows in a repeating region.

  • Show an optional region if a certain number of rows are used, or another region if that number of rows is exceeded.

  • Create sequential navigation links, allowing users to page to the next — or previous — document in a series.

  • Compute values displayed in a table, displaying items such as basic cost, tax, shipping, and total.

  • Display particular content depending on the position of the row — first, second, second-to-last, or last, for example — in the repeating region.

Two types of template expressions exist: template expression statements and inline template expressions. Template expression statements take the form of a specialized HTML comment, like this:

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

Template expression statements are coded by hand. Inline template expressions are surrounded by parentheses and double @ signs, like this:

@@(fileExt)@@

Inline template expressions can only be entered by hand, but they are very flexible. You can insert an inline template expression as an attribute into any of Dreamweaver's text field interfaces, such as the Link field of the text Property inspector or the Bg (Background Color) field of the row Property inspector. Template expressions not entered as attributes appear as Invisible Elements with a double @ sign symbol, as shown in Figure 3. Template expression statements appear with a script icon.

Although you can enter an inline template expression in Code view without a problem, you cannot enter one on the page in Design view.


3.1. Template expression language and object model

Template expressions are written in their own language, which uses a subset of JavaScript operators and its own object model. The syntax of template expressions closely resembles that of JavaScript, and both use a similar dot notation to refer to the properties of a specific object. Similar to JavaScript, Dreamweaver template expressions also have their own object model, although the object model for template expressions is much more limited in scope.

The elements supported by Dreamweaver template expressions are detailed in Table 1.

Figure 3. Template expressions can either be entered as statements or inline code.

Table 1. Template Expression Features and Operators
LiteralsSyntaxExample
Numeric LiteralDouble-quoted numbers"123"
String LiteralDouble-quoted string"Chapter"
Boolean Literalstrue/falsetrue
String Concatenationstring1 + string2"Number of rows: " + _numRows
Ternary Operator  
Conditionalcondition ? resultA : resultB(_index & 1) ? #FFFFFF : #CCCCCC
Logical Operators  
Logical NOT!operand!mainLogoRegion
Logical ANDoperand1 && operand2onSale && nowFeatured
Logical ORoperand1 || operand2onSale || nowFeatured
Arithmetic Operators  
Additionoperand1 + operand2_numRows + 1
Subtractionoperand1 – operand2_index – 1
Multiplicationoperand1 * operand2basePrice * taxBase
Divisionoperand1 / operand2numSold / quantityShown
Modulooperand1 % operand2_index % 2
Comparison Operators  
Less Thanoperand1 < operand2inStock < numSold
Greater Thanoperand1 > operand2numSold > numShipped
Less Than or Equaloperand1 <= operand2_index <= _numRows
Greater Than or Equaloperand1 >=operand2_numRows >= pageLimit
Equaloperand1 == operand2_index == 10
Not Equaloperand1 != operand2_numRows != 1
Bitwise Operators  
Bitwise NOT~erand~4
Bitwise ANDoperand1 & operand2_index & 1
Bitwise ORoperand1 | operand24 | 8
Bitwise XORoperand1 ^ operand22 ^ 4
Bitwise Signed Right Shiftoperand1 >>8 >> 1
Bitwise Left Shiftoperand1 <<1 << 0

The template expressions document model is made up of two primary objects: _document and _repeat. The document object contains all the template variables found on the page. For example, if you create an optional region with the name altImageRegion, you can refer to it in a document expression with the following statement:

<!-- TemplateBeginIf cond="_document.altImageRegion"-->

However, the _document prefix is implicit, and the same statement can be written like this:

<!-- TemplateBeginIf cond="altImageRegion"-->

As you may suspect, the _repeat object refers to a repeating region. The _repeat object has a number of very useful properties, as shown in Table 2.

Table 2. _repeat Object Properties
PropertyDescription
_indexReturns the index number of the current entry. The _index property is zero-based, so for the first entry of a repeating region, _index equals zero.
_numRowsReturns the total number of entries in a repeating region.
_isFirstReturns True if the current entry is the first entry of a repeating region, False otherwise.
_isLastReturns True if the current entry is the last entry of a repeating region, False otherwise.
_prevRecordReturns the _repeat object for the entry before the current entry. For example, if _index = 2, then _prevRecord._index = 1. If _prevRecord is used in the first entry, an error occurs.
_nextRecordReturns the _repeat object for the entry after the current entry. For example, if _index = 2, then _nextRecord._index = 3.
_parentReturns the _repeat object for a repeating region enclosing the current repeating region. For example, use _parent._numRows to find the total number of rows of the outer repeating region.

The _repeat object is also implicit, and it is not necessary to reference it specifically in a template expression.

3.2. Multiple-if template expressions

Certain template expressions cannot be handled by referencing a single condition — "If A is true, show B" does not cover every possible circumstance. What if you wanted to test against multiple conditions and provide multiple results? Can Dreamweaver handle something like "If A is true, show B; but if C is true, show D — and if neither of them is true, show E"? With the help of multiple-if expressions, you bet it can.

With a multiple-if template expression, you can test for any number of conditions and act accordingly. Multiple-if expressions use two different template expressions: one to close the entire expression and another one for each separate case. Here is an example:

<!-- TemplateBeginMultipleIf-->
<!-- checks value of template parameter SKU and shows the desired image-->
<!-- TemplateBeginIfClause cond = "SKU == 101">
<img src = "/images/ring101.tif" width="125" height="125">
<!-- TemplateEndIfClause-->

<!-- TemplateBeginIfClause cond = "SKU == 102">
<img src = "/images/bracelet102.gif" width="125" height="125">
<!-- TemplateEndIfClause-->

<!-- TemplateBeginIfClause cond = "SKU == 103">
<img src = "/images/necklace103.gif" width="125" height="125">
<!-- TemplateEndIfClause-->

//default display if none of the other conditions are met
<!-- TemplateBeginIfClause cond = "SKU != 103">
<img src = "/images/spacer.gif" width="125" height="125">
<!-- TemplateEndIfClause-->
<!-- TemplateEndMultipleIf-->

In this code, if none of the conditions are met, a blank spacer image is displayed. As with other template expressions, multiple-if expressions must be coded by hand.

 
Others
 
- Dreamweaver CS5 : Establishing Optional Regions (part 1)
- Adobe Photoshop CS5 : The Fastest Way to Resize Brushes Ever (Plus, You Can Change Their Hardness, Too)
- Adobe Photoshop CS5 : Fixing Dark Eye Sockets
- Adobe After Effects CS5 : Creating a Basic Animation Using Effects and Presets - Creating a new composition
- Adobe After Effects CS5 : Importing footage using Adobe Bridge
- Adobe Fireworks CS5 : Repairing areas with the Rubber Stamp tool
- CorelDraw 10 : Hyperlinks and Bookmarks (part 2) - Creating bookmarks and links
- CorelDraw 10 : Hyperlinks and Bookmarks (part 1)
- Adobe Flash Professional CS5 : Free Transform Commands and Options
- Adobe Flash Professional CS5 : Applying Modify Shape Menu Commands
- Adobe InDesign CS5 : Erasing to Reshape Paths & Working with Pathfinder
- Adobe InDesign CS5 : Using the Pencil Tool
- Adobe Flash Catalyst CS5 : Importing Your Project into Flash Builder - Beginning the Import Process
- Adobe Flash Catalyst CS5 : Designing with Data - Using a wireframe data list
- Adobe Illustrator CS5 : Understanding the Layers Panel & Setting Layers Panel Options
- Adobe Illustrator CS5 : Understanding Overprints
- Dreamweaver CS5 : Using Dreamweaver Templates - Enabling Repeating Regions
- Dreamweaver CS5 : Using Dreamweaver Templates - Making Attributes Editable
- Adobe Photoshop CS5 : Fixing Group Shots the Easy Way
- Adobe Photoshop CS5 : Fixing Reflections in Glasses
 
 
Most View
 
- Sharepoint 2013 : Building an Application with Access Services (part 5) - Modifying Application Views, Creating a Query
- Windows 8 : Working with file systems (part 2) - Inheritance and cumulative effectiveness
- Microsoft PowerPoint 2010 : Coordinating Multiple Animations (part 2) - Set Time Between Animations,Modify an Animation
- SQL Server 2012 : Configuration Options (part 7) - Advanced Server-Configuration Properties
- Microsoft Visio 2010 : Aligning and Arranging Shapes (part 6) - Using the Grid, Ruler, and Guides
- Microsoft Lync Server 2013 : Installing the Director Role (part 1) - Prerequisites
- Sharepoint 2013 : Security and Policy - Permissions and Permission Levels (part 2) - Creating Custom Permission Levels
- Securing an Exchange Server 2007 Environment : Securing Outlook 2007 (part 2) - Encrypting Communications Between Outlook and Exchange , Blocking Attachments
- Sharepoint 2013 : Managing and Configuring Profile Synchronization (part 5) - SharePoint Profile Synchronization - Editing Connection Filters
- Microsoft Visio 2010 : Exporting Visio Graphics to Other Formats
 
 
Top 10
 
- Microsoft LynServer 2013 : Dependent Services and SQL - Office Web Apps Server
- Microsoft LynServer 2013 : Dependent Services and SQL - Network Dependencies (part 2) - Defining Network Sites
- Microsoft LynServer 2013 : Dependent Services and SQL - Network Dependencies (part 1) - Supporting Lync Phone Edition with DHCP
- SQL Server 2012 : Performance Monitor Overview (part 5) - Remotely Running PerfMon
- SQL Server 2012 : Performance Monitor Overview (part 4) - Working with Data Collector Sets
- SQL Server 2012 : Performance Monitor Overview (part 3) - Getting Started with PerfMon - Monitoring Real-Time Server Activity, Starting Out with Data Collector Sets
- SQL Server 2012 : Performance Monitor Overview (part 2) - New PerfMon Counters for SQL Server 2012
- SQL Server 2012 : Performance Monitor Overview (part 1) - Reliability and Performance Monitor
- SQL Server 2012 : Knowing Tempdb - Configuration Best Practices (part 2) - Tempdb Initial Sizing and Autogrowth , Configuring Multiple Tempdb Data Files
- SQL Server 2012 : Knowing Tempdb - Configuration Best Practices (part 1) - Tempdb File Placement