• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Golbal UDFs

Guest
Jan 26, 2007 Jan 26, 2007

Copy link to clipboard

Copied

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?



TOPICS
Advanced techniques

Views

908

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Jan 26, 2007 Jan 26, 2007

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Jan 26, 2007 Jan 26, 2007

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 26, 2007 Jan 26, 2007

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jan 26, 2007 Jan 26, 2007

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Jan 26, 2007 Jan 26, 2007

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jan 26, 2007 Jan 26, 2007

Copy link to clipboard

Copied

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.


Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jan 26, 2007 Jan 26, 2007

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jan 26, 2007 Jan 26, 2007

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jan 26, 2007 Jan 26, 2007

Copy link to clipboard

Copied

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().

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Jan 26, 2007 Jan 26, 2007

Copy link to clipboard

Copied

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?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jan 26, 2007 Jan 26, 2007

Copy link to clipboard

Copied

This line accomplishes nothing because you already established your object, application.lib. Just continue to use it.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 26, 2007 Jan 26, 2007

Copy link to clipboard

Copied

LATEST
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


Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Documentation