Created
April 15, 2009 10:30
-
-
Save timblair/95715 to your computer and use it in GitHub Desktop.
A hack-tastic way of safely including arbitrary templates in a function contained within a cached ColdFusion component
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<cffunction name="myFunc"> | |
<!-- | |
What's going on here? Basically, any <cfinclude>d view files run within the scope | |
of this function, which means any variables used are effectively unscoped in terms | |
of the component. Because we're caching the view component it means we could end up | |
with nasty race conditions. By var-ing a new 'variables' scope it effectively | |
localises any variable usage (scoped or unscoped) within both the function call and | |
any included files. We also make a reference to the original (component) 'variables' | |
scope in to a new named 'scope' called 'global', so any calls to functions within | |
this component, such as private variables calls, should be made via this 'global' | |
scope: <cfset global.my_private_var = "value"> | |
--> | |
<cfset var global = variables> | |
<cfset var variables = {}> | |
<cfinclude template="..."> | |
</cffunction> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment