Using include within cfscript
I’ve only recently started using cfscript to code my components, and today ran into this little problem…
I have an Application.cfc, written in cfscript, and I want it to include a settings file which contains the different settings between development and production servers (so I can update the Application.cfc file without having to worry about altering settings before copying it to the live server).
It looks something like this (simplified, of course!):
component output="false"
{
this.name = "myApp";
this.sessionManagement = true;
this.ormEnabled = true;
this.datasource = "myDatasource";
include "/config/serversettings.cfm";
}
And the settings file it included was this:
// Server settings for dev server
this.ormSettings.logSQL = true;
I couldn’t work out why the logging setting was not being seen. By trial and error with other script-based includes, I discovered the answer:
I’d assumed that, like a normal cfinclude, it would just take the contents of the included file and placed them in place of the include call – so, because I was already within cfscript, it would see the code in the included file as just another piece of cfscript to run.
This doesn’t seem to be the case. By changing the included file to this:
<cfscript>
// Server settings for dev server
this.ormSettings.logSQL = true;
</cfscript>
…it worked perfectly. It would appear that, even if you’re calling an include from within a cfscript block, it doesn’t see what’s included as cfscript unless you explicitly define it as such.
I suppose it makes sense – otherwise you’d never be able to include traditional CFML tags from within cfscript – but it caught me out; I hope this is helpful to anyone else who is caught out by it!
I would expect that to be pretty predictable behavior. A cfinlcude inside a cfoutput block still needs a cfoutput in it to process variables.