Perl Programming Building a Complete Website using the Template Toolkit |
Although the focus of this chapter is on generating web content, it also serves as a general introduction to the Template Toolkit. It demonstrates techniques that can be adapted to different application areas. This chapter will quickly get you up to speed using the Template Toolkit, but without bogging you down in too much gory detail (we’re saving that for the rest of the book). We come back to the Web to look at more advanced examples of static and dynamic web content in Chapter 11 and Chapter 12. Although we may touch briefly on some more advanced issues, we try not to bore you with too much detail, except where it is absolutely necessary to illustrate a key point or explain an important concept. Chapter 3 discusses the syntax and structure of templates and the use of variables, while Chapter 4 covers the various template directives. More information relating to filters and plugins can be found in Chapter 5 and Chapter 6, respectively. More advanced topics concerning the use of the Template Toolkit for generating web content and interfacing to web applications can be found in Chapter 11 and Chapter 12. We assume a Unix system in the examples in this chapter, but the principles apply equally well to other operating systems. On a Microsoft Windows machine, for example, the File Explorer can be used to create folders (directories) and shortcuts (symbolic links) using the familiar point-and-click interface. Another option we can highly recommend is to install Cygwin. Cygwin is freely available from http://www.cygwin.com and provides you with a Unix-like environment on Win32. Getting Started Every big web site is made up of individual pages. Let’s start with a small and simple page, showing how to eliminate basic repetition using templates. In later sections, we can build on this to generate more pages and add more complex elements. A Single Page Example 2-1 shows the HTML markup of a page that displays the customary “Hello World” message, complete with a title, footer, and various other bits of HTML para phernalia. Example 2-1. hello.html <html> HTML is relatively straightforward in terms of syntax and semantics. We’ll assume that you’ve got at least a passing aquaintance with the basics of HTML. If you don’t, HTML & XML by Chuck Musciano and Bill Kennedy (O’Reilly) provides a definitive guide to the subject. Although HTML is simple, it does tend to be rather verbose. It’s all too easy for the core content of the page to be obscured by the extra markup required around it. There’s also some repetition that we would like to avoid. The page title and author’s name both appear twice in the same page, for example. We can also assume that other pages in the site will be using similar pieces of data, repeated over and over again in numerous different places. The author’s name, background color, and copyright message are a few examples of items that we would really rather define in just one place in case we ever decide to change them. We don’t want to have to edit every page in the site when we need to change the copyright message (at the start of a new year, for example), or decide that blue is the new orange and want to use it as the background color for every page.
blog comments powered by Disqus |