我们有这个代码,它工作正常。重构后,它不再工作。就像评论所说,如果请求不是ajax请求,我们只想从基本页面继承。为此,我们将参数传递给模板,并根据参数继承或不继承。Mako动态模板继承
View.py
class Router(object):
def __init__(self, request):
self.request = request
@view_config(route_name="home")
def get(self):
template = "home.mak"
value = {'isPage':self.request.is_xhr is False}
return render_to_response(template, value, request=self.request)
Template.mak
##conditional to determine with the template should inherit from the base page
##it shouldn't inherit from the base page is it is being inserted into the page using ajax
<%!
def inherit(context):
if context.get('isPage') == True:
return "base.mak"
else:
return None
%>
<%inherit file="${inherit(context)}"/>
目前,错误的是未定义没有属性__ 的GetItem __。如果我们将$ {inherit(context)}更改为$ {inherit(value)},那么我们得到的全局变量值是未定义的。
你可以尝试把所有的逻辑到inherit标签?只是将函数调用从等式中删除:$ {'base.mak'if context.get('isPage')else None} –
我不认为这是问题所在。我们做了相当大的重构,上面的代码再次运行。我猜传入的上下文没有初始化,或者其中一个模板出现语法错误。 – JeffRegan
另外,请求对象有一个名为is_xhr的属性,如果请求是异步的,则该属性为true。我们使用这个属性来确定我们是否需要加载整个页面。所以is_page = self.request.is_xhr是False – JeffRegan