All source code in ColdFusion Ask a ColdFusion Pro Discussion Forum Categories All jobs in ColdFusion
HTML,Tutorial,show,custom,page,template,will,
   Code/Articles » |  Newest/Best » |  Community » |  Jobs » |  Other » |  Goto » | 
CategoriesSearch Newest CodeCoding ContestCode of the DayAsk A ProJobsUpload
ColdFusion Stats

 Code: 9,671. lines
 Jobs: 32. postings

 How to support the site

 
Sponsored by:
Quick Search for:  in language:    
You are in:
 
Login





Latest postings for ColdFusion.
Vat Number Format Checker
By aMUSICsite on 11/18


Click here to put this ticker on your site!


Daily Code Email
To join the 'Code of the Day' Mailing List click here!



 
 
   

Tutorial: Using Custom Tags as Page Templates

Print
Email
 
article
Submitted on: 6/9/2004 3:29:33 AM
By: Tim Garver  
Level: Intermediate
User Rating: Unrated
Compatibility:Cold Fusion 4.5, Pre Cold Fusion 4.5, Cold Fusion 5, Cold Fusion MX

Users have accessed this article 12905 times.
 
author picture
(About the author)
 
     Tutorial to show how to use a custom tag as a page template. This will help manage static HTML that rarly changes.

 
 
Terms of Agreement:   
By using this article, you agree to the following terms...   
1) You may use this article in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.   
2) You MAY NOT redistribute this article (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
3) You may link to this article from another website, but ONLY if it is not wrapped in a frame. 
4) You will abide by any additional copyright restrictions which the author may have placed in the article or article's description.
Tutorial to show how to use a custom tag. Replace (<) with "<". PSP will not allow some tags in the uploads. Consider this HTML as an example:
    (<)html>
    (<)head>(<)title>Page Title (<)/title>
    (<)/head>
    (<)body>
    (<)table width="800">
    (<)tr>
    (<)!--- Header --->
    (<)td>
Header goes here(<)/td> (<)/tr> (<)tr> (<)td>Menu or navigation goes here(<)/td> (<)/tr> (<)tr> (<)td>!! Dynamic Content will go here !!(<)/td> (<)/tr> (<)tr> (<)td>Some footer goes here(<)/td> (<)/tr> (<)/table> (<)/body>(<)/html>
Its pretty straight forward for those who know HTML. Now consider the above example spread across 50 to 200 cfm files. When you want to change the back ground color of all your pages, then you have a lot of work on your hands. With Cold Fusion, you create one custom tag that will hold all that static HTML. Then take your existing pages, and delete all the same HTML and wrap your code in the custom tag. I know, I know, thats just as much work as change the back ground color on all your pages. But once you have your template in place, you can add or change anything about it making your change reflect across all your pages that use it. If we take the above code and chop it into three parts, you will have the start of the template, the body, and the end of the template. In the above code, we are only concerned with the part that says, !!!Dynamic Content !!!… The rest of the code will not change. Thats our template. Lets take the above code and turn it into a custom tag we can wrap around our dynamic code. Create a new file called template.cfm Add this section of code to the very top of your template page.
    -------
    (<)cfif ISDEFINED("thistag.executionmode") IS False>
    (<)cfset thistag.executionmode = "start">
    (<)/cfif>
    (<)cfif thistag.executionmode is 'start'>
    -------
    
These calls are using the “ThisTag.ExecutionMode” variable. When a tag is called, the ExecutionMode is “Start” when it ends its “End”. To call your tag, you would use something like this: (<)cf_filename> In our situation we will be using the end tag too, (<)/cf_filename> To make your template truly work, you have to think about every aspect of your static page that changes with each new page. Like the title, or calling javascript functions with the body onLoad, or similar events. To handle this all you have to do is give your template some attributes. Like the following:
    -----
    (<)cfset pagetitle = "RINO Information Systems. - Blank Template Page">
    (<)cfparam name="attributes.doctitle" default="#pagetitle#">
    -----
    
We set a default page title variable called “pagetitle”. Then we make a few params that hold our template attributes one of which is called “attributes.doctitle” its just a name I choose, you could use any name you want, but it has to have the “Attributes” scope on it. Now I went ahead and created 3 body tag event attributes.
    -----
    (<)cfparam name="attributes.onload" default="">
    (<)cfparam name="attributes.onfocus" default="">
    (<)cfparam name="attributes.onresize" default="">
    -----
    
Now I set my page title with my passed attribute value or the default.
    -----
    (<)cfoutput>
    (<)title>#attributes.doctitle#(<)/title>
    (<)/cfoutput>
    -----
    
You can assign any number of attributes. And to call them inside of your template page, just use the #attributes.name#. Next is our body tag. Again I could have created a background color attribute but this is just an example. I choose to pass the entire body tag attributes and values into my template attribute for my body events, meaning the variable
    -----
    #attributes.onload#
    -----
    would contain the text:
    -----
    “onLoad=’MyFunction()’;”
    -----
    (if I passed it one)
    You could just use the function name instead.
    -----
    (<)body bgcolor="##FFFFFF" #attributes.onload# #attributes.onfocus# #attributes.onresize#>
    -----
    
