如何在自定义标记中创建代码块仅在第一次调用标记时运行?

Jos*_*ody 4 coldfusion custom-tags

我正在创建一组ColdFusion自定义标记,旨在简化重用某些布局元素.我将以类似于以下的方式使用它们:

<cfimport prefix="layout" taglib="commonfunctions/layouttags">

<layout:fadingbox>
    This text will fade in and out
</layout:fadingbox>
<layout:stockticker>
    This text will scroll across the screen
</layout>
Run Code Online (Sandbox Code Playgroud)

为了使这些自定义标记生成的代码能够工作,需要将JavaScript文件链接到页面中,如下所示:

<script src="commonfunctions/layouttags/enablingscript.js" type="text/javascript"></script>
Run Code Online (Sandbox Code Playgroud)

我更喜欢在自定义标记内包含脚本,而不是让用户自己包含它.问题是JavaScript文件每页只应包含一次.在第一次使用其中一个自定义标签后,我希望在同一页面上对相同标签的后续调用,以避免重复<script>标签.我想到我可以这样做:

<cfif NOT isDefined("Caller.LayoutTagInitialized")>
    <script src="commonfunctions/layouttags/enablingscript.js" type="text/javascript"></script>
</cfif>
<cfset Caller.LayoutTagInitialized = 1>
Run Code Online (Sandbox Code Playgroud)

......但它似乎不优雅.

我想知道,还有更好的方法吗?

你会如何实现这个?

编辑 - 澄清:

如果我上面写的内容没有意义,这里有一个更详细的例子:

如果我有这样的自定义标签:

<cfif ThisTag.ExecutionMode EQ "start">
    <script src="commonfunctions/layouttags/enablingscript.js" type="text/javascript"></script>
    <div class="mytag">
<cfelse>
    </div>
</cfif>
Run Code Online (Sandbox Code Playgroud)

...我有CFML标记调用标签,如下所示:

<layout:mytag>
    One
</layout:mytag>
<layout:mytag>
    Two
</layout:mytag>
<layout:mytag>
    Three
</layout:mytag>
Run Code Online (Sandbox Code Playgroud)

...我希望生成如下的HTML:

<!-- Script included only the first time the tag is called -->
<script src="commonfunctions/layouttags/enablingscript.js" type="text/javascript"></script>
<div class="mytag">
    One
</div>
<!-- No <script> tag on the second call -->
<div class="mytag">
    Two
</div>
<!-- No <script> tag on the third call -->
<div class="mytag">
    Three
</div>
Run Code Online (Sandbox Code Playgroud)

Pet*_*ton 6

使用请求范围.