So far, all the examples we've shown you have dealt with static data that the page author will update when creating the new page. A template can also include dynamic text. By dynamic, we mean that the template will display text from the site's database, and the page author doesn't have to do a thing. To illustrate this capability, we'll add dynamic text to the template that displays the last new book added to the sample Books database.
First, you must connect the template to the book site by defining the site. You learned how to do that in Chapters 2 and 10, so we won't repeat those instructions here. We're also working with a system data source name (DSN) named BooksConnection. (You can learn how to create a DSN in Chapter 4.) We're still working in the local system we mentioned earlier in this chapter:
It doesn't matter where your copy of the sample books database is, but we'll be working with a copy that's in the Chapter23 folder for this example.
Now, let's update the template by adding a dynamic text control that displays the last book entered into the database. That way, the page will always show the most recently donated (and available) book. Follow these steps:
Open the Application window and click the Server Behaviors tab.
Click the plus sign (+) and choose Recordset (Query) from the submenu.
Name the recordset ShowMostRecentBook, and add the following SQL to the SQL control, as shown in Figure 23.34:
SELECT Last(tblBooks.Title) AS LastOfTitle FROM tblBooks
The Connection option should default to your books connection (ours is the DSN Books- Connection). If you like, click Test to see the results of the SQL statement, and then click OK to return to the Recordset dialog box.
Figure 23.34: Create a recordset that will retrieve the dynamic text.
Click OK, and Dreamweaver MX will warn you about code lying outside the <html> section. You'll need to add the <TemplateInfo> template tag to the <head> section in order for Dreamweaver MX to update pages based on the template. Without this statement, pages won't display the dynamic text. Click OK to clear the message.
Display the template's code, if necessary, and locate the <head> section and add the following tag:
as shown in Figure 23.35.Make sure it's in the <head> section-we placed ours just before the closing </head> tag.
Figure 23.35: Add the <Template Info> tag to the <head> section.
Return to Design view and add a new line after the right-aligned text, "A Harey Tale."
Click the Application tab in the Insert bar.
Click the Dynamic Text tool to display the Dynamic Text dialog box, which is shown in Figure 23.36.
Figure 23.36: Select the field that contains the data you want to display.
Select LastOfTitle, and click OK.
Left-align the text by choosing Text ® Align ® Left.
Save the change by choosing File ® Save.
When Dreamweaver MX prompts you to update existing template files, select October.asp, click Update, and then click Close. You may see another warning about code outside the <html> tag. Click OK to clear it, and then close the base template.
In your browser, enter the address to October.asp. Ours is http://localhost/ masteringdreamweaver/chapter23/october.asp. If the page is current, click Refresh.You may need to hold down the Shift key while clicking Refresh. The page in Figure 23.37 shows the dynamic text. The displayed book title will change when you add a new book to the database.
Figure 23.37: Our template-based page displays data from our Books database.