View comments | RSS feed
Contents > CFML Reference > ColdFusion Tags > cfinclude PreviousNext

cfinclude

Embeds references to ColdFusion pages in CFML. You can embed cfinclude tags recursively. For another way to encapsulate CFML, see cfmodule. (A ColdFusion page was formerly sometimes called a ColdFusion template or a template.)

Flow-control tags, Page processing tags

<cfinclude 
template = "template_name">

cfcache, cfflush, cfheader, cfhtmlhead, cfsetting, cfsilent

ColdFusion MX: Changed error behavior: if you use this tag to include a CFML page whose length is zero bytes, you do not get an error.

Attribute

Req/Opt

Default

Description

template

Required

 

A logical path to a ColdFusion page.

ColdFusion searches for included files in the following sequence:

  1. In the directory of the current page
  2. In directories mapped in the ColdFusion Administrator for the included file

The included file must be a syntactically correct and complete CFML page. For example, to output data from within the included page, you must have a cfoutput tag, including the end tag, on the included page, not the referring page. Similarly, you cannot span a cfif tag across the referring page and the included page; it must be complete within the included page.

<!--- This example shows the use of cfinclude to paste CFML 
or HTML code into another page dynamically ---> <h4>This example includes the main.htm page from the CFDOCS directory.
The images do not display, because they are located in
a separate directory. However, the page appears fully rendered
within the contents of this page.</h4> <cfinclude template = "/cfdocs/main.htm">

Contents > CFML Reference > ColdFusion Tags > cfinclude PreviousNext

ColdFusion 8 | ColdFusion MX 7 | ColdFusion MX 6.1 | ColdFusion MX | Forums | Developer Center | Bug Reporting

Version 6.1

Comments are no longer accepted for ColdFusion MX 6.1. ColdFusion 8 is the current version.

Comments


mydognacho said on Apr 26, 2004 at 9:20 AM :
It appears that cfinclude can't use variables for the template attribute. I've tried calling the variable normally:
<cfinclude template="templ">

I've also tried using CFOUTPUT like so:
<cfinclude template="<cfoutput>templ</cfoutput>">

and like so:
<cfoutput>
<cfinclude template="templ">
</cfoutput>

and nothing. Anyone have any ideas?
Promila said on May 5, 2004 at 11:56 PM :
Basically <cfinclude> simply includes the page that is being called and cannot call any variable
ASandstrom said on May 6, 2004 at 8:40 AM :
You might try something like the following:
<cfset templatetouse="header.cfm">
<cfinclude template="#templatetouse#">

Just don't forget the pound signs.
No screen name said on Jul 13, 2004 at 2:02 PM :
when <cfinclude> in used in <cfoutput>, the varibles in the include file are displayed as their text varible names with #'s and all, rather than being replaced with there values. For example the following include does not work.

<cfoutput>
<cfinclude template="outputfile.cfm">
</cfoutput>

where outputfile.cfm contains:
<b>#url.class#</b>

in ie this displays:
#url.class# (not the value of the varible)


Anyone have a solution?

thanks,

Ian
profit11 said on Jul 23, 2004 at 2:47 PM :
The next version of this documentation should include content that addresses Ian's question, namely that if CFINCLUDE is to contain variables that need to be outputted, then the CFOUTPUT tags should also be in the include, not in the parent template.
BlueSpline said on Sep 17, 2004 at 1:49 PM :
Cfinclude works fine with realtive pathnames such as './'. However, I can't make it work with absolute pathnames. For example:

<cfset TemplatePath = 'http://localhost/inetpub/website/Login.cfm'>
<cfinclude template="#TemplatePath#">

And

<cfset TemplatePath = 'C:/inetpub/wwwroot/website/Login.cfm'>
<cfinclude template="#TemplatePath#">

And even

<cfset TemplatePath = 'C:\inetpub\wwwroot\websiteogin.cfm'>
<cfinclude template="#templatepath#">

all fail with:

"the filename, directory name, or volume label syntax is incorrect"

one of the main benefits from using an absolute pathname is that you don't have to know where cfinclude is called from.

maybe cfinclude can only load files from the current directory. haven't verified this.

