I am creating a set of special ColdFusion tags designed to simplify the reuse of some layout elements. I will use them as shown below:
<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>
In order for the code generated by these custom tags to work, the javascript file must be linked on the page as follows:
<script src="commonfunctions/layouttags/enablingscript.js" type="text/javascript"></script>
I would prefer to include the script from user tags instead of having the user include it. The problem is that the javascript file should only be included once per page. After the first use of one of these custom tags, I would like subsequent calls to the same tag on the same page to avoid repeating the <script> tag. It seemed to me that I could do something like this ...
<cfif NOT isDefined("Caller.LayoutTagInitialized")> <script src="commonfunctions/layouttags/enablingscript.js" type="text/javascript"></script> </cfif> <cfset Caller.LayoutTagInitialized = 1>
... But it seems inelegant. I wonder if there is a better way? How do you implement this?
Edit - Clarification:
In case I wrote above, it makes no sense, here is a more detailed example:
If I have my own tag like this ...
<cfif ThisTag.ExecutionMode EQ "start"> <script src="commonfunctions/layouttags/enablingscript.js" type="text/javascript"></script> <div class="mytag"> <cfelse> </div> </cfif>
... and I have CFML markup calling a tag like this ...
<layout:mytag> One </layout:mytag> <layout:mytag> Two </layout:mytag> <layout:mytag> Three </layout:mytag>
... I want the HTML to look like this:
<script src="commonfunctions/layouttags/enablingscript.js" type="text/javascript"></script> <div class="mytag"> One </div> <div class="mytag"> Two </div> <div class="mytag"> Three </div>