從documentation你可以看到,寫作模板標籤涉及編寫目標函數和渲染器。所以我假設你目前的代碼如下所示:
def my_tag(parser, token):
# ... some code
return MyNode(...)
class MyNode(template.Node):
def render(self, context):
# here is where you write your <script> tags
所以基本上,你必須做的是掛在上下文中的變量,因此您可以知道是否針對特定的請求你已經包含的代碼加載所有你需要的腳本。
class MyNode(template.Node):
def render(self, context):
if '_included_faceboxify_deps' in context:
# render your <script> dependency includes
context['_included_faceboxify_deps'] = True
# render your <script>s that are specific for this call
這應該是關鍵。它不像在頁面頂部包含依賴關係那樣優雅,但每次需要調用它們時都不會包含它們。
謝謝,我想這就是我正在尋找的..我會盡快嘗試.. – pocoa 2010-06-13 16:37:57