A quick intro into the world of Custom Tags!
What is a custom tag and why do i need it?
This is a question I get a lot when I bring up
custom tags. A custom tag is a piece of code that you create and you call it
from within your ColdFusion applications. Think of it as a "SNIPPLET"
that you can implement at any time, anywhere in your site. What can you do with
a custom tag? Well, just about anything!
In this tutorial I will demonstrate how you can
use a custom tag to add a list of states (in a drop down select box) in a
form.
Let's see how you call a custom tag. The
easiest (and probably most used) method is: <cf_states>.
What does that mean?
Ok, you are simply telling ColdFusion to look in the same folder (as the file
you are accessing to call the custom tag) or in the custom tags folder (Usually
C:\CFusion\CustomTags\) for a file called: states.cfm. If it finds that file,
you are saying to the server, execute it, process it and send me the results of
it. So it's like an include, but with the difference that you can do other neat
things! I'll explain more below.
You can pass values to the custom tag, as follows:
<cf_states DefaultState="TX"
FieldName="States">
This basically says that when executing the
custom tag "states.cfm" the default state I want selected is "TX
- Texas". Of course you'll have to write the code that will select the
stat, but it's basically allowing you to tell the code in the
"states.cfm" file that that is the default state. Let's create the
actual states.cfm file so you can see how it works. Notice also that I'm passing
a value called "FieldName" this will become the name of the form field
that will be generated. That ay when you process the page in the end you'll know
what name to reference.
(Note: Since you are
specifying a variable for the custom tag called "DefaultState", within
the custom tag, this variable is referenced as follows:
"Attributes.DefaultState". All variables you pass when calling the tag
(i.e. one="" two="") will always be
referenced within the custom tag as "Attributes.[name of variable]".)
<!--- States.cfm
custom tag --->
<!--- Make sure that user specified a name for the form field --->
<cfif NOT IsDefined("Attributes.FieldName")>
<!--- No Name Was Specified, stop
custom tag and ask that user specify field --->
<font color="red" size="4"
face="Verdana">You did not specify a valid name for the select
field!</font>
<cfabort>
</cfif>
<!--- define a variable called
"Attributes.DefaultState" to be Arizona --->
<cfif NOT IsDefined("Attributes.DefaultState")>
<!--- No Default State Was Define,
Set Default To Be Arizona --->
<cfset Attributes.DefaultState = "AZ">
</cfif>
<!--- Now Create the Select Box With The States --->
<cfoutput>
<select name="#Attributes.FieldName#">
<option
value="AZ"<cfif
Attributes.DefaultState eq "AZ">
SELECTED</CFIF>>AZ
- Arizona</option>
<option value="CA"<cfif
Attributes.DefaultState eq "CA">
SELECTED</CFIF>>CA
- California</option>
<option value="MS"<cfif
Attributes.DefaultState eq "MS">
SELECTED</CFIF>>MS
- Mississippi</option>
<option value="NJ"<cfif
Attributes.DefaultState eq "NJ">
SELECTED</CFIF>>NJ
- New Jersey</option>
<option value="NY"<cfif
Attributes.DefaultState eq "NY">
SELECTED</CFIF>>NY
- New York</option>
<option value="TX"<cfif
Attributes.DefaultState eq "TX">
SELECTED</CFIF>>TX
- Texas</option>
</select>
</cfoutput>
OK, so what was the purpose of this custom tag?
Well imagine that you are building 30 web sites for 30 different customers that
all ask for users information. So now with this custom tag you can simply type
in:
<cf_states DefaultState="TX"
FieldName="States">
So for customer one you created an
automatic states drop down in the form on the fly.
Now say your second customer likes that the
database values be written as tblflStates and the default state for this
customer has to be New York. So in the "FieldName" you can no longer
use "states". If you hadn't used a custom tag you'd have to recreate
the file, use unnecessary space and waste time with something you could of
already have had done. So for this customer so simple modify as follows:
<cf_states DefaultState="NY"
FieldName="TblflStates">
Are you starting to see the value of Custom
tags? Well in short they're a way for you reuse your code easily and fast!
You can also do other neat things with them,
but that's a whole other tutorial! :)
Congratulations, you now know how custom tags
work!
Questions? Comments? Email Me.... webmaster@easycfm.com
-
A brief demonstration of Fusebox 2.0
This is a brief demonstration on how to use Fusebox 2.0 Methodology.
Author: Pablo Varando
Views: 21,107
Posted Date: Friday, September 6, 2002
-
A quick intro into the world of Custom Tags!
The following tutorial will briefly touch over Custom Tags and show you what they are, how you use them, and how they benfit you by using them.
Author: Pablo Varando
Views: 25,147
Posted Date: Friday, September 6, 2002
-
A Simple Contact Us Page….
Learn how to create a contact page in ColdFusion.
Author: Pablo Varando
Views: 34,637
Posted Date: Tuesday, August 13, 2002
-
Alternating Row Colors!
This tutorial will demonstrate how to alternate row colors when outputing your data.
Author: Pablo Varando
Views: 34,178
Posted Date: Tuesday, September 17, 2002
-
Automatically Adding Smiles To Your Messages!
This tutorial will show you how you can add smiles to your messages on the fly!
Author: Pablo Varando
Views: 22,906
Posted Date: Tuesday, October 29, 2002
-
CaSe SensitiVe password logins!
This tutorial will demonstrate how to verify users passwords to be CaSe SensiTive so add another layer of security to your applications!
Author: Pablo Varando
Views: 51,650
Posted Date: Wednesday, February 5, 2003
-
Changing the form submission page on the fly!
This tutorial is not ColdFusion oriented, but covers a great trick to allow you to submit a single form to a variety of different pages on the fly.
Author: Pablo Varando
Views: 17,867
Posted Date: Monday, December 1, 2003
-
Clearing your session variables!
This tutorial will demonstrate how to clear your applications sessions variables with just three lines of code!
Author: Pablo Varando
Views: 27,463
Posted Date: Friday, October 4, 2002
-
ColdFusion and .INI Files!
This tutorial will demonstrate how to use .INI files with ColdFusion. Perfect for users wishing to create administration areas for existing software titles that are INI file driven (i.e. FTP Servers).
Author: Pablo Varando
Views: 21,309
Posted Date: Friday, October 4, 2002
-
Combining two queries into one..
This tutorial will demonstrate how to create a query from two different queries based from two separate datasources. This is the easiest way to combine your data.
Author: Pablo Varando
Views: 22,430
Posted Date: Monday, March 10, 2003
-
Correct Content (document) serving!
This tutorial will demonstrate how to correctly serve documents via ColdFusion and allow you to correctly name the download as you see fit!
Author: Pablo Varando
Views: 16,188
Posted Date: Tuesday, February 10, 2004
-
Count Active Users On Your Site.
Have you ever wanted to display a count of how many people are on your web site at any given moment? This tutorial will demonstrate to you how to achieve just that. It will count your web site's active sessions and allow you to display them to your visitors.
Author: Pablo Varando
Views: 31,884
Posted Date: Sunday, August 25, 2002
-
Creating a file content crawler with ColdFusion....
This tutorial will show you how to make a file content crawler with ColdFusion to find specified documents in a folder and its children folders. (Similar to find files or folder in Windows(c) Operating Systems 'find' feature).
Author: Pablo Varando
Views: 27,071
Posted Date: Saturday, July 19, 2003
-
Creating a Newsletter System....
This tutorial will show you how to create a fully automated system to allow visitors to subscribe and unsubscribe to your newsletter, and for administrators to send out a newsletter to all the registered users.
Author: Pablo Varando
Views: 30,945
Posted Date: Friday, September 6, 2002
-
Creating a user athentication (Login) area.
This tutorial will demonstrate how you can create a "member's only" area. It will show you how to log them in and how to check that they are logged in.
Author: Pablo Varando
Views: 78,647
Posted Date: Monday, August 19, 2002
-
Creating an ODBC Connection within ColdFusion MX Server...
This tutorial will show you how to create an ODBC (Database) connection from within your ColdFusion MX Administration Area.
Author: Pablo Varando
Views: 31,029
Posted Date: Monday, January 6, 2003
-
Creating your very own RSS XML Feeds with ColdFusion MX!
Have you ever wanted to create your very own RSS XML News Feeds? This tutorial will show you how to create an RSS feed that will allow you to syndicate your web site and allow the world to easily use your data!
Author: Pablo Varando
Views: 31,549
Posted Date: Thursday, January 15, 2004
-
Creating, Altering and Deleting database tables with ColdFusion.
This tutorial will show you how to create, modify and delete database tables easily with ColdFusion.
Author: Pablo Varando
Views: 27,273
Posted Date: Monday, October 14, 2002
-
Delete files and folders in a specified path!
This tutorial will demonstrate how you can delete all files and sub-folders in a specified folder using ColdFusion and Windows!
Author: Pablo Varando
Views: 17,203
Posted Date: Wednesday, September 7, 2005
-
Delete Records From Your Database With ColdFusion!
This tutorial will demonstrate how to delete records from a database via your website using ColdFusion.
Author: Pablo Varando
Views: 27,127
Posted Date: Friday, July 4, 2003
-
Do you want to remember your members?
This tutorial will show you how to you can provide your members with the ability to save their username and password into memory, so they dont have to type it in everytime the want to log in to your web site.
Author: Pablo Varando
Views: 23,421
Posted Date: Tuesday, May 13, 2003
-
DSNLess Coldfusion?
Learn how to create database connection, by skipping the old ODBC connections with ColdFusion.
Author: Pablo Varando
Views: 25,118
Posted Date: Friday, August 16, 2002
-
Dynamic Last Date Modified?
This tutorial will demonstrate how to display the date a web page was last modified to your visitors dynamically.
Author: Pablo Varando
Views: 14,464
Posted Date: Monday, April 12, 2004
-
Get A Folder Size Using ColdFusion and FSO...
This tutorial will demonstrate how you can get the size of a folder (and sub folders) using ColdFusion and Windows File System Object (FSO).
Author: Pablo Varando
Views: 19,936
Posted Date: Tuesday, April 8, 2003
-
Having Your Database Do The Work… not ColdFusion!
This tutorial will demonstrate how you can use the functions that come built in on your database to do the work, instead of doing the work in your code the hard way!
Author: Pablo Varando
Views: 27,728
Posted Date: Thursday, August 8, 2002
-
Implementing FORM Error Checking On Your Pages!
This tutorial will show you two two ways you can implement error checking, to ensure that your users are actually entering the required fields on your forms!
Author: Pablo Varando
Views: 21,615
Posted Date: Wednesday, October 16, 2002
-
Inserting data into a database
This tutorial will show you how to insert data into a database, then have that information emailed to you and the person submitting the data.
Author: Pablo Varando
Views: 40,744
Posted Date: Thursday, August 1, 2002
-
Inserting FORM data into multiple database tables!
This tutorial will demonstrate how you can use one form a user fills out to insert into multiple database tables.
Author: Pablo Varando
Views: 25,786
Posted Date: Tuesday, October 15, 2002
-
Preventing People From Leeching Your Images!
This tutorial will show you how to load your images from an actual .cfm page. Therefore, allowing you to prevent people from using your content on their web sites.
Author: Pablo Varando
Views: 29,727
Posted Date: Friday, March 14, 2003
-
Previous / Next n Records
This tutorial demonstrate how to implement "Previous" & "Next" into your existing results page.
Author: Pablo Varando
Views: 32,727
Posted Date: Tuesday, September 17, 2002
-
Print your web pages on the fly!
This tutorial will demonstrate how use ColdFusion, Javascript and Style sheets to create the perfect Printing Machine! ;)
Author: Pablo Varando
Views: 26,658
Posted Date: Sunday, December 15, 2002
-
Processing XML/RSS feeds with ColdFusion MX
This tutorial will show you how to parse XML files (RSS Feeds) with ColdFusion MX and it uses an EasyCFM.COM Feed for example [Feed: 5 Most Viewed Tutorials]. It shows you how to call it via CFHTTP all the way to parse and display your records!
Author: Pablo Varando
Views: 26,484
Posted Date: Saturday, December 27, 2003
-
Reading your IIS Log Files with ColdFusion!
This tutorial will show you how you can parse through your IIS (web server) log files and insert the values into a database, therefore allowin you to display real-time stats to your visitors (i.e hits this week, etc..)
Author: Pablo Varando
Views: 29,941
Posted Date: Monday, November 4, 2002
-
Retrieving Records From a Database..
This is the basics of ColdFusion. This tutorial will demonstrate how to query a database and then display the records found.
Author: Pablo Varando
Views: 32,735
Posted Date: Saturday, August 3, 2002
-
Sending multiple attachments with CFMAIL!
This tutorial will demonstrate how to send out multiple attachments with .
Author: Pablo Varando
Views: 35,940
Posted Date: Friday, October 11, 2002
-
User Defined Functions....
Learn how to use User-Defined Functions in ColdFusion 5.0.
Author: Pablo Varando
Views: 19,668
Posted Date: Wednesday, August 21, 2002
-
Using <CFPOP> and creating an email client for POP3 Email Reading!
This tutorial will show you how to create a mail system for your site. It will allow you to get your email from a POP3 server, view your inbox, then view the message (with attachments), reply and delete that message as well.
Author: Pablo Varando
Views: 29,375
Posted Date: Thursday, November 7, 2002
-
Using Arrays in ColdFusion To Properly Display Data....
This tutorial will show you how to use arrays to display data properly in ColdFusion.
Author: Pablo Varando
Views: 25,714
Posted Date: Monday, October 28, 2002
-
Using CFRegistry to Add Your IP To CF Debug IP List!
This tutorial is intended to show you how to use the ColdFusion tag <CFRegistry>. This tutorial will show you how to add your current IP to the IP Debug List in the ColdFusion Administrator.
Author: Pablo Varando
Views: 23,777
Posted Date: Wednesday, November 6, 2002
-
Using PayPal's IPN with ColdFusion!
This tutorial will demonstrate how to implement the PayPal IPN (Instant Payment Notification) into your e-commerce applications to accept credit cards in real time!
Author: Pablo Varando
Views: 64,221
Posted Date: Wednesday, September 25, 2002
-
Using Query String Values....
This tutorial will demonstrate how to use query string values instead of form values.
Author: Pablo Varando
Views: 36,005
Posted Date: Sunday, September 15, 2002
-
What is the ID for the record I just inserted?
This tutorial will demonstrate how you can get the ID of the record you have just inserted without having to connect to the database again!
Author: Pablo Varando
Views: 22,260
Posted Date: Monday, August 11, 2003