>"Come to me, all you that are weary and are carrying heavy burdens, and I will give you rest. Take my yoke upon you, and learn from me; for I am gentle and humble in heart, and you will find rest for your souls. For my yoke is easy, and my burden is light."(MATTHEW 12:28-30)
#模æ¿ä¸çè¯æ³
å¨[ä¸ä¸è®²](./311.md)çç»ä¹ ä¸ï¼åä½å·²ç»æå¾ï¼æ¨¡æ¿ä¸`{{placeholder}}`å¯ä»¥æ¥æ¶æ¥èªpythonæä»¶ï¼.pyï¼ä¸éè¿`self.render()`ä¼ è¿æ¥çåæ°å¼ï¼è¿æ ·æ¨¡æ¿ä¸å°±æ¾ç¤ºç¸åºçç»æãå¨è¿éï¼å¯ä»¥å°`{{placeholder}}`ç解为å ä½ç¬¦ï¼å°±å¦ååé䏿 ·å¦ã
è¿æ¯ä¸ç§æåºæ¬çæ¨¡æ¿æ¾ç¤ºæ¹å¼äºãä½å¦æä»
ä»
妿¤ï¼æ¨¡æ¿çåè½æç¹åè°ï¼æ æ³å®ææ¯è¾å¤æçæ°æ®ä¼ éãä¸ä»
ä»
æ¯tornadoï¼å
¶å®æ¡æ¶å¦Djangoçï¼æ¨¡æ¿é½ææ¯è¾âé«çº§âçåè½ãå¨tornadoçæ¨¡æ¿ä¸ï¼åè½è¿æ¯å¾ä¸å°çï¼æ¬è®²ä»ç»æ¨¡æ¿è¯æ³å
ã
##模æ¿ä¸å¾ªç¯çä¾å
卿¨¡æ¿ä¸ï¼ä¹è½åå¨pythonä¸ä¸æ ·ï¼å¯ä»¥ä½¿ç¨æäºè¯æ³ï¼æ¯å¦å¸¸ç¨çifãforãwhileçè¯å¥ï¼ä½¿ç¨æ¹æ³å¦ä¸ï¼
å
çä¾å
å
åä¸ä¸ªpythonæä»¶ï¼å½å为index.py)ï¼è¿ä¸ªæä»¶ä¸æä¸ä¸ªå表`["python", "www.itdiffer.com", "[email protected]"]`ï¼è¦æ±æ¯å°è¿ä¸ªå表éè¿`self.render()`ä¼ ç»æ¨¡æ¿ã
ç¶å卿¨¡æ¿ä¸ï¼å©ç¨forè¯å¥ï¼ä¾æ¬¡æ¾ç¤ºå¾å°çå表ä¸çå
ç´ ã
#! /usr/bin/env python
#-*- coding:utf-8 -*-
import os.path
import tornado.httpserver
import tornado.ioloop
import tornado.web
import tornado.options
from tornado.options import define, options
define("port", default=8000, help="run on the given port", type=int)
class IndexHandler(tornado.web.RequestHandler):
def get(self):
lst = ["python","www.itdiffer.com","[email protected]"] #å®ä¹ä¸ä¸ªlist
self.render("index.html", info=lst) #å°ä¸è¿°å®ä¹çlistä¼ ç»æ¨¡æ¿
handlers = [(r"/", IndexHandler),]
template_path = os.path.join(os.path.dirname(__file__), "temploop") #模æ¿è·¯å¾
if __name__ == "__main__":
tornado.options.parse_command_line()
app = tornado.web.Application(handlers,template_path)
http_server = tornado.httpserver.HTTPServer(app)
http_server.listen(options.port)
tornado.ioloop.IOLoop.instance().start()
æ¨¡æ¿æä»¶ï¼åç§°æ¯index.htmlï¼å¨ç®å½temploopä¸ã代ç å¦ä¸ï¼
There is a list, it is {{info}} I will print the elements of this list in order. {{element}} info[{{index}}] is {{element}}
{% end %}
è¿è¡ä¸é¢çç¨åºï¼
>>> python index.py
ç¶å卿µè§å¨å°åæ ä¸è¾å
¥ï¼`http://localhost:8000`ï¼æ¾ç¤ºç页é¢å¦ä¸å¾ï¼

å¨ä¸é¢çä¾åä¸ï¼ç¨å¦ä¸æ ·å¼ï¼å®ç°äºæ¨¡æ¿ä¸çfor循ç¯ï¼è¿æ¯å¨æ¨¡æ¿ä¸å¸¸ç¨å°çï¼å½ç¶ï¼ä»pythonç¨åºä¸ä¼ è¿æ¥çä¸ä¸å®æ¯listç±»åæ°æ®ï¼ä¹å¯è½æ¯å
¶å®ç±»åçåºåæ°æ®ã
{% for index,element in enumerate(info) %}
info[{{index}}] is {{element}}
{% end %}
ç¹å«æé注æçæ¯ï¼è¯å¥è¦ç¨`{% end %}`æ¥ç»å°¾ãå¨å¾ªç¯ä½ä¸ï¼ç¨`{{ element }}`æ¹å¼ä½¿ç¨åºåçå
ç´ ã
##模æ¿ä¸ç夿è¯å¥
é¤äºå¾ªç¯ä¹å¤ï¼å¨æ¨¡æ¿ä¸ä¹å¯ä»¥æå¤æï¼å¨ä¸é¢ä»£ç çåºç¡ä¸ï¼æ¹åä¸ä¸ï¼ç´æ¥ä¸ä»£ç ï¼ç宿³å¿
ä¹è½çè§£äºã
index.pyç代ç ä¸åï¼åªä¿®æ¹æ¨¡æ¿index.htmlç代ç ï¼éç¹ç解模æ¿ä¸çè¯å¥åæ³ã
There is a list, it is {{info}} I will print the elements of this list in order. {{element}} info[{{index}}] is {{element}}
{% if element == "python" %}
I love this language--{{element}} A Ha, this the python lesson of LaoQi, It is good! His email is {{info[2]}} There is a list, it is {{info}} I will print the elements of this list in order. {{element}} info[{{index}}] is {{element}}
{% if element == "python" %}
I love this language--{{element}} A Ha, this the python lesson of LaoQi, It is good! His email is {{info[2]}} Would you like {{var}}?
{% for index,element in enumerate(info) %}
{% for index,element in enumerate(info) %}
{% for index,element in enumerate(info) %}
Next, I set "python-tornado"(a string) to a variable(var)
{% set var="python-tornado" %}