Jump to content

Coldfusion Example Code

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
6 replies to this topic

#1
Guest_Kaabi_*

Guest_Kaabi_*
  • Guests
I want to see how Coldfusion looks, and compare it to other web programming languages. I saw a bit of it in another topic, but I want to see an extended example. Thanks.

#2
DevilsCharm

DevilsCharm

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 884 posts
Well, here's how you say "Hello World":

<cfset variable = "Hello World">

<cfoutput>#variable#</cfoutput>

Pretty simple, at least, more simple than something like C++ (although that's a competely different language).

#3
falco85

falco85

    Programmer

  • Members
  • PipPipPipPip
  • 105 posts
wow, that looks like XML or HTML. Got any more code you could show? I'm interested in learning more about it.

#4
Fischerspooner

Fischerspooner

    Newbie

  • Members
  • PipPip
  • 27 posts
Querying a DB.

<cfquery name="myQuery" datasource="yourODBCdatasource">

   SELECT * FROM table

</cfquery>


<!--- now show me all the records from the query  --->

<cfoutput query="myQuery">

#field1# - #field2# - #field3#

</cfoutput>

We use it at work for our intranet

#5
Lop

Lop

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,172 posts
Looks a lot like HTML

#6
TcM

TcM

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 11,147 posts
Yeah I agree.. a little it seems a cool language to learn

#7
WillB

WillB

    Learning Programmer

  • Members
  • PipPipPip
  • 43 posts

Tcm9669 said:

Yeah I agree.. a little it seems a cool language to learn

It can be...

I use Coldfusion in practitcally everything at work, and I must say I enjoy it...

If you take a look at Welcome to the world of Westgate Resorts - WestgateResorts.com
Most of the site is written in Coldfusion.

Also, MySpace is written (well part of it...) in Coldfusion also. But that isn't really the best example...

It can be a great tool to use, however it is pretty expensive.

Currently the most mature framework out there is Fusebox. However others exist that have potenial in the future as well...

The bonus with CFML is that it isn't a compiled language, but an Interpreted Language. That means that HTML can be written in a Coldfusion file (*.cfm) and it will work.

Here is a extended example of a functions file we use:

<!-----------------
   Global Functions
------------------>
<cffunction name="makeHeader"
            hint="Makes page headers, wrapping the header in standard HTML for this web site">
   <cfargument name="header"
               required="true">

   <cfset var returnContent = "">

   <cfoutput>
      <cfsavecontent variable="returnContent">
         <table class="boxHeader"
                width="550"
                border="0"
                cellpadding="3"
                cellspacing="0">
            <tr>
               <td class="boxHeader">
                  #header#</td>
            </tr>
         </table>
      </cfsavecontent>
   </cfoutput>

   <cfreturn returnContent>

</cffunction>

<cffunction name="wrapTable"
            hint="Wraps content in a standard style table for this web site.">
   <cfargument name="content"
               required="true">
   <cfargument name="header"
               required="true">

   <cfset var returnContent = "">

   <cfoutput>
      <cfsavecontent variable="returnContent">
         <table width="550"
                class="form"
                cellpadding="5"
                cellspacing="0"
                border="0">
            <tr>
               <td class="boxHeader">
                  #header#</td>
            </tr>
            <tr>
               <td bgcolor="##D1DFEC" width="529" valign="top">
                  #arguments.content#</td>
            </tr>
         </table>
      </cfsavecontent>
   </cfoutput>

   <cfreturn returnContent>

</cffunction>

<cffunction name="getRenderer"
            hint="Factory method for Renderer objects for the form style of this web site">
   <cfargument name="width"
               required="false"
               default="525">

   <cfscript>

      var renderer = getSAMSFormBuilder().getOverUnderRenderer(arguments.width);
      renderer.setCellPadding(3);
      renderer.setSubmitButtonAlignment(renderer.RIGHT);
      renderer.setErrorMessageStyle("formError");
      renderer.setRequiredSymbolStyle("requiredSymbol");

      return renderer;

   </cfscript>

</cffunction>

<cfscript>

   // Factory method for Form objects
   function getForm(name)
   {
      var theForm = getSAMSFormBuilder().getForm(name);
      theForm.setSubmitButtonImageURL("images/but_continue.jpg");
      theForm.useSubmitButtonImageOnRender(true);

      return theForm;
   }

   // Factory method for Formatter objects
   function getFormatter()
   {
      return CreateObject("component", "com.westgateresorts.web.Formatter");
   }

   // Factory method for SAMSFormBuilder objects
   function getSAMSFormBuilder()
   {
   	return application.G_SAMS_FORMBUILDER;
   }

   // Converts return characters in plain text to HTML <br> tags
   function plainTextToHTML(convertString)
   {
      return Replace(convertString, Chr(10), "<br>", "ALL");
   }

   // Standard Fusebox JSP forward function
   function fuseboxForward(XFA, attributesScope)
   {
      GetPageContext().forward("#request.G_SELF_FUSEACTION##XFA#&#attributesToURLString(attributesScope)#");
   }

   // Standard attributes to URL encoded string function
   function attributesToURLString(attributesScope)
   {
      var returnValue = "";
      var listOfKeys = StructKeyList(attributesScope);
      var listArray = ListToArray(listOfKeys);

      for (i = 1; i LTE ArrayLen(listArray); i = i + 1)
      {
         if ((CompareNoCase(listArray[i], 'FIELDNAMES') NEQ 0) AND (CompareNoCase(listArray[i], 'FUSEACTION') NEQ 0))
         {
            returnValue = returnValue & listArray[i] & "=" & URLEncodedFormat(StructFind(attributesScope, listArray[i])) & "&";
         }
      }

      // Trim trailing ampersand
      if (Len(returnValue) gt 0)
      {
         returnValue = Mid(returnvalue, 1, (Len(returnValue) - 1));
      }

      return returnValue;
   }

   // Adds BreadCrumb objects to the global breadcrumb stack for this page request
   function addBreadCrumb(newBreadCrumb)
   {
      ArrayAppend(request.G_BREADCRUMB, newBreadCrumb);
   }

   // Factory method for BreadCrumb objects
   function breadCrumb(BCText, BCLink)
   {
      return CreateObject("Component", "objects.BreadCrumb").init(BCText, BCLink);
   }

   // Factory method for WGRMenu object
   function getWGRMenuObject()
   {
      return CreateObject("Component", "objects.WGRMenu");
   }

   // Renders the BreadCrumb objects on the global breadcrumb stack for this page request
   function renderBreadCrumb(breadCrumbArray)
   {
      var returnString = "";
      var currentBreadCrumb = "";
      var currentText = "";
      var currentLink = "";
      var finalText = "";

      for (i = 1; i lte ArrayLen(breadCrumbArray); i = i + 1)
      {
         currentBreadCrumb = breadCrumbArray[i];
         currentText = currentBreadCrumb.getText();
         currentLink = currentBreadCrumb.getLink();

         if ((currentLink eq "") or (i eq ArrayLen(breadCrumbArray)))
         {
            finalText = currentText;
         }
         else
         {
            finalText = '<a href ="' & currentLink & '">' & currentText & "</a>";
         }
         if (returnString eq "")
         {
            returnString = " " & finalText;
         }
         else
         {
            returnString = returnString & " | " & finalText;
         }
      }

      return returnString;
   }


   function populateSelectListWithDestinations(selectListObject)
   {
      var currentDestination = "";
      var destinationName = "";

      // Get all of the resort destinations
      var resortDestinations = application.G_WGRESORTS.getResortLocations();

      // Go through all the resort local destinations
      for(i = 1; i lte ArrayLen(resortDestinations); i = i + 1)
      {
         // Get the destination at the i-th location and prepare the name
         currentDestination = resortDestinations[i];
         destinationName = (currentDestination.getTwoLetterState() & " - "  & currentDestination.getName());

         // Add the name of the i-th destination to the select list object
         selectListObject.addOption(destinationName, destinationName);
      }
   }

</cfscript>

<cffunction name="getWestgateResortsObject"
            access="public"
            output="false"
            returnType="com.westgateresorts.wgr.WGResorts"
            hint="Returns a reference to the global WGResorts component">

   <cfreturn application.G_WGRESORTS>
</cffunction>

<cffunction name="getHotWeeksPhone"
            access="public"
            output="false"
            returnType="string"
            hint="Returns the phone number displayed on the Hot Weeks page.">

   <cfreturn application.G_HOTWEEKS_PHONE>
</cffunction>

<cffunction name="getGoogleAnalyticsCode"
            access="public"
            returnType="string"
            hint="Outputs google analytics code according to the environment settings.">

   <cfset var googleAnalyticsEnabled = application.G_GOOGLE_ANALYTICS_ENABLED>
   <cfset var returnContent = "">

   <cfoutput>
      <cfsavecontent variable="returnContent">
			<cfif googleAnalyticsEnabled>
				<script src="http://www.google-analytics.com/urchin.js" type="text/javascript">
				</script>
				<script type="text/javascript">
				   _uacct = "UA-758430-2";
				   urchinTracker();
				</script>
			</cfif>
      </cfsavecontent>
   </cfoutput>
   
   <cfreturn returnContent>

</cffunction>

Something that other people haven't stated yet, is that Coldfusion also offers <cfscript> which resembles Java Syntax.
Everything in Life can be solved with an 'If/Else' statement.

Life is like one big try/catch statement, nested within a loop.

-Will

[SIGPIC][/SIGPIC]