Time for action implementing the opening screen

The opening screen of the wiki application shows a list of all defined topics on the right and several ways to locate topics on the left. Note that it still looks quite rough because, at this point, we haven't applied any style sheets:

Time for action implementing the opening screen

Let us first take a few steps to identify the underlying structure. This structure is what we would like to represent in the HTML markup:

  • Identify related pieces of information that are grouped together. These form the backbone of a structured web page. In this case, the search features on the left form a group of elements distinct from the list of topics on the right.
  • Identify distinct pieces of functionality within these larger groups. For example, the elements (input field and search button) that together make up the word search are such a piece of functionality, as are the tag search and the tag cloud.
  • Try to identify any hidden functionality, that is, necessary pieces of information that will have to be part of the HTML markup, but are not directly visible on a page. In our case, we have links to the jQuery and JQuery UI JavaScript libraries and links to CSS style sheets.

Identifying these distinct pieces will not only help to put together HTML markup that reflects the structure of a page, but also help to identify necessary functionality in the delivery layer because each of these functional pieces is concerned with specific information processed and produced by the server.

What just happened?

Let us look in somewhat more detail at the structure of the opening page that we identified.

Most notable are three search input fields to locate topics based on words occurring in their bodies, based on their actual title or based on tags associated with a topic. These search fields feature auto complete functionality that allows for comma-separated lists. In the same column, there is also room for a tag cloud, an alphabetical list of tags with font sizes dependent on the number of topics marked with that tag.

The structural components

The HTML markup for this opening page is shown next. It is available as the file basepage.html and the contents of this file are served by several methods in the Wiki class implementing the delivery layer, each with a suitable content segment. Also, some of the content will be filled in by AJAX calls, as we will see in a moment:

Chapter6/basepage.html

<html>
	<head>
			<title>Wiki</title>
			<script
					src=
"http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"
					type="text/javascript">
			</script>
			<script
					src=
"http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.3/jquery-ui.min.js"
					type="text/javascript">
			</script>
			<link rel="stylesheet"
					href="http://ajax.googleapis.com/ajax/libs/
jqueryui/1.8.3/themes/smoothness/jquery-ui.css"
					type="text/css" media="all" />
			<link rel="stylesheet" href="http:///wiki.css"
					type="text/css" media="all" />
	</head>
	<body>
			<div id="navigation">
					<div class="navitem">
						<a href="http://./>Wiki Home</a>
					</div>
					<div class="navitem">
						<span class="label">Search topic</span>
						<form id="topicsearch">
								<input type="text" >
								<button type="submit" >Search</button>
						</form>
					</div>
					<div class="navitem">
						<span class="label">Search word</span>
						<form id="wordsearch">
								<input type="text" >
								<button type="submit" >Search</button>
						</form>
					</div>
					<div class="navitem">
						<span class="label">Search tag</span>
						<form id="tagsearch">
								<input type="text" >
								<button type="submit" >Search</button>
						</form>
					</div>
					<div class="navitem">
						<p id="tagcloud">Tag cloud</p>
					</div>
			</div>
			<div id="content">%s</div>
			<script src="/wikiweb.js" type="text/javascript"></script>
	</body>
</html>

The<head> element contains both links to CSS style sheets and<script> elements that refer to the jQuery libraries. This time, we choose again to retrieve these libraries from a public content delivery network.

The highlighted lines show the top-level<div> elements that define the structure of the page. In this case, we have identified a navigation part and a content part and this is reflected in the HTML markup.

Enclosed in the navigation part are the search functions, each in their own<div> element. The content part contains just an interpolation placeholder %s for now, that will be filled in by the method that serves this markup. Just before the end of the body of the markup is a final<script> element that refers to a JavaScript file that will perform actions specific to our application and we will examine those later.

The application methods

The markup from the previous section is served by methods of the Wiki class, an instance of which class can be mounted as a CherryPy application. The index() method, for example, is where we produce the markup for the opening screen (the complete file is available as wikiweb.py and contains several other methods that we will examine in the following sections):

Chapter6/wikiweb.py

@cherrypy.expose
def index(self):
		item = '<li><a href="http://show?topic=%s">%s</a></li>'
		topiclist = "
".join(
				[item%(t,t)for t in wiki.gettopiclist()])
		content = '<div id="wikihome"><ul>%s</ul></div>'%(
				topiclist,)
		return basepage % content

First, we define the markup for every topic we will display in the main area of the opening page (highlighted). The markup consists of a list item that contains an anchor element that refers to a URL relative to the page showing the opening screen. Using relative URLs allows us to mount the class that implements this part of the application anywhere in the tree that serves the CherryPy application. The show() method that will serve this URL takes a topic parameter whose value is interpolated in the next line for each topic that is present in the database.

The result is joined to a single string that is interpolated into yet another string that encapsulates all the list items we just generated in an unordered list (a<ul> element in the markup) and this is finally returned as the interpolated content of the basepage variable.

In the definition of the index() method, we see a pattern that will be repeated often in the wiki application: methods in the delivery layer, like index(), concern themselves with constructing and serving markup to the client and delegate the actual retrieval of information to a module that knows all about the wiki itself. Here the list of topics is produced by the wiki.gettopiclist() function, while index() converts this information to markup. Separation of these activities helps to keep the code readable and therefore maintainable.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.31.22