How can I make a block of code in a user tag only when the tag is first called?

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 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> 
+4
source share
3 answers

Your decision is just around the corner.

Sam is right that runtime is what you want to use when you need something to appear in the start or end mode of the tag, which is part of what you want.

But then you say that you want the script tag to display in run mode only the first tag used on the page.

This is where you will use the Peter suggestion about the scope of the query. Unlike the default scope (or "variables"), the query scope is distributed among all custom tags for this query. You suggested using a call scope, and this could also work if the caller was not another custom tag, in which case the caller's scope would only be the local scope in the custom tag. The request area (which has been around since CF 4.01) is your best bet.

In this case, your proposed solution was close: in a custom tag in run mode, programmatically check if a tracking variable has already been created in the request area when you place the script tag for the first time. If not, put a script tag and create a tracking variable.

Besides changing the code with the caller for the request, I also suggest that you put CFSET inside IF. No need to run it again when the IF test fails.

+1
source

Use the query area.

+6
source

Custom tags have a built-in scope called thistag.

This code will work:

 <cfif thisTag.ExecutionMode eq "start"> 
+1
source

All Articles