mike
hall said on sep 17, 2004 at 2:23 pm :
the reference page does not provide sufficient information on valid paths. you cannot use an absolute url or file system path. you can only use paths relative to including page's directory or a directory that is registered in the coldfusion mx administrator mappings. therefore, any of the following will work, assuming the myinclude.cfm file exists in the relevant directory:

<cfinclude template="myinclude.cfm">
<cfinclude template="../myinclude.cfm">
<cfinclude template="/cfide/debug/myinclude.cfm">

but these will not work:

<cfinclude template="c:\cfusionmx\wwwroot\doccomments\myinclude.cfm">
<cfinclude template="http://localhost:8500/doccomments/myinclude.cfm">

this not considered a product bug, but the failure to explain this is a documentation bug.
themex said on oct 1, 2004 at 1:36 pm :
one thing worth noting is that if you copied the path from anywhere make sure that you have trimmed of any spaces from either side before saving the mapping. after a few hours wondering why it did not work we figured out that upon saving the mapping we had a space on the end but it looked correct.
jedimatt said on nov 2, 2004 at 6:27 am :
i have had sucess with the following:

wanted to set an includes dir path in application.cfm and then call this as a variable in an cfinclude url.

did the following:

in application.cfm set the following;

<!--- include directory path --->
<cfset includepath = "includes/" >


in the index.cfm page did the following:

<!--- include the page hjeader from the includes dir --->
<cfinclude template="#includepath#header.cfm">

<!--- include the page footer from the includes dir --->
<cfinclude template="#includepath#footer.cfm">

this then allows me to control the include directory path from application.cfm .
no screen name said on jan 23, 2005 at 11:52 pm :
what is you don't want to use relative paths and you don't have access to your admin console. how do you consistently include templates?

is there any "document root" variable, like in php, so you can do something like:
<cfinclude template="#document_root#/includes/header.cfm" />
roughly equivalent to php where you can do:
require_once $_server['document_root'].'/includes/header.php';

?????
no screen name said on jan 25, 2005 at 6:16 am :
in cf4.5 it worked like that:
main.cfm:
<cfset xx=1>
<cfinclude template="sub.cfm">

sub.cfm:
<cfoutput> xx = #xx# </cfoutput>

output:
xx = 1

trying this under cfmx results in :
"coldfusion.runtime.undefinedvariableexception: variable xx is undefined."

but why and how can i solve this?

thanx
rob
robert10 said on mar 7, 2005 at 7:20 am :
you can not pass url variables to the template.

for example:

<cfinclude template="file.cfm?myparam=12">

will result in the following error message:
the system cannot find the file specified
bclear said on apr 8, 2005 at 4:37 am :
if you are using dreamweaver then make sure to close the cfinclude with /> and not > or the page only display properly in the design view.
so use
<cfinclude template="afile.cfm"/>
and not
<cfinclude template="afile.cfm">

if you leave out the / the design view tries to display the included file.
grahambarron said on apr 22, 2005 at 10:11 am :
re bclear & dreamweaver - this also solved a problem where dreamweaver was dropping all my code after the cfinclude.
chwagssd said on aug 4, 2005 at 11:25 am :
themex , you are a stud. i read your advice to make sure to remove the spaces from before the directory in absolute mappings if you copied it from somewhere and pasted it.

why is it that programmers like me always think it must be something more complicated. thank you...
dmerrill said on aug 27, 2005 at 1:36 pm :
there appears to be some strange behavior when you use cfinclude inside a udf or component method. doing so causes the internal variable space of the function to become public i.e., all its variables become visible outside the function. this includes function arguments and variables declared with var.
no screen name said on sep 14, 2005 at 10:58 am :
in response to robert10's problem of passing on variables, try this:
before the <cfinclude> statement set the values of url.variable
for ex/
instead of:
<cfinclude template="mypage.cfm?var=value">
try:
<cfset url.var = "value">
<cfinclude template="mypage.cfm">
no screen name said on oct 27, 2006 at 12:13 pm :
in response to the problem of passing on variables, try this:
instead of:
<cfinclude template="mypage.cfm?myparam=12">
try:
<cfset myparam = "12">
<cfinclude template="mypage.cfm">

so you can use the value of myparam in the page mypage.cfm

 

rss feed | send me an e-mail when comments are added to this page | comment report

current page: http://livedocs.adobe.com/coldfusion/6.1/htmldocs/tags-p63.htm