{{extend}} and {{include}} are not Python code but are template language 
directives, so they are executed before any Python code is run. As a 
result, you cannot put an {{extend}} inside a Python "if" block. However, 
you can do the following:

{{extend 'layout.html' if not request.ajax else None}}

In other words, the argument to {{extend}} can be a Python expression, and 
if the expression returns None, no layout will be extended. The only 
drawback to this approach is that you cannot compile such a view because 
the value of the Python expression will only be known at runtime, not at 
compile time.

A better approach is as suggested by xmarx, which is to have two separate 
views. You can do so with no code redundancy by simply including the body 
of the Ajax view inside the non-Ajax view. For example, create an Ajax view 
called something like myaction.load, and do not include an {{extend}} in 
it. Then create a non-Ajax view called myaction.html with the following 
content:

{{extend 'layout.html'}}
{{include 'mycontroller/myaction.load'}}

If you have many actions that require this treatment, a third option would 
be to add logic to the layout.html view itself, conditionally turning off 
the header and footer (and whatever else) whenever request.ajax is True.

Anthony

On Friday, July 10, 2015 at 5:30:03 AM UTC-4, PRACHI VAKHARIA wrote:
>
>
>
>  
>
> Dear SK,
>
> That is a possible solution, but I feel that it is not *the most elegant 
> way* to do it, since it involves creating more files leading to 
> redundancy – and difficult to maintain as files number increases.
>
> My intended approach overcomes that with half the number of View files 
> using just one check for Ajax or not.
>
>  
>
> *The only problem with my program is that: it loads the footers twice – 
> and I do not know Why! I think it has something to do with either the very 
> rendering of Layout.html by w2p, or perhaps with my own JS Ajax call 
> function.*
>
> *I do not know why it loads the footers+scripts which come after the 
> {{include}} twice.*
>
>  
>
> Also, running it on Safari from direct URL makes a non-Ajax call first, 
> followed immediately with an Ajax call. That is not happening on Chrome or 
> Firefox. Funny!
>
> *— PRACHI V —*
>
>
>
>
>
> On Friday, 10 July 2015 04:49:38 UTC-4, xmarx wrote:
>>
>> you can write two view.
>>
>> 1. view.html - direct request, extends layout.html
>> 2. view.load - load via ajax. only requested information, not extends 
>> layout or footer etc. 
>>
>>
>> for example:
>>
>> view.load
>>
>> {{for i in range(0,9):}}
>> {{=i}}
>> {{pass}}
>>
>>
>>
>> view.html
>>
>> {{extend 'Layout.html'}}
>>  # other things
>> {{=LOAD('default','view.load',ajax=True)}}
>>
>>
>>
>>

-- 
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
--- 
You received this message because you are subscribed to the Google Groups 
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to