Skip to content
This repository has been archived by the owner on Apr 27, 2021. It is now read-only.

Reference for developers who are new to the language or need a quick refresh.

Notifications You must be signed in to change notification settings

kris-ellery/coldfusion-codeguide

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

29 Commits
 
 
 
 
 
 

Repository files navigation

ColdFusion CodeGuide

ColdFusion code guide for developers who are new to the language or need a quick refresh.

Table of Contents


Tag notation

<!--- Set variable ---->
<cfset padawan = "Kris">

<!--- Variable output ---->
<cfoutput>#padawan#</cfoutput>

<!--- Variable dump ---->
<cfdump var = "#padawan#">

Script notation

<cfscript>
  // Set variable
  master = "Yoda";

  // Variable output
  WriteOutput(master);

  // Variable dump
  WriteDump(master);
</cfscript>

Array Functions

Tag notation

<!--- Set array --->
<cfset padawans = [ "Eric", "Sean", "Jake" ]>

<!--- Array dump --->
<cfdump var="#padawans#">

<!--- Loop over array --->
<cfloop array="#padawans#" index="padawan">
  <cfoutput>#padawan#</cfoutput>
</cfloop>

Script notation

<cfscript>
  // Set array
  masters = [ "Qui-Gon Jinn", "Obi-Wan Kenobi", "Yoda" ];

  // Array dump
  WriteDump(masters);

  // Loop over array
  for (master in masters) {
    WriteOutput(master);
  }
</cfscript>

Structure Functions

Tag notation

<!--- Set struct --->
<cfset padawan = {}>

<!-- Append item to struct using bracket notation (case sensitive) --->
<cfset padawan["name"] = "Kris">

<!--- Append item to struct using dot notation (case insensitive) --->
<cfset padawan.side = "Light">

<!--- Dump struct --->
<cfdump var="#padawan#">

<!--- Set struct and append content in single statement --->
<cfset master = {
  "name": "Yoda",
  "side": "Light"
}>

<!--- Loop over a struct --->
<cfloop collection="#master#" item="key">
  <cfoutput>#key#: #master[key]#</cfoutput>
</cfloop>

Script notation

<cfscript>
  // Set struct
  sith = {};

  // Append item to struct using bracket notation (case sensitive)
  sith["name"] = "Vader";

  // Append item to struct using dot notation (case insensitive)
  sith.side = "Dark";

  // Dump struct
  WriteDump(sith);

  // Set struct and append content in single statement
  lord = {
    "name": "Sidious",
    "side": "Dark"
  };

  // Loop over struct
  for (key in lord) {
    WriteOutput("#key#: #lord[key]#");
  }
</cfscript>

Tag notation

<!--- Set variable --->
<cfset padawan = "Kris">

<!--- Switch case statement --->
<cfswitch expression="#padawan#">
  <cfcase value="Kris">

  </cfcase>
  <cfcase value="Eric">

  </cfcase>
  <cfcase value="Sean, Jake">

  </cfcase>
  <cfdefaultcase>

  </cfdefaultcase>
</cfswitch>

Script notation

<cfscript>
  // Set variable
  jedi = "Yoda";

  // Switch case statement
  switch(jedi) {
    case "Yoda":

      break;
    case "Obi-Wan Kenobi":

      break;
    case "Qui-Gon Jinn": case "Mace Windu":

      break;
    default:

      break;
  }
</cfscript>

Description Tag operator Script operator
Equal is, equal, eq ==
Not equal is not, not equal, neq !=
Greater than gt, greater than >
Greater than or equal greater than or equal to, ge, gte >=
Less than lt, less than <
Less than or equal less than or equal to, le, lte <=
And and &&
Or or ||

Operator Types

Tag notation

<!--- Set variable --->
<cfset age = 10>

<!--- Simple if/elseif/else --->
<cfif age eq 15>

<cfelseif age gte 30>

<cfelseif age lte 5>

<cfelseif age gt 10 or age lt 10>

<cfelse>

</cfif>

<!--- Set variable --->
<cfset height = 3>

<!--- Ternary operator --->
<cfset jedi = (height lte 3) ? "Yoda" : "Obi-Wan Kenobi">

<!--- Dump jedi --->
<cfdump var="#jedi#">

Script notation

<cfscript>
  // Set variable
  count = 10;

  // Simple if/elseif/else
  if (count == 15) {

  } else if (count >= 30) {

  } else if (count <= 5) {

  } else if (count > 10 || count < 10) {

  } else {

  }

  // Set variable
  power = 7;

  // Ternary operator
  sith = (power <= 8) ? "Sidious" : "Vader";

  // Dump sith
  WriteDump(sith);
</cfscript>

Tag notation

<!--- Loop --->
<cfloop from="1" to="10" index="i">
  <cfoutput>#i#</cfoutput>
</cfloop>

<!--- Loop break --->
<cfloop from="1" to="10" index="i">
  <cfoutput>#i#</cfoutput>
  <cfbreak>
</cfloop>

<!--- Loop skip --->
<cfloop from="1" to="10" index="i">
  <cfif i mod 2 eq 0>
    <cfcontinue>
  </cfif>
  <cfoutput>#i#</cfoutput>
</cfloop>

Script notation

<cfscript>
  // Loop
  for (i=1; i <= 10; i++) {
    WriteOutput(i);
  }

  // Loop break
  for (i=1; i <= 10; i++) {
    WriteOutput(i);
    break;
  }

  // Loop skip
  for (i=1; i <= 10; i++) {
    if (i mod 2 == 0) {
      continue;
    }
    WriteOutput(i);
  }

  // While loop
  k = 0;
  while (k < 10) {
    k++;
    WriteOutput(k);
  }

  // Do/While loop
  s = 0;
  do {
    s++;
    WriteOutput(s);
  } while (s LT 10);
