Ò׽ؽØÍ¼Èí¼þ¡¢µ¥Îļþ¡¢Ãâ°²×°¡¢´¿ÂÌÉ«¡¢½ö160KB

Python Mako Template ѧϰ±Ê¼Ç


MakoÊÇʲô£¿MokoÊÇPythonдµÄÒ»¸öÄ£°å¿â£¬Python¹ÙÍøpython.orgÓõľÍÊÇËüŶ¡£ÆäËû·Ï»°Ò²¾Í²»ÀÛ׸ÁË£¬Ö±½ÓÀ´µã´úÂ룬·½±ãÔĶÁÓëÁ˽â°Ñ¡£
(Mako¹ÙÍøµØÖ·£ºhttp://www.makotemplates.org/ £¬¿ÉÒÔÏÂÔØ°²×°°ü£¬ÍƼöʹÓÃeasy_install°²×°)
from mako.template import Template
mytemplate = Template("hello world!")
print mytemplate.render()
mytemplate = Template("hello, ${name}!")
print mytemplate.render(name="jack")
´úÂë¿ÉÒԲο¼¹Ù·½doc²¿·Ö
mytemplate = Template(filename='/docs/mytmpl.txt')
print mytemplate.render()
»¹¿ÉÒÔ´ÓÉèÖÃÄ£°åΪÎļþ£¬ÉèÖÃfilenameÊôÐÔ
mytemplate = Template(filename='/docs/mytmpl.txt', module_directory='/tmp/mako_modules')
print mytemplate.render()
Îļþ»¹¿ÉÒÔ»º´æµ½Ä³¸öĿ¼Ï£¬ÏÂÃæµÄ/docs/mytmpl.txt»á²úÉúÒ»¸öpy£º/tmp/mako_modules/docs/mytmpl.txt.py
from mako.lookup import TemplateLookup
mylookup = TemplateLookup(directories=['/docs'])
mytemplate = Template("""<%include file="header.txt"/> hello world!""", lookup=mylookup)
²éÕÒÄ£°å£¬·½±ãͳһģ°å·¾¶Ê¹Óá£
mylookup = TemplateLookup(directories=['/docs'], module_directory='/tmp/mako_modules')
def serve_template(templatename, **kwargs):
mytemplate = mylookup.get_template(templatename)
print mytemplate.render(**kwargs)
¸ÄÁ¼ÁËÉÏÃæµÄ²éÕÒ·½Ê½
mylookup = TemplateLookup(directories=['/docs'], output_encoding='utf-8',
encoding_errors='replace')
mytemplate = mylookup.get_template("foo.txt")
print mytemplate.render()
ÉèÖÃÊä³ö±àÂ룬ÒÔ¼°±àÂë´íÎóʱºò´¦Àí·½Ê½
.
À´Ô´:"СÓ㲩¿Í" http://chenxiaoyu.org/blog/


Ïà¹ØÎĵµ£º

pythonÖÐÀàµÄÊôÐÔ

pythonÖÐÀàµÄÊôÐÔ
pythonÖеÄÀà½Ðclass object,ÀàµÄʵÀý½Ðinstance object.
Àà Class Objects
ÀàÓµÓÐÁ½ÖÖ²Ù×÷,1.ÀàÊôÐÔ attribute references 2.ʵÀý»¯instantiation
1.ÀàÊôÐÔ¾ÍÏ൱ÓÚרÊôÓÚÒ»¸öÀàµÄ±äÁ¿(¼´Ä³Ð©ÓïÑÔÖеÄÀàµÄ¾²Ì¬¹«¹²±äÁ¿static public),ʹÓ÷½·¨ÊÇ:ÀàÃû³Æ.ÀàÊôÐÔÃû³Æ
2.ʵÀý»¯ÔòÊÇ´´½¨Ò»¸öÀàµÄʵÀýµÄ· ......

beginning python summary chapter 3 ʹÓÃ×Ö·û´®

0¡¢×Ö·û´®ÊDz»¿É±äµÄ¡£
1¡¢»ù±¾×Ö·û´®²Ù×÷£ºË÷Òý¡¢ÇÐÆ¬¡¢¸´ÖÆ¡¢³ÉÔ±¡¢³¤¶È¡¢×î´óºÍ×îС¡£
2¡¢×Ö·û´®¸ñʽ»¯£ºÓøñʽ»¯²Ù×÷·û°Ù·ÖºÅ%ʵÏÖ£¬eg:>>> format = "Hello, %s. %s enough for ya?"
                    ......

pythonÖÐos.path.dirname(__file__)µÄʹÓÃ

     (1).µ±"print os.path.dirname(__file__)"ËùÔڽű¾ÊÇÒÔÍêÕû·¾¶±»ÔËÐеģ¬ ÄÇô½«Êä³ö¸Ã½Å±¾ËùÔÚµÄÍêÕû·¾¶£¬±ÈÈ磺
             python d:\pythonSrc\test\test.py
         &nb ......
© 2009 ej38.com All Rights Reserved. ¹ØÓÚE½¡ÍøÁªÏµÎÒÃÇ | Õ¾µãµØÍ¼ | ¸ÓICP±¸09004571ºÅ