ColdFusion Tutorial

Reuse code with CFINCLUDE

Certain components, such as page header, page footer, and database queries will often be used on more than one page. Using the cfinclude tag to include code snippet on your page, will make it easier to maintain your code.
Example: when you have to change the footer text for your site, you can do this on one page (template) and get the change reflected on every page. You don't need to edit every page by hand or rely on cryptic find-and-replace functions.
As a general rule, use cfinclude when you use the same code on more than one page. And create a directory structure named includes, with subdirectories. Example: includes/layouts, includes/queries, includes/security...

You can also add more power to your cfinclude statements, by combined cfinclude with conditional code such as cfif and cfcase.

In this example we build a header file and a footer file and using cfinclude to include to code on the 'output page'.

First make your directory structure, and then start with the header.cfm and the footer.cfm files.

Then we include those files to our main mage.

 

The header.cfm file:

 

<html>

<head>

<title>Your Title</title>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

<meta name="Language" content="en">

<meta name="description" content="Site Description">

<meta name="keywords" content="primary_keyword, secondary one, secondary two, secondary three, SECONDARY ONE, SECONDARY TWO, SECONDARY">

<meta name="robots" content="INDEX,FOLLOW">

</head>

<body bgcolor="#008080">

The footer.cfm file:

 

<br>

<div align="center"><font size="2" face="Verdana, Arial, Helvetica, sans-serif">Copyright &copy; 19xx - <cfoutput>#DatePart("yyyy", Now())#</cfoutput>. All Rights Reserved.</font>

</div>

</body>

</html>

The main page.cfm:

 

<!--- Include the header template. --->

<cfinclude template="includes/layouts/header.cfm">

<!--- Start of page contains. --->

<font size="3"><strong>The standard example output: Hello world.</strong></font>

<!--- End of page contains. --->

<!--- Include the footer template. --->

<cfinclude template="includes/layouts/footer.cfm">

This will give a output like this:

 

The standard example output: Hello world.

Copyright ? 19xx - 2006 Site Name. All Rights Reserved.

ColdFusion Tutorial test

Reuse code with CFINCLUDE

No test is available or implemented for this ColdFusion Tutorial.

User Comments: 2

 Answer: How to passing parameter value through <CFINCLUDE> in this tutorial.

In the output file enter this line at the top.

<cfset page_name = "My Page Title">

In the header.cfm file:
Change the code for the <title>..</tile> tags like this. (Use the method for description and meta).

<title><cfoutput>#page_name#</cfoutput></title>

 Problem With Passing Parameter Value through <CFINCLUDE>

This is a great example.  But suppose you have a variable for the page title where header.cfm will be called as follows:
<cfset pagetitle = "My Page Title">

How would you pass the value of the vairable to header.cfm by using <cfinclude>?
I tried to add <cfoutput>#pagetitle#</cfoutput> in header.cfm.  ColdFusion couldn't determine the parameter value this way.  Where did I miss?  Are there other approaches that will work in this situation?

Add your comments

Your Name*:
Your Email:
Site URL:
Site Name:
Comemt Title*:
Your Comment*:
Key Phrase*:
Sn6
Your IP Address: 38.103.63.62
 

ScandicWeb is own by Scandic Systems LTD [UK] Company No. 5984000. All other trademarks and copyrights are the property of their respective holders.

[Home [Site Map [Privacy Statement [Site Feedback] [About ScandicWeb]


[ColdFusion [Flash [Search Engine Optimization [Graphics [Cascading Style Sheets (CSS) [JavaScript (JS) [Software]

Partner sites: ScandicSoft


Server time: 19. November 2008 Wednesday