ex:
from django.template import Template, Context
a=Template("<p>hello {{name}} </p>")
b=Context({ 'name': 'Peter' } )
a.render(b)
{{ }} in template define the variable
context is a dictionary that map variable to value
render function replace the value with value
call method in {{ }}:
no () & no arguments
ex:
{{ dog.bark }}
list in {{}}:
ex:
{{items.2}}
dictionary in {{}}:
ex:
{{ Person.name }}
define a method that can not be executed by template
ex:
def test(self):
hello
test.alters_data=true
template tag:
if & else:
ex:
{% if isHot %}
{{isHot}}
{% else %}
not Hot
{% endif %}
and , or, not, can be used in if
for:
ex:
{% for name in nameList %}
{{ name}}
{% endfor %}
for & reverse:
ex:
{% for name in nameList reversed %}
{{name}}
{% endfor %}
forloop.counter:
the number of times the loop has been entered
forloop.first:
forloop.revcounter:
forloop.last:
forloop.parentloop:
reference to the forloop object for the parent loop
ifequal / ifnotequal:
ex:
{% ifequal a b %}
test
{% endifequal %}
comment:
ex:
{# this is a comment #}
filter:
change variable, use | with filter name
ex:
date filter:
{{ tempDate | date: "F j, Y" }}
沒有留言:
張貼留言