The Dopefly Tech Blog

<< The Dopefly Tech Blog Main page

12 ways to reuse your ColdFusion code

posted under category: ColdFusion on April 8, 2010 at 1:00 am by MrNate

It's no doubt that code reuse is an important part of programming. Here's a list of ways you can do it while you're programming in ColdFusion.

  1. <cfinclude template="file.cfm">
    This is the most basic server-side include, which pulls one file into another. There is no way to send parameters to the included file (besides setting variables before the include). The included template can by dynamic using a #variable#. Unlike classic ASP, the file is included only when the tag executes.

  2. include "file.cfm"
    This is the cfscript version of the cfinclude tag and behaves similarly.

  3. <cf_file attribute1="value1">
    This is Custom Tag syntax. You can use this to send parameters at an included file. In this example, file.cfm exists either in the relative current directory, in a custom tag path specified by your Application.cfc file, or in a custom tag path specified in the CF Administrator.

  4. <cfmodule [name= or template=] >
    Similar to the custom tag model, but you can use any file with a relative path.

  5. <cfimport namespace="imported"> ... <imported:file />
    Specify a directory and namespace, and you can use this JSP style include. The cfimport tag must be applied on every template you wish to use the namespaced file set in. Imported files behave like custom tags in that they use attributes and can have a closing tag, but it can also be used for JSP tagsets.

  6. <cffunction name="reusable"></cffunction>
    Defines a reusable function, method or subroutine of your application. You can type this anywhere in your application, except within another function.

  7. function reusable() {}
    This is the cfscript version of cffunction. Since CF9, it can use extra syntax to give more metadata and control like the tag-based version has.

  8. <cfcomponent> ... </cfcomponent>
    This is the root element of a .cfc file. Any code within is executed immediately upon instantiation (see cfobject). The methods (functions) within are executed upon request.

  9. component { ... }
    This is the cfscript version of defining a CFC. This also provides the only way to write cfscript without the cfscript tags.

  10. <cfobject >
    Creates an instance of (a.k.a. "instantiates") an object, which could be Java, .NET, a web service, or a variety of others, but in this case, we are talking about creating an instance of a ColdFusion Component (CFC) or ColdFusion web service.

  11. createObject("component", "myComponent")
    new myComponent()

    cfscript versions of cfobject and traditionally the easiest way to create an instance of any CFC.

  12. <cfinvoke>
    Invokes a method on an object or webservice, instantiated or not.

That's a big list!

Too old to comment!
On Apr 8, 2010 at 1:00 AM jason dean (http://www.12robots.com) said:
I believe the second one should be

include "file.cfm"

It does not work like a function call.

On Apr 8, 2010 at 1:00 AM Nathan Strutz (http://www.dopefly.com/) said:
Thanks Jason, I don't know what I was thinking :)

On Apr 12, 2010 at 1:00 AM Allen (allen who can't believe it's not allenweb.com) said:
There is CFImport too; that'll let you treat the included code like a custom tag.

On Apr 12, 2010 at 1:00 AM Nathan Strutz (http://www.dopefly.com/) said:
Allen, CFImport is in there, it's #5. I know what you're thinking though, everyone always leaves it out, cfimport gets no love. Honestly, I've only seen it used once or twice in my life.

On Apr 12, 2010 at 1:00 AM Allen (allen who hates allenweb.com) said:
DOH! You're too kind. I wasn't thinking... well, no, I was thinking just not reading. :)

Once I got to see CFImport used, I really liked it. Very handy for the view.
Too old to comment!