3

I sometimes need to use partial views in Web2Py, but I need to pass some specific variables to them. In Django it would look like this:

{% include "image.html" with caption="Me" source="http://example.com/img.png" %}

In case of Web2Py I can do something like:

{{ include "image.html" }}

but there is not even a single mention about passing variables to partial views within the documentation (or I am missing something pretty obvious).

The use case for this is decreasing complexity of the views (and implementing DRY rule) and displaying some complex content within a loop (eg. images, complex containers etc.).

I do not want to use my own tags/functions instead - I need something quick and simple, just to include partial view with specific variables. Similarly as it could be done in Django or many other web frameworks. Is it even possible, or due to Web2Py's architecture it is rather impossible / laborous?

Please tell me if this is possible in web2py or if I should rather create my own tag to use it within views (if so, what is the easiest/simplest way to do it?).

Thanks.

Tadeck
  • 132,510
  • 28
  • 152
  • 198

3 Answers3

5

Interrobang's answer is correct -- variables returned by the controller will be available even in included (as well as extended) views. So, you can do:

In mycontroller.py:

def myfunc():
    return dict(caption='Me', source='http://example.com/img.png')

and then in /views/mycontroller/myfunc.html:

{{include 'image.html'}}

In that case, caption and source will be available in the image.html view. Instead of returning caption and source from the controller, another option is just to define them in the view before the include directive:

{{caption = 'Me'
  source = 'http://example.com/img.png'}}
{{include 'image.html'}}
Anthony
  • 25,466
  • 3
  • 28
  • 57
  • Thanks, Anthony. This is what I needed, I forgot about the implicit natur of web2py. I have a question, though. If I render some view within an action of some controller (eg. for passing through AJAX or for further processing), and then define a variable within view, as you suggested, will it be accessible in the action body (after view rendering, but before returning value by the action)? Also as I assume, passed variable reassigned in any of the views will have changed value in any view executed after that? Is there any namespacing? – Tadeck Mar 24 '12 at 03:08
  • No, variables defined in a view will not be accessible in the action that renders the view. When you call `response.render()`, it executes the view in a new environment, so the controller environment remains unaffected. And you are correct about reassigning variables in views. When you render a view, there is no namespacing among extended and included views. The reason is that the extended and included views are first combined and then executed as one single view (that's why objects defined before an `include` or `extend` are available in the included or extended view). – Anthony Mar 24 '12 at 13:55
2

From the book:

It is also worth pointing out that the variables returned by the controller function are available not only in the function's main view, but in all of its extended and included views as well.

Unless I'm misunderstanding your question, you don't have to specifically pass the variables-- instead, just use them as normal.

Interrobang
  • 16,984
  • 3
  • 55
  • 63
0

To elaborate on Anthony's answer, If you need additional variables passed to the view, just include them in the return dict. In my current project I pass a whole bunch of variables to be used in the view.

return dict(maxsize=5, message='hello world', fadetimeout=10, warning=0)

Also if you need to access certain values in multiple views in your web, you could store them in the session.

session.some_var_i_need_in_multiple_views = ['one', 'two', 'three']

Then access it in the view:

{{=H3(session.some_var_i_need_in_multiple_views[0])}}
profitehlolz
  • 640
  • 1
  • 10
  • 16
  • I really don't like things you can do in the views, because you cen expect eg. session variables being changed there. Thanks for your answer, the question was about partial views - I forgot that variables are passed into partial views in web2py implicitly, as opposed to the majority of Python & PHP frameworks I know. – Tadeck Mar 24 '12 at 03:01