"Creating Your Own Tags With Cold Fusion"
by  Leon Chalnick
Web Techniques,  March 1998

Web Techniques grants permission to use these listings and code for 
private or commercial use provided that credit to Web Techniques and 
the author is maintained within the comments of the source. For 
questions, contact editors@web-techniques.com.       

This file consists of six listings that accompany the article Creating 
Your Own Tags With Cold Fusion", published in the March 1998 issue of 
Web Techniques:

CHALNICK

LISTING ONE


<HTML>
   <TITLE><CFOUTPUT>#Attributes.ErrorTitle#</CFOUTPUT></TITLE>
<HEAD>
</HEAD>
<BODY BGCOLOR=”White”>
<DIV ALIGN=”center”>
<TABLE BORDER=4>
<TR ALIGN=”left” VALIGN=”bottom”>
   <TD BGCOLOR=”DarkBlue”>
      <FONT SIZE=+2 COLOR=”White”><CFOUTPUT>
#Attributes.ErrorTitle#</CFOUTPUT></FONT>
   </TD>
</TR>
<TR VALIGN=”bottom”>
<TD BGCOLOR=”silver”>
   <TABLE>
   <TR>
      <TD>
         <CFIF ParameterExists(Attributes.LeadIn) is “No”>
            There are one or more problems in this page:
         <CFELSE>
            <CFOUTPUT>#Attributes.LeadIn#</CFOUTPUT>
         </CFIF>

         <UL>
         <CFLOOP
            LIST=”#Attributes.ErrorMsg#”
            INDEX=”ii”
            DELIMITERS=”|”
         >
            <LI><CFOUTPUT>#ii#</CFOUTPUT><BR>
         </CFLOOP>
         </UL>
         <HR>
      </TD>   
   </TR>
   <TR>
      <TD ALIGN=”center”>
         <FORM NAME=”GoBack”>
         <P><INPUT TYPE=”button” VALUE=”   Ok   “ onClick=”history.back();”>
         </FORM>
      </TD>
   </TR>
   </TABLE>   
</TD>
</TR>
</TABLE>

</DIV>

</BODY>
</HTML>



LISTING TWO

<!—- Make sure user provided value for Attributes.Body —->
<CFIF ParameterExists( Attributes.Body ) is “No”>
   <CF_SHOWERR
      ERRORTITLE=”Developer Error”
      LEADIN=”This program has made an invalid call 
to the custom tag, CF_WRAPTEXT.”
      ERRORMSG=”When calling CF_WRAPTEXT, 
you must include the BODY attribute, which 
                contains the text to be wrapped”
   >
   </CFABORT>
</CFIF>

<!—- Default nCols to 70 if user hasn’t specified it in an attribute —->
<CFSET NCols = Iif( ParameterExists( Attributes.nCols ) 
is “Yes”, “Attributes.nCols”, DE(“70”))>

<CFSET nTxtSize = Len(Attributes.Body)>
<CFSET frmText = “”>
<CFSET CRLF = Chr(13) & Chr(10)>
<CFIF nTxtSize LTE nCols>
   <!—-
      Text isn’t as wide as the specified number of columns.
   —->
   <CFSET frmText = Attributes.Body>
