Conditional extend in templates?

193 views
Skip to first unread message

Anthony

unread,
Aug 27, 2013, 1:13:18 PM8/27/13
to
As a follow-up to this topic, should we allow conditional {{extend}}'s in templates? Attached is a patch -- if the expression after "extend" evaluates to a falsey value, it extends a dummy template that contains only "{{include}}", which is equivalent to not doing an extend at all. So, now you can do:

{{extend 'mylayout.html' if some_condition else None}}

Also works with includes:

{{include 'myview.html' if some_condition else None}}

I think the above is better than:

{{if some_condition:}}
{{include 'myview.html'}}
{{pass}}

because the latter version will still include and parse (though not render) the 'myview.html' template, whereas the former will not do that extra processing.

Anthony
parse_post_vars.patch

Vinicius Assef

unread,
Aug 27, 2013, 12:44:01 PM8/27/13
to web2py-d...@googlegroups.com
Very useful. :-)

I liked it.
> --
> -- mail from:GoogleGroups "web2py-developers" mailing list
> make speech: web2py-d...@googlegroups.com
> unsubscribe: web2py-develop...@googlegroups.com
> details : http://groups.google.com/group/web2py-developers
> the project: http://code.google.com/p/web2py/
> official : http://www.web2py.com/
> ---
> You received this message because you are subscribed to the Google Groups
> "web2py-developers" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to web2py-develop...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.

Anthony

unread,
Aug 27, 2013, 1:12:42 PM8/27/13
to web2py-d...@googlegroups.com
Slight tweak attached for the case of custom delimiters.
parse_post_vars.patch

Paolo Valleri

unread,
Aug 27, 2013, 5:09:06 PM8/27/13
to web2py-d...@googlegroups.com
Is it possible to compile something like that:

{{extend run_time_layout_selector()}}
When a function is provided instead of a filename, the extend is executed directly at runtime as a normal request.

Moreover, given the following code:
{{extend 'layout.html' if not request.ajax else 'layout2.html'}}

The compiler fails because request is not defined. In this case the whole application is not compiled at all.
I wondering if there is the possible to "catch" this compilation error and automatically wrap the entire code into a (lambda?) function (the run_time_layout_selector()) executed at runtime?

By doing this, only the specific template is rendered slower leaving to the rest of the application to be compiled without problem.

Paolo

Massimo DiPierro

unread,
Aug 27, 2013, 5:12:46 PM8/27/13
to web2py-d...@googlegroups.com
Yes. this way conditional templates where never supported. They break compilation. I did not review Anthony's solution yet.

Massimo

Anthony

unread,
Aug 28, 2013, 1:11:32 AM8/28/13
to web2py-d...@googlegroups.com
On Tuesday, August 27, 2013 2:12:46 PM UTC-7, Massimo Di Pierro wrote:
Yes. this way conditional templates where never supported. They break compilation. I did not review Anthony's solution yet.

My solution is simple and does nothing about compilation, which I don't think is feasible given the current architecture. The idea is simply to make this possible in case someone needs it and is willing to forego compilation for such views (you could still compile other views).

Anthony

Anthony

unread,
Aug 28, 2013, 1:20:30 AM8/28/13
to web2py-d...@googlegroups.com

The compiler fails because request is not defined. In this case the whole application is not compiled at all.
I wondering if there is the possible to "catch" this compilation error and automatically wrap the entire code into a (lambda?) function (the run_time_layout_selector()) executed at runtime?

Yes, maybe if a view fails to compile, rather than raising an exception and abandoning compilation of the entire app, it should just proceed without compiling that particular view (and possibly display a warning when compiling via admin).

Anthony

Anthony

unread,
Aug 28, 2013, 2:13:16 PM8/28/13
to web2py-d...@googlegroups.com
Attached is a patch that enables app compilation when some of the views are not compileable. It puts two compile options in the admin menu for each site: "Compile (skip failed views)" and "Compile (all or nothing)" (the latter yields the old behavior -- the entire app is not compiled if a single view fails to compile).