</cfscript>

Tag notation

<!--- Set struct --->
<cfset jedi = {
  "name": "Luke Skywalker",
  "side": "Light",
  "starfighter": "X-Wing"
}>

<!--- Serialize to JSON --->
<cfset jediToJSON = serializeJSON(jedi)>

<!--- Deserialize from JSON --->
<cfset jediFromJSON = deserializeJSON(jediToJSON)>

<!--- Dump --->
<cfdump var = "#jediToJSON#">
<cfdump var = "#jediFromJSON#">

Script notation

<cfscript>
  // Set struct
  sith = {
    "name": "Darth Vader",
    "side": "Dark",
    "starfighter": "TIE fighter"
  };

  // Serialize to JSON
  sithToJSON = serializeJSON(sith);

  // Deserialize from JSON
  sithFromJSON = deserializeJSON(sithToJSON);

  // Dump
  WriteDump(sithToJSON);
  WriteDump(sithFromJSON);
</cfscript>

Tag notation

<!--- Public function --->
<cffunction name="getJedi" access="public" returnType="struct">

  <!--- Arguments --->
  <cfargument name="id" type="numeric" required="true">

  <!--- Do something with "id" and create new struct --->
  <cfset jedi = {
    "id": "5",
    "name": "Yoda"
  }>

  <!--- Return --->
  <cfreturn jedi>
</cffunction>

<!--- Access function --->
<cfset jedi = getJedi(5)>

<!--- Dump function return --->
<cfdump var="#jedi#">

Script notation

<cfscript>
  // Public function
  public struct function getSith(required numeric id) {

    // Do something with "id" and create new struct
    sith = {
      "id": "7",
      "name": "Darth Vader"
    };

    // Return
    return sith;
  }

  // Access function
  sith = getSith(7);

  // Dump return
  WriteDump(sith);
</cfscript>

Tag notation

<cfcomponent>

  <!--- Public scope --->
  <cfset this.master = "Yoda">

  <!--- Private scope --->
  <cfset variables.padawan = "Kris">

  <!--- Public function --->
  <cffunction name="getJedi" access="public" returnType="struct">
    <cfargument name="id" type="numeric" required="true">

    <cfset jedi = {
      "id": "5",
      "name": "Yoda"
    }>

    <cfreturn jedi>
  </cffunction>

</cfcomponent>

<!--- Create an instance of component (Jedi.cfc) --->
<cfset Jedi = CreateObject("Component", "path.to.component.Jedi")>

<!--- Access public variable --->
<cfset master = Jedi.master>

<!--- Access method --->
<cfset jedi = Jedi.getJedi(7)>

Tag notation

<!--- Get data from database --->
<cfquery name="myQuery" datasource="myDataSource">
    SELECT id, name FROM jedi
</cfquery>

<!--- Dump results --->
<cfdump var = "#myQuery#">

<!--- Output results --->
<cfoutput query="#myQuery#">
  Id: #id#, Name: #name#
</cfoutput>

<!--- Loop over results --->
<cfloop query="#myQuery#">
  <cfoutput>Id: #id#, Name: #name#</cfoutput>
</cfloop>

<!--- Query caching --->
<cfquery name="myQuery" datasource="myDataSource" cachedwithin="#createTimespan(0,1,0,0)#">
    SELECT id, name FROM jedi
</cfquery>

<!--- Query with query param --->
<cfquery name="myQuery" datasource="myDataSource">
    SELECT id, name FROM jedi
    WHERE id = <cfqueryparam value="7" cfsqltype="cf_sql_integer">
</cfquery>

Tag notation

<!--- Simple stored procedure --->
<cfstoredproc procedure="myStoredProcedure" datasource="myDataSource">
  <cfprocparam type="in" cfsqltype="cf_sql_integer" value="7">
  <cfprocresult name="myStoredProcedureResults">
</cfstoredproc>

Script notation

<cfscript>
  // Store procedure using "attribute-value" pairs
  spService = new storedproc();
  spService.setDatasource("myDataSource");
  spService.setProcedure("myStoredProcedure");
  spService.addParam(cfsqltype="cf_sql_integer", type="in", value="7");
  spService.addProcResult(name="myStoredProcedureResults");
  spService.execute();
</cfscript>

Tag notation

<cftry>

  <!--- Do work son! --->

  <cfcatch type="database">
    <cfthrow message="It broke..." detail="Wasn't me.">
  </cfcatch>

  <cfcatch type="any">
    <!--- Handle any exception type --->
    <cfoutput>"Type: " & #cfcatch.type#</cfoutput>
    <cfoutput>"Message: " & #cfcatch.message#</cfoutput>
  </cfcatch>

  <cffinally>
    <!--- Always execute --->
  </cffinally>
</cftry>

Script notation

<cfscript>
  try {

    // Do work son!

  } catch(database e) {
    throw(message="It broke...", detail="Wasn't me.");
  } catch(any e) {
    // Handle any exception type
    WriteOutput("Type: " & e.type);
    WriteOutput("Message: " & e.message);
  } finally {
    // Always execute
  }
</cfscript>

Tag notation

<!--- Include --->
<cfinclude template="path/to/include.cfm">

Script notation

<cfscript>
  // Include
  include "path/to/include.cfm";
</cfscript>

About

Reference for developers who are new to the language or need a quick refresh.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published