robot
最新文章(10)
Mqskit 和其它相關工具
CPython 的 GC 二、三事
寫 Mecurial Extension 是件快樂的事!
Mozilla 台灣辨公室徵人啟事
關於 Apple 的兩項專利
core dump 之前的 frame
怎麼發出 beep 聲?
先承認你要找的是奴才吧!
程式碼要清的多乾淨?
FreeBSD 的 Thread-Local Storage 實作
首頁
新編
最新留言
Entries RSS
重要關鍵字(10)
coding (122)
Python (93)
FreeBSD (71)
WEB (61)
URL (48)
hardware (46)
javascript (36)
Linux (34)
blog (30)
C++ (16)
所有關鍵字
新增 URL
ez_xml 的新設計 -- mez_xml
by thinker
2 Columns
關鍵字:
Python
WEB
coding
延宕了個把月,剛剛終於把月前改版的 ez_xml ,稱 mez_xml ,拿出來以 Python2.5 測試。修正幾個小 bug 之後,終於 work 了。新的 tempalte engine 使用如下: == Template Source == {{{ <html> <head> <meta content="text/html; charset=Big5" http-equiv="content-type" /> <title>$test$.xhtml</title> </head> <body> Hello Mr/Miss <span id="NAME">NAME</span> <table> <tr id="LOOP"> <td><span id="XXX">XXX</span></td> <td><span id="YYY">YYY</span></td> <td><input value="{VAL}"/></td> </tr> </table> <table> <tr id="LOOP2"> <td><span id="XXX2">XXX</span></td> <td><span id="YYY2">YYY</span></td> <td><input value="{VAL2}"/></td> </tr> </table> </body> </html> }}} 注意 id 和 {} 括號。 == Compile Template == {{{ mez_xml$$ python mez_xml.py $test$_temp01.xhtml > $test$_temp01.py }}} == Using Template == {{{#!python from __future__ import with_statement from $test$_temp01 import $test$_temp01 from sys import stdout temp = $test$_temp01(stdout) with temp: temp.NAME = 'foo' temp.XXX = 'xXx' for i in range(5): with temp.LOOP: temp.YYY = 'YYY' + str(i) temp.VAL = str(i) pass pass pass }}} 透過新的 with 語法,我們直接在程式的 control flow 裡,指定巢狀的資料,不需另外透過 DOM 或 dictionary 建立 tree style 的資料結構。如此一來, template 的使用更直覺。程式的 structure 直接對映到資料的 structure 。 == 執行結果 == {{{ mez_xml$$ PYTHONPATH=. python2.5 test01.py <html> <head> <meta content="text/html; charset=Big5" http-equiv="content-type"></meta> <title>$test$.xhtml</title> </head> <body> Hello Mr/Miss foo <table> <tr id="LOOP"> <td>xXx</td> <td>YYY0</td> <td><input value="0"></input></td> </tr><tr id="LOOP"> <td>xXx</td> <td>YYY1</td> <td><input value="1"></input></td> </tr><tr id="LOOP"> <td>xXx</td> <td>YYY2</td> <td><input value="2"></input></td> </tr><tr id="LOOP"> <td>xXx</td> <td>YYY3</td> <td><input value="3"></input></td> </tr><tr id="LOOP"> <td>xXx</td> <td>YYY4</td> <td><input value="4"></input></td> </tr> </table> <table> <tr id="LOOP2"> <td>xXx2</td> <td>mmm</td> <td><input value="1"></input></td> </tr><tr id="LOOP2"> <td>xXx2</td> <td>mmm</td> <td><input value="2"></input></td> </tr> </table> </body> </html> mez_xml$$ }}}
最後更新時間: 2007-05-23 23:49:02 CST |
引用
查詢:
COMMENTS: