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="http://example.com/img.png" %}

      

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?).

Thank.

+3


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:

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 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 = 'http://example.com/img.png'}}
{{include 'image.html'}}

      

+5


source


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.

+2


source


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:

{{=H3(session.some_var_i_need_in_multiple_views[0])}}

      

0


source







All Articles