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)
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
index.cfm login.cfm products.cfm checkout.cfm
These pages interact with users.
Components
User.cfc Product.cfc Order.cfc Payment.cfc
These files perform the actual work.
Visual Architecture
The browser never communicates directly with the database.
Creating Your First Component
Create a file named Math.cfc:
component {
function add(required numeric a, required numeric b) {
return a + b;
}
}Notice there is no HTML. A CFC usually contains only functions.
Calling a Component from a CFM Page
Create index.cfm:
Another Example
component {
function getName() {
return "Sandeep";
}
}Comparing CFM and CFC
| Feature | CFM | CFC |
|---|---|---|
| Extension | .cfm | .cfc |
| Purpose | Display pages | Reusable code |
| Browser Access | Yes | Usually No |
| Contains HTML | Yes | Usually No |
| Contains Functions | Optional | Yes |
| Similar To | Web Page | Java Class |
| Used For | UI | Business Logic |
Typical Project Structure
MyProject/ │ ├── index.cfm ├── login.cfm ├── products.cfm │ ├── components/ │ ├── User.cfc │ ├── Product.cfc │ ├── Order.cfc │ └── EmailService.cfc │ ├── includes/ │ ├── header.cfm │ └── footer.cfm │ └── Application.cfc
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:
index.cfm login.cfm checkout.cfm invoice.cfm
With a CFC, every page simply calls:
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:
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.