Skip to main content
Participant
July 21, 2021
Answered

How ColdFusion framework ONE making view function available in views

  • July 21, 2021
  • 1 reply
  • 704 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 has been closed for replies.
    Correct answer BKBK

    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>
    

     

     

     

    1 reply

    BKBK
    Community Expert
    Community Expert
    July 24, 2021

    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!

     

    Inspiring
    July 26, 2021

    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?

    BKBK
    Community Expert
    Community Expert
    July 26, 2021
    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.