blog.Ring.idv.tw

Python Server Pages - Nested PSP Templates

Python Server Pages - Nested PSP Templates


在上一篇「Python Server Pages - Forms」已經簡單的記錄一下「Form」的處理~

而這篇則是針對如果我們想在同一份頁面中~ 分離多個「Template」來進行處理~ 它就稱作「Nested PSP Templates」。

我們仍然以上一篇文章的例子來作解釋以方便理解。

Nested PSP Templates

「form.html」不需要變動到它,只要更動「index.py」和相關的「templates」即可。

index.py

from mod_python import apache, psp

def index(req,userid,fruit):
	req.content_type = 'text/html'
	uid_temp = psp.PSP(req, filename='uid.tmpl',vars = {'uid':userid})
	fruit_temp = psp.PSP(req, filename='fruit.tmpl')
	fruit_temp.run({'fruit':fruit,'uid_temp':uid_temp})

從這個例子可以發現,我們同時使用到「uid.tmpl」和「fruit.tmpl」來處理同一份頁面。

uid.tmpl

<h1>Hello, <%=uid%></h1>

fruit.tmpl

<html>
<%=uid_temp%>
<%
for f in fruit:
%>
<%=f%>
<%
%>
</html>

在這個範例上,整個重點就在於「uid_temp」已經優先被剖析和編譯了,直到執行「fruit_temp.run()」時才會整個傳送到Client端~

相關資源

4.9 psp - Python Server Pages

2008-10-01 01:43:01

Leave a Comment

Copyright (C) Ching-Shen Chen. All rights reserved.

::: 搜尋 :::

::: 分類 :::

::: Ads :::

::: 最新文章 :::

::: 最新回應 :::

::: 訂閱 :::

Atom feed
Atom Comment