Is it possible to include a partial view in Web2Py by passing certain variables to it?

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

{% include "image.html" with caption="Me" source="" %}


In the case of Web2Py, I can do something like:

{{ include "image.html" }}


but there is not even a mention of passing variables to partial views in the documentation (or I'm missing something pretty obvious).

It does this by using complexity reduction (and implementing a DRY rule) and displaying some complex content in a loop (like images, complex containers, etc.).

Instead, I don't want to use my own tags / functions - I need something quick and simple, just to enable a partial view with certain variables. Just like it can be done in Django or many other web environments. Is this possible, or because of the Web2Py architecture, is it impossible or time consuming?

Please tell me if this is possible in web2py or if I prefer to create my own tag to use in views (if so what's the easiest / easiest way to do this?).



source to share

3 answers

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


def myfunc():
    return dict(caption='Me', source='')


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

{{include 'image.html'}}


In this case caption

, source

they will be available in the image.html view. Instead of returning to caption

and source

from the controller, another option is to simply define them in the view before the directive include


{{caption = 'Me'
  source = ''}}
{{include 'image.html'}}




From the book :

It is also worth noting that the variables returned by the controller function are available not only in the main function but in all of its extended and included views.

If I don't understand your question, you don't need to specifically pass variables - instead, use them as usual.



To clarify Anthony's answer, If you need additional variables passed to the view, just include them in the return dict. In my current project, I am passing 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 specific values ​​across multiple views on the web, you can store them in a session.

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


Then go to it in the view:





All Articles