6.7 A Better Template

In order to have a header and footer HTML in the previous examples, we used two separate files. It is preferable to maintain one template file ”it's simpler and easier to maintain, and there are fewer opportunities to induce bugs . Both the header and footer HTML can be folded into one file with the following syntax:

 # put all the HTML for the top of the page here  <<PAGE_BODY>>  # put all the HTML for the bottom of the page here  ..PAGE_BODY>>  # the syntax above means to take all the next stuff and stuff it  # into the <<PAGE_BODY>> section 

The diversion PAGE_BODY (again, the name PAGE_BODY is arbitrary; it could have been named just about anything) is included between the HTML for the top of the page and the HTML for the bottom of the page. Then, the last noncomment line of the file is ..PAGE_BODY>> , which means everything that follows is the definition of the PAGE_BODY diversion that is to be included above.

What follows? In this file, nothing, but this file will be use d by another WML file, so the contents of that file are what follows. This is possible because that file, the one that use s this file, has an implicit <<.. terminating the PAGE_BODY definition.

Here is a real, albeit simple, example. This template file is stored in /var/www/html/wml/template1.wml :

 #  # define the head of the template  #  <html>  <head>  <title>Our Template</title>  </head>  <body bgcolor="#ffffff">  <h1>Welcome to www.opensourcewebbook.com!</h1>  #  # here we end the head, and add the body with  # <<PAGE_BODY>>, then start the footer  #  <<PAGE_BODY>>  <hr>  Copyright 2002, <a href="/">www.opensourcewebbook.com</a>.  <br>  Comments are appreciated. Please send them to our  <a href="mailto:webmaster@opensourcewebbook.com">webmaster</a>.  #  # the following is added to say "whatever follows is to be  # dumped into where we say <<PAGE_BODY>>", which is above,  # so all the following text is slurped into the PAGE_BODY  # section, and the following text comes from the file that  # uses or includes this one  #  ..PAGE_BODY>> 

The file that use s this template is /var/www/html/wml/better1.wml :

 #use "template1.wml"  foo  bar  baz  blah 

The file better1.wml use s the template file. That file defines the HTML in the head and the foot of the document. Then, the rest of the page body, which is the stuff in this file after the use , is included between the header and the footer stuff with <<PAGE_BODY>> . To build this, execute:

 $  wmk better1.wml  wml -n -o better1.html better1.wml 

The resulting HTML file is in better1.html :

 <html>  <head>  <title>Our Template</title>  </head>  <body bgcolor="#ffffff">  <h1>Welcome to www.opensourcewebbook.com!</h1>  foo  bar  baz  blah  <hr>  Copyright 2002, <a href="/">www.opensourcewebbook.com</a>.  <br>  Comments are appreciated. Please send to our  <a href="mailto:webmaster@opensourcewebbook.com">webmaster</a>. 

The contents of better1.wml are inserted into the body of the document, and the extra blank lines are removed.

You can view this example by going to http://localhost/wml/better1.html or www.opensourcewebbook.com/wml/better1.html. The result can be seen in Figure 6.8.

Figure 6.8. Our first template


Open Source Development with Lamp
Open Source Development with LAMP: Using Linux, Apache, MySQL, Perl, and PHP
ISBN: 020177061X
EAN: 2147483647
Year: 2002
Pages: 136

Similar book on Amazon

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