<CFELSE>
   <CFSET nSPos = 1>
   <CFLOOP
      CONDITION=”True”
   >
      <CFIF nSPos + nCols -1 GTE nTxtSize>
         <CFSET frmText = frmText & Mid(Attributes.Body,nSPos,
nTxtSize-nSPos+1)>
         <CFBREAK>
      <CFELSE>
         <CFSET nPos = nSPos + nCols -1>
         <CFLOOP
            INDEX = “ii”
            FROM  = #nPos#
            TO    = #nSPos#
            STEP  = -1
         >
            <CFIF ( Mid(Attributes.Body,ii,1) is “ “ )
             OR   ( Mid(Attributes.Body,ii,1) is #CRLF# )>
               <CFSET frmText = frmText & Mid
(Attributes.Body, nSPos, nCols-(nPos-ii)) & #CRLF# >
               <CFSET nSPos = ii + 1 >
               <CFBREAK>
            <CFELSEIF ( ii is nSPos )>
               <!—-
                  There wasn’t a single break in the current chunk of text.
                  We’ll simply cut of a chunk which is nCols wide 
	      starting at nSPos.
               —->
               <CFSET frmText = frmText & Mid
(Attributes.Body,nSPos,nCols) & #CRLF# >
               <CFSET nSPos = nSPos + nCols>
               <CFBREAK>
            </CFIF>
         </CFLOOP>
      </CFIF>
   </CFLOOP>
</CFIF>
<CFSET Caller.WrappedText = frmText>



LISTING THREE

<CF_WRAPTEXT
   NCOLS=20
   BODY = “This is a message that is very long and requires formatting in order to fit on the screen. I think 
that I could keep going but it would probably get boring.”
>
<PRE>
<cfoutput>#WrappedText#</cfoutput>
</PRE>



LISTING FOUR

<!—- CallSelectItems.cfm —->
<!—- This template both calls CF_SELECTITEMS and processes the results —->
<HTML>
<HEAD>
   <TITLE>CallSelectItems.CFM</TITLE>
</HEAD>
<BODY BGCOLOR=”white”>

<CFIF ParameterExists( RightItems ) is “Yes”>
   <H3>Last Selected</H3>
   <CFOUTPUT>#RightItems#</CFOUTPUT>
   <H3>Last Unselected</H3>
   <CFOUTPUT>#LeftItems#</CFOUTPUT>
<CFELSEIF ParameterExists( UserCancel ) is “Yes”>
   <H3>No Items Selected </H3>
<CFELSE>
   <CF_SELECTITEMS
      DATASOURCE=”Picnic”
      LISTTABLE=”PicnicItems”
      VALUEFIELD=”ItemCode”
      DISPLAYFIELD=”Description”
      SELECTEDITEMS=”SELECT DISTINCT ItemCode FROM 
OurPicnicItems WHERE Year=1996”
      ONOK=”/testarea/p/callSelectItems.cfm”
      TITLE=”Items in the 1996 picnic”
   >
</CFIF>
</BODY>
</HTML>


LISTING five

<CFQUERY
         DATASOURCE=”#Attributes.DataSource#”
         NAME=”NotSel”
      >
         SELECT DISTINCT #Attributes.ValueField#
         <!—- Only include Display field if attribute was specified —->
         <CFIF ParameterExists(Attributes.DisplayField) is “Yes”>
            , #Attributes.DisplayField# 
         </CFIF>
         FROM #Attributes.ListTable#
         <CFIF ParameterExists(Attributes.SelectedItems) is “Yes”>
            WHERE #Attributes.ValueField# NOT IN (#PreserveSingleQuotes(Attributes.SelectedItems)#)
         </CFIF>
         <CFIF ParameterExists(Attributes.ListCondition) is “Yes”>
            <CFIF ParameterExists(Attributes.SelectedItems) is “Yes”>
               AND #PreserveSingleQuotes(Attributes.ListCondition)#
            <CFELSE>
               WHERE #PreserveSingleQuotes(Attributes.ListCondition)#
            </CFIF>
         </CFIF>
      </CFQUERY>


LISTING Six

<CFIF ParameterExists(Attributes.SelectedItems) is “Yes”>
         <CFQUERY
            DATASOURCE=”#Attributes.DataSource#”
            NAME=”Sel”
         >
            SELECT DISTINCT #Attributes.ValueField#
            <!—- Only include Display field if attribute was specified —->
            <CFIF ParameterExists(Attributes.DisplayField) is “Yes”>
               , #Attributes.DisplayField# 
            </CFIF>
            FROM #Attributes.ListTable#
            WHERE #Attributes.ValueField# IN (#PreserveSingleQuotes(Attributes.SelectedItems)#)
            <CFIF ParameterExists(Attributes.ListCondition) is “Yes”>
               AND #PreserveSingleQuotes(Attributes.ListCondition)#
            </CFIF>
         </CFQUERY>
      </CFIF>