我可以在Web2Py中包含部分视图,将特定变量传递给它吗?

Tad*_*eck 3 python django partial-views web2py view

我有时需要在Web2Py中使用部分视图,但我需要将一些特定的变量传递给它们.在Django中它看起来像这样:

{% include "image.html" with caption="Me" source="http://example.com/img.png" %}
Run Code Online (Sandbox Code Playgroud)

在Web2Py的情况下,我可以做类似的事情:

{{ include "image.html" }}
Run Code Online (Sandbox Code Playgroud)

但是甚至没有提到将变量传递给文档中的部分视图(或者我遗漏了一些非常明显的东西).

用于此的用例是降低视图的复杂性(以及实现DRY规则)并在循环内显示一些复杂内容(例如,图像,复杂容器等).

我不想使用我自己的标签/功能 - 我需要快速简单的东西,只是包含具有特定变量的局部视图.同样可以在Django或许多其他Web框架中完成.它是否可能,或者由于Web2Py的架构,它是相当不可能/繁重的?

请告诉我这是否可以在web2py中使用,或者我是否应该创建自己的标记以在视图中使用它(如果是这样,最简单/最简单的方法是什么?).

谢谢.

Ant*_*ony 5

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

在mycontroller.py中:

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

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

{{include 'image.html'}}
Run Code Online (Sandbox Code Playgroud)

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

{{caption = 'Me'
  source = 'http://example.com/img.png'}}
{{include 'image.html'}}
Run Code Online (Sandbox Code Playgroud)