Skip to main content
Participant
Jul 21, 2021 7:39 PM UTC
Answered

How ColdFusion framework ONE making view function available in views

I am wondering how framework one (fw1) making `view()` function available in views , like ColdFusion in built functions?.

 

For example, in the below code , the `view()` function can be called like any ColdFusion in built functions. How fw1 making it possible?.

 

#view(‘components/contact’)#

 

The function `view()` has been defined in /framework/one.cfc

    This topic is closed to new replies. Start a new post to keep the conversation going.
    BKBK
    BKBKCorrect answer
    Community Expert
    Jul 26, 2021 5:05 PM UTC

    But the view function is not application scoped in one.cfc right? And since it is not application scoped , how it is accessible in cfm pages?


    I shall repeat what I said:

    1. store the function as a variable in application scope, it will be available in all CFM pages of the application.
    2. test the example I gave you, and you will see it for yourself.

     

    Some more code examples, in case you need help to set up a test:

    TestComponent.cfc

     

    <cfcomponent>
       <cffunction name="init" output="false">
            <cfset application.globalView = variables.view>
        </cffunction>
    
        <cffunction name="view" output="false">
    		<cfargument name="callerName">
            <cfreturn "Hi, " & arguments.callerName & "!">      
        </cffunction> 
    </cfcomponent>

     

     

    testcode1.cfm

     

    <!---
    CFM page is in same folder as the component.
    After this line runs, the function View() will be
    universally available throughout the application.
    --->
    <cfset testObject = new TestComponent()>

     

     

    testcode2.cfm

     

    <!--- 
    Run this page somewhere else in the application,
    some minutes later.
    --->
    <cfoutput>#application.globalView("Abdul L Koyappayil")#</cfoutput>
    

     

     

     

    6 replies

    BKBK
    Community Expert
    Community Expert
    Jul 24, 2021 8:56 PM UTC

    Yes, you can do that in ColdFusion. Irrespective of whether you're using Framework One or not.

     

    You may store a function, just like any other variable, in application scope. It can then be called from any page in the application.

     

    For example, define the following in one of the CFM pages in Views:

    <cfscript>
    	public string function myView(string caller) {
    		return "Hello, #arguments.caller# - go view the World!"
    	}
    	application.view=myview;	
    </cfscript>

     

    Now, if you run 

    <cfoutput>#application.view('BKBK')#</cfoutput>

    anywhere in the application, you will get:

          Hello, BKBK - go view the World!

     

    Translate
    Inspiring
    Jul 26, 2021 9:19 AM UTC

    I think you haven't answer to the question. As I mentioned, view function has been defined in /framework/one.cfc. Still it is available in cfm pages. How it is available in all cfm pages?

    Translate
    BKBK
    Community Expert
    Community Expert
    Jul 26, 2021 10:30 AM UTC
    quote

     How it is available in all cfm pages?


    By @Abdul L Koyappayil

     

    If you store the function as a variable in application scope, it will be available in all CFM pages of the application. Test the example I gave you, and you will see it for yourself.

    Translate