[Python编程(第4版)].(Programming.Python.4th.Edition).Mark.Lutz.文字版

(yzsuai) #1

Figure 15-16. Response page created by tutor5.py (2)


Keeping display and logic separate


In fact, this illustrates an important point in the design of larger websites: if we are
careful to keep the HTML and script code separate, we get a useful division of display
and logic—each part can be worked on independently, by people with different skill
sets. Web page designers, for example, can work on the display layout, while pro-
grammers can code business logic.


Although this section’s example is fairly small, it already benefits from this separation
for the input page. In some cases, the separation is harder to accomplish, because our
example scripts embed the HTML of reply pages. With just a little more work, though,
we can usually split the reply HTML off into separate files that can also be developed
independently of the script’s logic. The html string in tutor5.py ( Example 15-12), for
instance, might be stored in a text file and loaded by the script when run.


In larger systems, tools such as server-side HTML templating languages help make the
division of display and logic even easier to achieve. The Python Server Pages system
and frameworks such as Zope and Django, for instance, promote the separation of
display and logic by providing reply page description languages that are expanded to
include portions generated by separate Python program logic. In a sense, server-side
templating languages embed Python in HTML—the opposite of CGI scripts that embed
HTML in Python—and may provide a cleaner division of labor, provided the Python


Climbing the CGI Learning Curve| 1169
Free download pdf