But if you don’t pass the attribute, then your body tag would contain
(<)body onLoad=’’>
. So I choose the other method. The rest is very straight forward. From the above code what we have left is the following:
    -------------------
    (<)table>
    (<)tr>
    (<)!--- Header --->
    (<)td>Header goes here, maybe a pic or something(<)/td>
    (<)/tr>
    (<)tr>
    (<)td>Menu or navigation goes here(<)/td>
    (<)/tr>
    (<)tr>
    (<)td>
    	 <--- Content will go here --->	 
    	 <--- End Content --->
    (<)cfelse>
    	(<)/td>
    (<)/tr>
    (<)tr>
    (<)td>Some footer goes here(<)/td>
    (<)/tr>
    (<)/table>
    (<)/body>
    (<)/html>
    (<)/cfif>
    -------------------
    
Notice the
    (<)cfelse>
? Save your file as template.cfm, copy the above and finish it out. Now, to wrap your template around your dynamic content, all you have to do is: make a new file called NewFile.cfm:
    -----
    (<)cf_template doctitle=”My Page Title”>
    Here is my dymanic content!!!
    This part of the content will show up just after the <--- (<)cfelse> ---> tag above.
    (<)/cf_template>
    ---------
    
Paste the above into your new file with NO OTHER Tags. Thats It! you now have a static site wide template that you can modify once and the changes will be site wide. Pretty simple huh? Your template can also contain other custom tags. Here is all the code together for the template.cfm page:
    (<)cfif ISDEFINED("thistag.executionmode") IS False>
    	(<)cfset thistag.executionmode = "start">
    (<)/cfif>
    (<)cfif thistag.executionmode is 'start'>
    (<)html>
    (<)head>
    (<)cfset pagetitle = "RINO Information Systems. - Blank Template Page">
    (<)cfparam name="attributes.doctitle" default="#pagetitle#">
    (<)cfparam name="attributes.onload" default="">
    (<)cfoutput>
    (<)title>#attributes.doctitle#(<)/title>
    (
    (<)/head>
    (<)cfoutput>
    (<)body #attributes.onload#>
    (<)/cfoutput>
    (<)table width="800">
    (<)tr>
    (<)!--- Header --->
    (<)td>Header goes here, maybe a pic or something(<)/td>
    (<)/tr>
    (<)tr>
    (<)td>Menu or navigation goes here(<)/td>
    (<)/tr>
    (<)tr>
    (<)td>
    	 (<)!--- Content will go here --->
    	 
    	 (<)!--- End Content --->
    (<)cfelse>
    	(<)/td>
    (<)/tr>
    (<)tr>
    (<)td>Some footer goes here(<)/td>
    (<)/tr>
    (<)/table>
    (<)/body>
    (<)/html>
    (<)/cfif>
    --------------
    
And to call your template you would use the following:
    (<)cf_template doctitle“My Page Title”>
    	Page content would go here
    (<)/cf_template>
    
The tag itself can be broken down to the following: 1) “(<)cf_” donotes the tag as custom and calls "filename" .cfm 2) “(<)cf_” + filename (without DOT.CFM) or your custom tag. 3) “(<)cf_” + FileName + attributes(Values) + “>” attribute names and their values 4) “(<)/cf_” + FileName + “>” Used to wrap your tag around other content. You can use your custom tag in the following way as well:
    custom tag myTag.cfm:
    (<)cfoutput>#Now()#(<)/cfoutput>
    AnyOtherFile.cfm
    (<)cf_myTag/>
Would output todays date in every file that the myTag custom tag is called from. Since the myTag.cfm file will not need execution mode processing and will not use the “End Tag” then we can just plop it any where on any page even in other custom tags. You can even set variable values in the calling pages from inside your custom tags.
    in template.cfm:
    (<)cfset caller.MyName = "Tim">
in the calling page: (<)cf_template> (<)cfparam name="MyName" default=""> (<)cfoutput>#myName#(<)/cfoutput> would output "Tim" (<)/cf_template>
I use custom tags to write every possible template for my sites. You may only need one, or you might need 100. The point is to “Reuse Code”, and this saves you LOTS of time and head aches later on. If you found this interesting and helpfull or even usefull, please vote and comment if you would like. Thankx


Other 11 submission(s) by this author

 

 
 Report Bad Submission
Use this form to notify us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:
 
Your Vote!

What do you think of this article(in the Intermediate category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor See Voting Log
 
Other User Comments

 There are no comments on this submission.
 
Add Your Feedback!
Note:Not only will your feedback be posted, but an email will be sent to the code's author from the email account you registered on the site, so you can correspond directly.

NOTICE: The author of this article has been kind enough to share it with you.  If you have a criticism, please state it politely or it will be deleted.

For feedback not related to this particular article, please click here.
 
To post feedback, first please login.


 

Categories | Articles and Tutorials | Advanced Search | Recommended Reading | Upload | Newest Code | Code of the Month | Code of the Day | All Time Hall of Fame | Coding Contest | Search for a job | Post a Job | Ask a Pro Discussion Forum | Live Chat | Games | Feedback | Customize | ColdFusion Home | Site Home | Other Sites | Open Letter from Moderators | About the Site | Feedback | Link to the Site | Awards | Advertising | Privacy

Copyright© 1997-2009 by Exhedra Solutions, Inc. All Rights Reserved.  By using this site you agree to its Terms and Conditions.   Planet Source Code (tm) and the phrase "Dream It. Code It" (tm) are trademarks of Exhedra Solutions, Inc.