LogoLogo
Printed BookDownload BookCFML SupportCFML Videos
  • Introduction
  • Intro
    • Welcome
      • Author
      • About This Book
    • What is ColdFusion (CFML)
    • CommandBox CLI
    • Instructions & Interpreters
  • CFML Language
    • Syntax
    • Comments
    • Variables
    • Variable Scopes
    • Operators
    • Null & Nothingness
    • Strings
    • JSON
    • Numbers
    • Arrays
    • Structures
    • Database Queries
    • Conditionals
    • Exception Management
    • Components
      • Properties
      • Functions
      • Static Constructs
      • Final Constructs
      • Abstract Constructs
      • Interfaces
    • Closures
    • Code Locking
    • Threading
    • Includes
    • Java Integration
  • Beyond The 100
    • Beyond The 100
    • Application.cfc
    • File Handling
    • Image Manipulation
    • HTTP/S Calls
    • Sending Emails
    • Asynchronous Programming
  • Extra Credit
    • MVC
    • Dependency Injection
    • Security Guide
Powered by GitBook

Support Us

  • Become a Patreon
  • Contribute
  • CFCasts

Social Media

  • LinkedIn
  • Facebook
  • Twitter

Ortus Solutions, Corp

On this page
  • A Stern Warning
  • Implementation

Was this helpful?

Edit on GitHub
  1. CFML Language

Includes

PreviousThreadingNextJava Integration

Last updated 1 year ago

Was this helpful?

If you've used other scripting environments such as PHP, or dynamic HTML, you would be familiar with the concept of server side includes. An include is a file that is embedded, or included within another file making it part of the execution; simple as that.

This can be very useful when you want multiple CFML templates to share the same block of code, scopes and visibility. In modern times, you can call this a mixin.

A typical example might be your website's header and footer, or the reuse of included functions. The ColdBox MVC framework even allows you to define mixin helper templates that can be injected at runtime in Controller objects, views, layouts and much more.

A Stern Warning

Now, even though doing includes/mixins are easy to do in CFML, let me give you a BIG WARNING. Includes are one of the most abused features in ANY language, that bring confusion and sustainability issues. Easy doesn't mean sustainable or maintainable. Do not go crazy with includes, there are many other design patterns like dependency injection and composition/aggregation that can solve reusability in much better approaches. Think about it not twice, but thrice!

Mixin : In object-oriented programming languages, a mixin is a class that contains methods for use by other classes without having to be the parent class of those other classes; No inheritance needed. -

Implementation

CFML provides the <cfinclude> tag and the include construct for including files in script - .

<cfinclude template="" runonce="true|false">

// script
include "template.cfm" runonce=true;

The template argument is a relative, absolute or CFML mapping path to the template to inject.

include template="path/to/libraries/mixins.cfm";
https://en.wikipedia.org/wiki/Mixin
https://cfdocs.org/cfinclude