Best approach to render fragments

47 views
Skip to first unread message

Paolo Valleri

unread,
Aug 27, 2013, 5:42:20 AM8/27/13
to web...@googlegroups.com
Dear all, 
I am trying to understand which is the best way to render a fragment (which can be inside an other fragment and so on).
Lets take a very simple example in which I render a list of comments, and by clicking the 'more' button, few other are loaded with ajax.
So far I found a couple of working solutions:

1 approach:
-- layout.html has an include directive
-- wrapper.html extends layout.html and include comment.html
-- comment.html, renders comments.

When the controller receives an ajax call sets response.view to comment.html otherwise wrapper.html

2. approach:
- layout.html, same as before, it has the include directive
- empty.html, it has only the include directive
- comment.html. it has {{extend 'layout.html' if not request.ajax else 'empty.html'}} 

The first approach is difficult to maintain, the example is very simple, the chain can be arbitrary long. The second one doesn't compile, and empty.html is very ugly. I am wondering if could be possible to have extend and include more flexible and easier.

Paolo

Anthony

unread,
Aug 27, 2013, 9:05:10 AM8/27/13
to web...@googlegroups.com

The first approach is difficult to maintain, the example is very simple, the chain can be arbitrary long. The second one doesn't compile, and empty.html is very ugly. I am wondering if could be possible to have extend and include more flexible and easier.

What exactly do you envision? What do you mean by "more flexible and easier"?

Note, rather than nesting lots of separate views, you might consider using Python logic within a given view. You could also have relatively simple request return rendered results directly rather than passing data to a view (if you return a string or HTML helper object, you don't need a view).

Anthony

paolo....@gmail.com

unread,
Aug 27, 2013, 9:18:22 AM8/27/13
to web...@googlegroups.com
I don't know rails at all, and actually I don't even like to compare frameworks, but with flexible and easier I meant something like (rails): "render layout: false". In a talk I attended I remember that symfony has something very similar as well.
Given that, I know they are completely different frameworks but personally I don't know how to simply solve the example proposed in a cleaner way.

 Paolo


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

--
 
---
You received this message because you are subscribed to a topic in the Google Groups "web2py-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/web2py/NhgqZlbu8L8/unsubscribe.
To unsubscribe from this group and all its topics, send an email to web2py+un...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Anthony

unread,
Aug 27, 2013, 11:18:17 AM8/27/13
to web...@googlegroups.com
On Tuesday, August 27, 2013 6:18:22 AM UTC-7, Paolo Valleri wrote:
I don't know rails at all, and actually I don't even like to compare frameworks, but with flexible and easier I meant something like (rails): "render layout: false".

I see. Yes, I suppose it would be nice to have a conditional "extend" (equivalent to Rails "render layout: false"). You still wouldn't be able to compile the entire view, though, because the components of the view wouldn't be determined until request time.

Anthony 
Reply all
Reply to author
Forward
0 new messages