如果不使用会话范围,是否可以在正在加载的模板中访问 Coldfusion 页面上的变量集?



我有一个 Coldfusion8 页面,其中我预先声明了一些变量,如下所示:

<cfif (structKeyExists(url,"extern"))>
    <cfset variables.someVar = "value">
<cfelse>
    <cfset variables.someVar = "">
</cfif>

在此之后,我有许多模板,我正在加载这些模板:

<cfinclude template="templates/tmp_pagetop.cfm">
<cfoutput><head></cfoutput>
<cfinclude template="templates/tmp_pageheader.cfm">
... 

我在模板中访问我的变量 .someVar 时遇到问题。

问题:
使用变量范围可以做到这一点吗?我不想使用会话应用程序范围,因为我正在处理的变量实际上应该只存在于相应的页面中。但我认为可以在页面中声明一次并在整个模板中引用。如果我不能使用变量,还有别的方法吗?

编辑:
首先,感谢您的所有反馈!

当我开始使用 SO 时,我通常会提出如下问题,但最终减少到最低限度,因为我通常没有得到彻底问题的答案......可惜。。。 我想我可能离开了太多:-)

所以,如果有人想看一下,我有一个名为search.cfm的页面。此页面是一个 shell 页面,我通过 AJAX 和/或模板将不同的布局加载到其中。具体情况是通过 AJAX 加载的搜索表单。

所以这是 3 个部分:

1)搜索.cfm在这里我正在检查Session.extern(al)以获取页面的本地实例。如果是本地实例,我会获取用户变量 A、B、C,我希望在搜索表单模板中加载时访问这些变量 A、B、C。

<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>

然后在我的应用程序中.js我正在侦听 (Jquery-Mobile) pagebeforeshow 事件,并通过 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 ) 
                        ...
                });

正在加载的tmp_searcher.cfm模板包含所有搜索表单变体。在初始加载时,我正在抓取基于基本或外部(用户 ID)的。

它在tmp_searcher.cfm里面,我再也无法访问 variables.XXX 了......与我之前问题的网址相同....哦,好吧,把它写下来有点道理,为什么它不起作用;-)

我省略了细节,tmp_searcher.cfm的第一行是:

<cfdump output="D:ColdFusion8logsdump.txt" label="catch" var="#variables.user_module#">

它不转储任何内容并 (@Leigh) 抛出错误:

Element USER_MODULE is undefined in VARIABLES

所以,我的问题应该是:

如果内容在同一页面上并且我不想使用会话或应用程序范围,是否有办法让 Coldfusion 变量通过 AJAX 调用持久化?

您可以使用请求范围。这将允许您访问请求中的任何变量,包括模板。

但是我用来这样做的方法是使用带有前缀的cfimport,例如.."哎呀"

<cfimport taglib="templates" prefix="ui">
<ui:tmp_pagetop heyLookAVar="#myvar# >

然后在您的模板中,您可以访问attributes.heyLookAVar

您可以非常花哨并检测模板调用标签是结束还是开始......那么你只需要一个

<ui:page>
   my page
</ui:page>

最新更新