我可以在Web2Py中包含部分视图,将特定变量传递给它吗? [英] Can I include partial view in Web2Py, passing specific variables into it?

查看:216
本文介绍了我可以在Web2Py中包含部分视图,将特定变量传递给它吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有时需要在Web2Py 中使用部分视图,但是我需要传递一些特定的变量。在Django中,它将如下所示:

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" %}

如果是Web2Py,我可以做某事如:

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

这个用例是降低复杂性视图(并实现DRY规则)并在循环中显示一些复杂的内容(例如图像,复杂容器等)。

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

我不想使用自己的标签/ functions改为 - 我需要一些快速和简单的东西,只是为了包括具有特定变量的部分视图。类似地,可以在Django或许多其他Web框架中完成。是否可能,或由于Web2Py的架构,这是不可能/有力的?

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?

请告诉我,如果这是可能的web2py或者我应该建立自己的标签在视图中使用它(如果是,最简单/最简单的方法是什么?)。

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

谢谢。

推荐答案

Interrobang的答案是正确的 - 控制器返回的变量即使在包含(以及扩展)视图中也可以使用。所以你可以这样做:

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:

在mycontroller.py中:

In mycontroller.py:

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

然后在/views/mycontroller/myfunc.html中:

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

{{include 'image.html'}}

在这种情况下, caption 将在image.html视图中可用。而不是从控制器返回 caption source ,另一个选项只是在视图之前定义它们 include 指令:

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'}}

这篇关于我可以在Web2Py中包含部分视图,将特定变量传递给它吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