从文档中您可以看到编写模板标签涉及编写目标函数和渲染器。所以我假设您当前的代码如下所示:
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
This should do the trick. It's not as elegant as including your dependencies in the top of the page, but it's enough not to include them for every time you need to call them.