edx-platform中的Python模板语法
我正在学习edx-platform中的lms服务。它使用的是Python 2.7和Django框架。一般来说,使用Python模板语法的方式如下:
{% extends "base_generic.html" %}
{% block title %}{{ section.title }}{% endblock %}
{% block content %}
<h1>{{ section.title }}</h1>
{% for story in story_list %}
<h2>
<a href="{{ story.get_absolute_url }}">
{{ story.headline|upper }}
</a>
</h2>
<p>{{ story.tease|truncatewords:"100" }}</p>
{% endfor %}
{% endblock %}
但是在edx-platform中,它的用法是这样的:
<%namespace name='static' file='static_content.html'/>
<%!
from django.utils.translation import ugettext as _
from django.core.urlresolvers import reverse
from courseware.courses import course_image_url, get_course_about_section
%>
<%page args="course" />
<article id="${course.id.to_deprecated_string()}" class="course">
%if course.is_newish:
<span class="status">${_("New")}</span>
%endif
<a href="${reverse('about_course', args=[course.id.to_deprecated_string()])}">
<div class="inner-wrapper">
我不太理解这个语法。可能单行用的是“%”,多行用的是“<% %>”。这是Python的语法吗?该怎么使用这个语法呢?
1 个回答
4
edx-platform 使用的是 Mako 模板语言。
Mako 是一个用 Python 写的模板库。它提供了一种熟悉的、非 XML 的语法,这种语法会被编译成 Python 模块,以达到最佳性能。Mako 的语法和 API 借鉴了很多其他优秀的想法,包括 Django 模板、Cheetah、Myghty 和 Genshi。简单来说,Mako 就像是一个嵌入式的 Python 语言(也就是 Python 服务器页面),它改进了组件化布局和继承的常见概念,形成了一种非常简单灵活的模型,同时也与 Python 的调用和作用域语义保持紧密联系。
想了解更多关于 Mako 的信息,可以查看它的文档:http://www.makotemplates.org/docs/