ColdFusion Tutorial

Using cfinclude helps reusing and maintain text or code.

The <cfinclude> tag include a template into your page.

Using <cfinclude> helps reusing and maintain text or code.

When using cfinclude you can have a nice header and footer on every page. Because you don?t have to type the code in it again and again, you can be sure that the consistency of your pages won?t suffer from the extra code.

Syntax:
<cfinclude template = ?template_name.cfm?>


The contains of the template will be inserted where the <cfinclude> tag are used.

As an example, lets say that every page should have the same text at end of each page.

First make a template, let's call it bottom.cfm.

<div align="center"><font size="2" face="Verdana, Arial, Helvetica, sans-serif">Copyright Site Name - All Rights Reserved.</font>
</div>


At the end of your pages enter this code:

?.
<cfinclude template = "bottom.cfm">
</body>
</html>

Every page that you have included the bottom template on, will have the text you entered in the bottom template. Changing the text or add an image in the bottom template, will automatically being updated on those pages. So by using the <cfinclude> tage you only have to change the code in the included template to have the changes updated on all your pages.

See this tutorial for an example.

Notice: be careful using the html tags below in your templates, so you don't duplicate those tags on your pages.

<html>

<head>

<title>..</title>

</head>

<body>

</body>

</html>

 

Page layout and cfinclude.

So far we are using cfinclude on a page without tables. Let?s imagine that your site layout is something like this:
 

Page top

Navigation menu

Page contains

Page bottom

 
Ok, let?s make five templates:
One for the main page that is the site layout, we can call this main_page.cfm.
The rest of our templates we include in the main_page.cfm.
 
Page top template [page_top.cfm]

<!--- Template: page_top.cfm --->

<div align="center"><strong>Logo</strong> [page_top template]</div>

 
Navigation menu template [navigation_menu.cfm]

<!--- Template: navigation_menu.cfm --->

<strong>Navigation menu</strong><br>

[Link1]<br>

[link2]<br><br>

[navigation_menu template]

 
Page contains template [page_contains.cfm]

<!--- Template: page_contains.cfm --->

<div align="center">This is just an example of using cfinclude into tables, so we easier can have a common layout site layout and also easier maintain and reuse our code.<br>

<br>

[page_contains template]

</div>

 
Page bottom template [page_bottom.cfm]

<!--- Template: page_bottom.cfm --->

<div align="center">CopyRight&copy; Your site.<br>

[page_bottom template]

</div>

 
Main Page template [main_page.cfm]

<!--- Template: main_page.cfm --->

<table border="1" width="100%" cellspacing="0" cellpadding="3" style="border-collapse: collapse" bordercolor="#000000" bgcolor="#FFFFE7">

<tr>

<td colspan="2"><!--- Include template: page_top.cfm --->

<cfinclude template="page_top.cfm"></td>

</tr>

<tr>

<td width="180"><!--- Include template: navigation_menu.cfm --->

<cfinclude template="navigation_menu.cfm"></td>

<td width="100%"><!--- Include template: page_contains.cfm --->

<cfinclude template="page_contains.cfm"></td>

</tr>

<tr>

<td colspan="2"><!--- Include template: page_bottom.cfm --->

<cfinclude template="page_bottom.cfm"></td>

</tr>

</table>

 
The index.cfm file [that starts everything]

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

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

<title>Site Name</title>

</head>

<body>

<!--- Include template: main_page.cfm --->

<cfinclude template="main_page.cfm">

</body>

</html>

 
Now, run the index.cfm file and if everything is correct you will get the following output in your browser.

Logo [page_top template]

Navigation menu

 [Link1]

 [link2]

 [navigation_menu template]

This is just an example of using cfinclude into tables, so we easier can have a common layout site layout and also easier maintain and reuse our code.

[page_contains template]

CopyRight? Your site. [page_bottom template]

 

ColdFusion Tutorial test

Using cfinclude helps reusing and maintain text or code.

No test is available or implemented for this ColdFusion Tutorial.

No User Comments.

No User Comments, be the first one to write your comments?

Add your comments

Your Name*:
Your Email:
Site URL:
Site Name:
Comemt Title*:
Your Comment*:
Key Phrase*:
0Qu
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