DevLearningTools

🚧 This site is under active construction — new tools, guides, and pages are added every week.

MODULE 1 · LESSON 08

CFM vs. CFC

The difference between .cfm pages and .cfc components — when to create each, how a CFM page calls a CFC, and why separating presentation from logic matters.

New lessons are added one at a time as the course gets built out — a graded quiz for each lesson is still on the way.

As you start building ColdFusion applications, you'll notice two common file extensions: .cfm and .cfc. Although both contain CFML code, they serve completely different purposes.

Think of a ColdFusion application like a restaurant:

  • A .cfm file is the waiter who interacts with the customer.
  • A .cfc file is the chef who prepares the food behind the scenes.

The customer only talks to the waiter, but the waiter depends on the chef to do the real work.

The same idea applies in ColdFusion. Pages (.cfm) handle requests and display information, while Components (.cfc) contain reusable business logic.

By the end of this lesson, you'll understand when to use each file type and how they work together.

Learning Objectives

After completing this lesson, you'll be able to:

  • Understand the difference between .cfm and .cfc
  • Know when to create each file
  • Call a CFC from a CFM page
  • Organize your applications more professionally

What is a CFM File?

A CFM (ColdFusion Markup) file is a page that users access directly through a browser. Examples:

  • index.cfm
  • login.cfm
  • products.cfm
  • contact.cfm
  • dashboard.cfm

A CFM file can:

  • Display HTML
  • Receive form data
  • Execute CFML
  • Display database records
  • Call Components (CFCs)
index.cfm
Simulated output — illustrative only, not a live ColdFusion/Lucee server.
NOTE

Visit at http://localhost:8500/index.cfm — the browser can open a CFM file directly.

What is a CFC File?

A CFC (ColdFusion Component) is similar to a class in Java or C#. It contains reusable code that other pages can call. Examples:

  • User.cfc
  • Employee.cfc
  • Product.cfc
  • EmailService.cfc
  • Math.cfc

A CFC usually contains:

  • Functions
  • Business logic
  • Database operations
  • Validation
  • Utility methods

Unlike a CFM file, users normally do not open a CFC directly in a browser.

Real-Life Example

Imagine an online shopping website.

CFM Pages

pages
index.cfm
login.cfm
products.cfm
checkout.cfm
NOTE

These pages interact with users.

Components

components
User.cfc
Product.cfc
Order.cfc
Payment.cfc
NOTE

These files perform the actual work.

Visual Architecture

Browser
products.cfm
Product.cfc
Database
Product.cfc
products.cfm
Browser
NOTE

The browser never communicates directly with the database.

Browser
CFM Page
CFC
Database

Creating Your First Component

Create a file named Math.cfc:

Math.cfc
component {

    function add(required numeric a, required numeric b) {

        return a + b;

    }

}
NOTE

Notice there is no HTML. A CFC usually contains only functions.

Calling a Component from a CFM Page

Create index.cfm:

index.cfm
Simulated output — illustrative only, not a live ColdFusion/Lucee server.

Another Example

User.cfc
component {

    function getName() {

        return "Sandeep";

    }

}
index.cfm
Simulated output — illustrative only, not a live ColdFusion/Lucee server.

Comparing CFM and CFC

FeatureCFMCFC
Extension.cfm.cfc
PurposeDisplay pagesReusable code
Browser AccessYesUsually No
Contains HTMLYesUsually No
Contains FunctionsOptionalYes
Similar ToWeb PageJava Class
Used ForUIBusiness Logic

Typical Project Structure

MyProject/
MyProject/
│
├── index.cfm
├── login.cfm
├── products.cfm
│
├── components/
│      ├── User.cfc
│      ├── Product.cfc
│      ├── Order.cfc
│      └── EmailService.cfc
│
├── includes/
│      ├── header.cfm
│      └── footer.cfm
│
└── Application.cfc
NOTE

This is how many real ColdFusion projects are organized.

Why Use CFCs?

Imagine you need to calculate GST on 100 different pages. Without a CFC, every page repeats the same code:

without a CFC
index.cfm
login.cfm
checkout.cfm
invoice.cfm

With a CFC, every page simply calls:

with a CFC
tax.calculateGST()

This keeps your code:

  • Cleaner
  • Easier to maintain
  • Reusable
  • Less error-prone

Modern ColdFusion Development

Modern ColdFusion applications use CFCs extensively. Frameworks like ColdBox and FW/1 are built around components. Typical flow:

Browser
Controller (CFC)
Service (CFC)
DAO (CFC)
Database
NOTE

The CFM page mainly displays the final result.

Common Beginner Mistakes

Putting all code in one CFM file

A 1,000+ line index.cfm is a warning sign. Move business logic into a component like UserService.cfc instead, and let index.cfm just call it and display the result.

Opening a CFC directly

A component is designed to be called from other code — don't think of it as a webpage.

Mixing HTML inside components

Avoid putting lots of HTML inside CFC files. Components should focus on business logic, not presentation.

Best Practices

  • Use CFM for pages users visit.
  • Use CFC for reusable business logic.
  • Keep HTML inside CFM files.
  • Keep functions inside CFC files.
  • Reuse components instead of duplicating code.
  • Organize CFCs into folders such as components, services, and models.

Interview Questions

What is a CFM file?

A ColdFusion page that can be accessed directly through a browser.

What is a CFC?

A reusable ColdFusion Component that contains functions and business logic.

Can a CFM file call a CFC?

Yes.

Can a CFC call another CFC?

Yes.

Which one is similar to a Java class?

A CFC.

Which one contains HTML?

Usually a CFM file.

Summary

In this lesson, you learned that:

  • A CFM file represents a web page that users interact with.
  • A CFC file is a reusable component that contains business logic and functions.
  • CFM files can create and call CFCs to perform calculations, interact with databases, or execute other application logic.
  • Separating presentation (CFM) from logic (CFC) makes applications easier to maintain and is the standard approach in modern ColdFusion development.

What's Next?

Now that you know the difference between pages and components, it's time to learn how ColdFusion stores and works with data. The next lesson covers Variables — what they are, how to declare them, naming rules, a basic introduction to variable scopes, and practical examples using both CFScript and Tag Syntax.