12 Replies Latest reply on Jan 26, 2007 10:30 PM by BKBK

    Golbal UDFs

    bmcgin
      I want to create my own UDF library of functions, like isNull(x), isNotNull(x) etc and put them in lib.cfm.

      I do not want to have to include lib.cfm in every page. Furthermore I do not want to have to reference the functions as application.lib.isNull() or request.lib.isNull().

      Is there a way to create my own functions that have the same scope as the built-in CF functions such as Ucase? So they can be referenced in every cfm or cfc?

      Any performance considerations?



        • 1. Re: Golbal UDFs
          insuractive Level 3
          If you are worried about importing files on every page, you could probably do it using good old fashioned CF custom tags. Given, they would be tags instead of functions but it would give you the ability to call them on any CF page without having to include anything or scoping your methods. As for creating custom CF functions that are available universally, maybe another forum member can shed some light as to whether or not it is possible / a good idea.
          • 2. Re: Golbal UDFs
            Level 7
            I do not want to have to include lib.cfm in every page.

            Firstly:
            Even by using application.cfm|applicaiton.cfc functionality?

            Secondly:
            IIRC the cf tags and functions are defined directories under the CFIDE
            folder. I am not sure if one can extend CF by adding content to these
            locations.

            Thirdly:
            Would custom tag syntax work for you. I KNOW you can add
            global/universal custom tags to the system custom tag directory. These
            custom tags are then available to all templates on that server.

            • 3. Re: Golbal UDFs
              bmcgin Level 1
              I don't think custom tags does the trick. I would like to write code like:

              <cfif isNull(form.x) or form.x eq "123">...</cfif>

              Or use the functions inside of <cfscipt> tags.

              • 4. Re: Golbal UDFs
                bmcgin Level 1
                I am not 100% clear about adding code in the application.cfc. We are using the OnRequestStart function to set session variables.

                Could we include lib.cfc (contains all of our common UDFs) somewhere in the application.cfc? Then would all of the functions in lib.cfc be available in every cfm/cfc? I am looking for a way to not have to include the lib.cfc in every page, one or two places is fine.

                Performance has to be a consideration in whatever solution is implemented.

                • 5. Re: Golbal UDFs
                  Level 7
                  Could we include lib.cfc (contains all of our common UDFs) somewhere in
                  the application.cfc? Then would all of the functions in lib.cfc be
                  available in every cfm/cfc? I am looking for a way to not have to
                  include the lib.cfc in every page, one or two places is fine.

                  Yup, just add the <cfinclude template="myUDFLib.cfm"> line to an
                  appropriate place in your application.cfm or application.cfc file. Then
                  any udf defined in that file will be available to all templates affected
                  by above mentioned application template as local functions.

                  The relatively small affect is that all the code in the template is
                  always included. But I would think your library would have to be
                  massive or your performance requirements to be VERY tight for couple of
                  extra CPU cycles to matter.


                  • 6. Re: Golbal UDFs
                    Level 7
                    Could we include lib.cfc


                    I just noticed that your library is a cfc. You wouldn't use
                    <cfinclude...> then. I would presume that you would use the same
                    invocation logic would use in a template inside the application.cfc.
                    This does add a bit of extra thinking, but should be just as good as the
                    old fashioned <cfinclude...> we used in the days before CF had components.
                    • 7. Re: Golbal UDFs
                      Dan Bracuk Level 5
                      If you are concerned about performance, cfinclude the lib.cfm, not in every page, just the ones that use one of the functions.

                      If coding efficiency trumps performance, put your functions into a separate cfc and create an object as an application or session variable in your application.cfc file.
                      • 8. Re: Golbal UDFs
                        Level 7
                        If coding efficiency trumps performance, put your functions into a
                        separate cfc and create an object as an application or session variable
                        in your application.cfc file.


                        And if you are still adament about not wanting to call your function
                        with session.UDFLib.myFunction() or application.UDFLib.myFunction, you
                        could follow up the above suggestion with a line of code that creates a
                        local reference to the session or application library object.

                        In your onReqeustStart function a simple line like this <cfset
                        localLibObj = application.UDFlib> Will allow all templates to use
                        localLibObj.myFunction().
                        • 9. Re: Golbal UDFs
                          BKBK Adobe Community Professional & MVP
                          Use <cfimport>, <cfcomponent> and <cffunction>, object and function or custom tag. They were designed for just such a purpose. Any other alternative will either be a poor performer or a bad design.

                          • 10. Re: Golbal UDFs
                            bmcgin Level 1
                            I think I can live with Ian's solution. However I am having problems implementing it.

                            In the OnRequstStart function I added:

                            <cfset application.lib = createObject(“component”, “lib”)>
                            <cfset lib2 = application.lib>

                            Then in my test cfm:

                            <cfif application.lib,test(“x”)>…</cfif> ---- this works
                            <cfif lib2.test(“x”)>…</cfif> ---- this does not work

                            Did I put this is the wrong place?
                            • 11. Re: Golbal UDFs
                              Dan Bracuk Level 5
                              This line accomplishes nothing because you already established your object, application.lib. Just continue to use it.
                              • 12. Re: Golbal UDFs
                                BKBK Adobe Community Professional & MVP
                                In the OnRequstStart function I added:

                                <cfset application.lib = createObject(“component”, “lib”)>
                                <cfset lib2 = application.lib>

                                Then in my test cfm:

                                <cfif application.lib,test(“x”)>…</cfif> ---- this works
                                <cfif lib2.test(“x”)>…</cfif> ---- this does not work


                                For it to work, the line <cfset lib2 = application.lib> should be in test.cfm instead