Tornadao—模板语法(控制语句)

  • 控制语句


    可以在Tornado模板中使⽤Python条件和循环语句。控制语句以{\%和\%}包围,
    并以类似下⾯的形式被使⽤:
    {% if page is None %}
    或
    {% if len(entries) == 3 %}

    控制语句的⼤部分就像对应的Python语句⼀样⼯作,⽀持if、for、while,注意
    end:
    {% if ... %} ... {% elif ... %} ... {% else ... %} ... {% end %}
    {% for ... in ... %} ... {% end %}
    {% while ... %} ... {% end %}

    <ul class="house-list">
     {% if len(houses) > 0 %}
     {% for house in houses %}
     <li class="house-item">
     <a href=""><img src="/static/images/home01.jpg"></a>
     <div class="house-desc">
     <div class="landlord-pic"><img
    src="/static/images/landlord01.jpg"></div>
     <div class="house-price">¥<span>{{house["price"]}}
    </span>/晚</div>
     <div class="house-intro">
     <span class="house-title">{{house["title"]}}
    </span>
     <em>整套出租 - {{house["score"]}}
    分/{{house["comments"]}}点评 - {{house["position"]}}</em>
     </div>
     </div>
     </li>
     {% end %}
     {% else %}
     对不起,暂时没有房源。
     {% end %}
    </ul>

    class IndexHandler(RequestHandler):
     def get(self):
     houses = [
     {
     "price": 398,
     "title": "宽窄巷⼦+160平⼤空间+⽂化保护区双地铁",
     "score": 5,
     "comments": 6,
     "position": "北京市丰台区六⾥桥地铁"
     },
     {
     "price": 398,
     "title": "宽窄巷⼦+160平⼤空间+⽂化保护区双地铁",
     "score": 5,
     "comments": 6,
     "position": "北京市丰台区六⾥桥地铁"
     },
     {
     "price": 398,
     "title": "宽窄巷⼦+160平⼤空间+⽂化保护区双地铁",
     "score": 5,
     "comments": 6,
     "position": "北京市丰台区六⾥桥地铁"
     },
     {
     "price": 398,
     "title": "宽窄巷⼦+160平⼤空间+⽂化保护区双地铁",
     "score": 5,
     "comments": 6,
     "position": "北京市丰台区六⾥桥地铁"
     },
     {
     "price": 398,
     "title": "宽窄巷⼦+160平⼤空间+⽂化保护区双地铁",
     "score": 5,
     "comments": 6,
     "position": "北京市丰台区六⾥桥地铁"
     }]
     self.render("index.html", houses=houses)
发布了258 篇原创文章 · 获赞 6 · 访问量 3532

猜你喜欢

转载自blog.csdn.net/piduocheng0577/article/details/105061016