Inheriting Mako Template Templates

We had this code and it worked fine. After working with the refactor, it no longer works. As the comment says, we want to inherit only from the base page if the request is not an ajax request. To do this, we pass the parameter to the template and, based on the parameter, inherit or not.

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)}"/> 

Currently, the Undefined error does not have the __ getitem __ attribute. If we change $ {inherit (context)} to $ {inherit (value)}, we get the value of the global variable undefined.

+4
source share
3 answers

Just ran into the same problem, in the same case as it actually is (rendering the layout or lack of dependency on the XHR request).

Apparently, you can access request through context , so you can avoid breaking this tiny bit of logic into two places (view and pattern):

 <%! def inherit( context ): if not context.get('request').is_xhr: return 'layout_reports.mako' else: return None %> <%inherit file="${inherit(context)}"/> 
+1
source

We made a pretty significant refactor, and the above code works again. I assume that the passed context was not initialized or there was a syntax error in one of the templates.

Aside, the request object has the is_xhr property, which is true if the request is asynchronous. We use this property to determine whether to load a full page or not. Thus is_page = self.request.is_xhr False

0
source

I'm not sure if this works or not

  %if not request.is_xhr: <inherit file='base.mako'/> %endif 

Aassuming request is available in context

-1
source

Source: https://habr.com/ru/post/1488525/


All Articles