django前端模板 mako的使用.md
阿新 • • 發佈:2018-12-12
django前端模板 mako的使用
標籤(空格分隔): mako的使用
###mako是應用於django的一個前端模板:
要使用mako首先要引入mako
pip install Mako==1.0.4
###對於後端使用mako 首先mako模板能夠接受view傳遞的通過python的語法對html進行修改
views ----------》 render_mako_context(request,"/home.html")
mako 找到/templates/home.html。對裡面的動態變數進行解釋以後生成htm
###其中render_mako_context用法
def render_mako_context(request,template_name,dictionary={}): request: django原生request物件 template:/templates/目錄下的html檔名 dictionary: 要傳遞給模板的引數,dict格式
###語法迴圈 和django自帶的模板語法差不多:
<ul>
%for book in books:
<li> ${book["name"]}<li>
%endfor
</ul>
###模板巢狀 <%block> 和 <%inherit>
<%block name="content">
</%block>
###對於函式的定義
<%def>
對於函式首先是定義,然後是使用
1:定義
<%def name="myfunc(x)"> this is myfunc ,x is ${x} </%def>
2:使用
${myfunc(7)}
###檔案引用<%include/>
<%include file = "header.html">
這裡是正文內容${myfunc(7)}
<%include file = "footer.html"/>
注:如果header.html中包含mako的函式。可以在正文中直接使用