I have a Coldfusion8 page, in which I’m declaring some variables upfront like so:
<cfif (structKeyExists(url,"extern"))>
<cfset variables.someVar = "value">
<cfelse>
<cfset variables.someVar = "">
</cfif>
Following this I have a number of templates, which I’m loading:
<cfinclude template="templates/tmp_pagetop.cfm">
<cfoutput><head></cfoutput>
<cfinclude template="templates/tmp_pageheader.cfm">
...
I’m having trouble accessing my variables.someVar inside my template.
Question:
Is this at all possible using the variables scope? I don’t want to use session or application scope, because the variables I’m dealing with should actually only exist in the respective page. But I thought it would be possible to declare once in the page and reference throughout the templates. If I can’t use variables, is there another way?
EDIT:
First up, thanks for all the feedback!
When I started using SO, I usually put up questions like below, but ended up cutting down to the bare minimum, as I usually didn’t get an answer on thourough questions… pity… I guess I may be leaving too much away 🙂
So, if anyone wants to take a look, I have a page called search.cfm. This page is a shell page into which I’m loading different layouts through AJAX and/or templates. Specific case was the search form loaded via AJAX.
So this is 3 parts:
1) search.cfm Here I’m checking Session.extern(al) for local instances of the page. If it’s a local instance I grab the users variabels A,B,C which I had hoped to access when loading in the search form template.
<cfif (structKeyExists(url,"extern"))>
<!--- preload external user data --->
<cfstoredproc procedure="proc_select_extern" datasource="dtb">
<cfprocparam type="in" value="#Session.extern#" cfsqltype="cf_sql_varchar" maxlength="13">
<cfprocresult name="external_user">
</cfstoredproc>
<!--- set external variables --->
<cfif external_user.recordcount eq 1>
<cfoutput query="external_user">
<cfscript>
// CULPRIT string
variables.user_modules = external_user.modules;
</cfscript>
</cfoutput>
<cfelse>
<!--- remove unknown URL params --->
<cfset StructDelete(Session, "Extern")>
</cfif>
</cfif>
Then in my app.js I’m listening for the (Jquery-Mobile) pagebeforeshow event and load the respective form into the page via AJAX:
$(document).on('pagebeforeshow', '#search' , function(e, data) {
// load main search form
if ( $(this).attr('val') != true ) {
$(this).attr('val') == true;
// here I'm calling the default search form
ajaxUpdate( "../layouts/tmp_searcher.cfm", $('.searchFromWrapper'), "search", "default", "search" );
}
....
var ajaxUpdate =
function update( from, target, param, account, bindings ) {
$.mobile.loading( 'show' );
$.ajax({
async: true, type: 'GET', returnFormat: 'json',
data: { value: param, type: account },
url: from, timeout: 7500,
success: function(data) {
var makeUp = !$.support.touch ? data.replace("<select", "<select data-native-menu='false' ") : data;
target.addClass('.fade.out')
.html( makeUp )
...
});
The tmp_searcher.cfm template being loaded contains all search form variants. On initial load I’m grabbing the basic or external(userID) based one.
It’s inside tmp_searcher.cfm, where I cannot access variables.XXX anymore… same as URL from my earlier question…. Oh well, writing it down it sort of makes sense, why it’s not working 😉
I spare you the details, first line in tmp_searcher.cfm is:
<cfdump output="D:\ColdFusion8\logs\dump.txt" label="catch" var="#variables.user_module#">
Which dumps nothing and (@Leigh) throws an error:
Element USER_MODULE is undefined in VARIABLES
So, my question should have been:
Is there a way for Coldfusion variables to persist through AJAX calls, if the content is on the same page and I’m not wanting to use Session or Application Scope?
You can use the request scope. That will let you access any variables in the request, including the templates.
But the way I use to do it was to use cfimport with a prefix like.. “ui”
Then inside your template you have access to
attributes.heyLookAVarYou can get really fancy and detect if the template calling tag is ending or beginning… then you only need one