① _layout中的#@main()是调用layout函数的页面本身吗?因为有好几个#define main()
②jfinal_demo中的blog.html的分页:
#@paginate(blogPage.pageNumber, blogPage.totalPage, "/blog/")
与_paginate.html中的代码看不太懂,有没有大佬帮帮我,稍微解释一下精髓
#define paginate(currentPage, totalPage, link)
#if(totalPage <= 0 || currentPage > totalPage) #return #end
#setLocal(startPage = currentPage - 4)
#if (startPage < 1) #setLocal(startPage = 1) #end
#setLocal(endPage = currentPage + 4)
#if (endPage > totalPage) #setLocal(endPage = totalPage) #end
<div class="pagination">
#if (currentPage <= 8)
#setLocal(startPage = 1)
#end
#if ((totalPage - currentPage) < 8)
#setLocal(endPage = totalPage)
#end
#if (currentPage == 1)
<span class="disabled prev_page">上页</span>
#else
<a href="#(link)#(currentPage - 1)#(append)" class="prev_page">上页</a>
#end
#if (currentPage > 8)
<a href="#(link)#(1)#(append)">#(1)</a>
<a href="#(link)#(2)#(append)">#(2)</a>
<span class="gap">…</span>
#end
#for(i : [startPage..endPage])
#if (currentPage == i)
<span class="current">#(i)</span>
#else
<a href="#(link)#(i)#(append)">#(i)</a>
#end
#end
#if ((totalPage - currentPage) >= 8)
<span class="gap">…</span>
<a href="#(link)#(totalPage - 1)#(append)">#(totalPage - 1)</a>
<a href="#(link)#(totalPage)#(append)">#(totalPage)</a>
#end
#if (currentPage == totalPage)
<span class="disabled next_page">下页</span>
#else
<a href="#(link)#(currentPage + 1)#(append)" class="next_page" rel="next">下页</a>
#end
</div>
#end
public void configEngine(Engine me) {
me.addSharedFunction("/common/_layout.html");
me.addSharedFunction("/common/_paginate.html");
}
me.addSharedFunction(...) 方法会将 .html 中的所有 #define xxx() 定义的模板函数给 "搞成" 全局的
所以你在任意页面都可以调用 _layout.htm 中的 #define layout() , 在任意页面都可以调用 _paginate.html 的 #define paginate(...)
目光回到你的 controller 中的 render("blog.html"),在 blog.html 中你会看到 #@layout() 这类调用,就是在调用前面提到的全局模板函数
这个逻辑并不复杂,就是先添加全局函数,然后任何地方调用