If you choose the "skip failed views" option, the app will still compile, but the flash message will display a message listing the views that failed to compile and warn the user not to use the "Pack compiled" feature (that feature packs only the compiled files, so the non-compiled views won't be available in the packed app). For views that don't compile, the non-compiled view will be used (the order of precedence is compiled view > non-compiled view > compiled generic). Could use some testing.

Anthony
compile_with_failed_views.patch

paolo....@gmail.com

unread,
Aug 28, 2013, 4:09:56 PM8/28/13
to web2py-d...@googlegroups.com
Well done, I've just tested without any problem.

 Paolo


2013/8/28 Anthony <abas...@gmail.com>

--
-- mail from:GoogleGroups "web2py-developers" mailing list
make speech: web2py-d...@googlegroups.com
unsubscribe: web2py-develop...@googlegroups.com
details : http://groups.google.com/group/web2py-developers
the project: http://code.google.com/p/web2py/
official : http://www.web2py.com/
---
You received this message because you are subscribed to a topic in the Google Groups "web2py-developers" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/web2py-developers/GhvqakUaujA/unsubscribe.
To unsubscribe from this group and all its topics, send an email to web2py-develop...@googlegroups.com.

Massimo DiPierro

unread,
Aug 28, 2013, 11:15:24 PM8/28/13
to web2py-d...@googlegroups.com
I have no objection on the technical side of this patch but is the purpose of compiling and skipping failing views if the app cannot be packed compiled? The only purpose of compilation is allow distribution in compiled form.

Massimo

--
-- mail from:GoogleGroups "web2py-developers" mailing list
make speech: web2py-d...@googlegroups.com
unsubscribe: web2py-develop...@googlegroups.com
details : http://groups.google.com/group/web2py-developers
the project: http://code.google.com/p/web2py/
official : http://www.web2py.com/
---
You received this message because you are subscribed to the Google Groups "web2py-developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to web2py-develop...@googlegroups.com.

For more options, visit https://groups.google.com/groups/opt_out.
<compile_with_failed_views.patch>

Anthony

unread,
Aug 28, 2013, 11:58:40 PM8/28/13
to web2py-d...@googlegroups.com
On Wednesday, August 28, 2013 8:15:24 PM UTC-7, Massimo Di Pierro wrote:
I have no objection on the technical side of this patch but is the purpose of compiling and skipping failing views if the app cannot be packed compiled? The only purpose of compilation is allow distribution in compiled form.

You can pack just the compiled files and distribute only those (via the "Pack compiled" feature), and for that, this is not helpful. However, I think the main purpose of compiling is to speed up execution, no? In that case, why should you be prevented from compiling the entire app just because one view won't compile? You should be able to benefit from compiled versions of everything that can be compiled, even if there are a few special views that cannot be compiled. With this patch, if you have a few views that don't compile, the non-compiled versions of those views will be executed, but for all other views (and of course the controllers and models), the compiled versions will be executed.

Anthony

Tim Richardson

unread,
Feb 10, 2014, 1:10:09 PM2/10/14
to web2py-d...@googlegroups.com
should this be documented?

Anthony

unread,
Feb 10, 2014, 4:52:46 PM2/10/14
to web2py-d...@googlegroups.com
That would be great.

On Monday, February 10, 2014 1:10:09 PM UTC-5, Tim Richardson wrote:
should this be documented?

Massimo Di Pierro

unread,
Dec 4, 2015, 1:09:51 PM12/4/15
to web2py-developers
I have added this but please check this again! This patch is 3 years old.


On Wednesday, 28 August 2013 15:09:56 UTC-5, Paolo Valleri wrote:
Well done, I've just tested without any problem.

 Paolo


2013/8/28 Anthony <abas...@gmail.com>
Attached is a patch that enables app compilation when some of the views are not compileable. It puts two compile options in the admin menu for each site: "Compile (skip failed views)" and "Compile (all or nothing)" (the latter yields the old behavior -- the entire app is not compiled if a single view fails to compile).

If you choose the "skip failed views" option, the app will still compile, but the flash message will display a message listing the views that failed to compile and warn the user not to use the "Pack compiled" feature (that feature packs only the compiled files, so the non-compiled views won't be available in the packed app). For views that don't compile, the non-compiled view will be used (the order of precedence is compiled view > non-compiled view > compiled generic). Could use some testing.

Anthony


On Tuesday, August 27, 2013 10:20:30 PM UTC-7, Anthony wrote:

The compiler fails because request is not defined. In this case the whole application is not compiled at all.
I wondering if there is the possible to "catch" this compilation error and automatically wrap the entire code into a (lambda?) function (the run_time_layout_selector()) executed at runtime?

Yes, maybe if a view fails to compile, rather than raising an exception and abandoning compilation of the entire app, it should just proceed without compiling that particular view (and possibly display a warning when compiling via admin).

Anthony

--
-- mail from:GoogleGroups "web2py-developers" mailing list

details : http://groups.google.com/group/web2py-developers
the project: http://code.google.com/p/web2py/
official : http://www.web2py.com/
---
You received this message because you are subscribed to a topic in the Google Groups "web2py-developers" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/web2py-developers/GhvqakUaujA/unsubscribe.
To unsubscribe from this group and all its topics, send an email to web2py-developers+unsubscribe@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages