Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timblair/95715 to your computer and use it in GitHub Desktop.
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
<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