Skip to content

Instantly share code, notes, and snippets.

@cocolote
Last active May 3, 2023 10:24
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save cocolote/81af8e8dba187de66af3 to your computer and use it in GitHub Desktop.
Save cocolote/81af8e8dba187de66af3 to your computer and use it in GitHub Desktop.
ColdFusion cheatsheet

The ColdFusion Basics

Set variables

  • Current time
<cfset currentTime = now() />
  • Regular string
<cfset name = "Ezequiel" />
  • integer
<cfset age = 29 />
  • Concatenate variable and string
<cfset todayDate = "Today is: #now()#" />
<cfset greeting = "Hello " & name />
  • array
<cfset dataArray = [dateFormat(now(), "short"), dateFormat(dateadd('d',1,now()), "short"), "Me", 42] />
  • data structure
<cfset dictionary = { today = dateFormat(now(), "short"), tomorrow = dateFormat(dateadd('d', 1, now()), "short"), who = "Me", the_answer_to_life_and_everything_else = 42 } />
  • to save a big content
<cfsavecontent>
    Hi there,

    I want to send you a hoverboard.
    Let me know if you will accept this free offer.

    -Me
</cfsavecontent>

Print out

  • Just a hard coded string
<cfdump var = "This is a message for you" />
<cfoutput>This is a message for you</cfoutput>
  • A variable
<cfdump var = "#name#" /> _same as inspect in ruby_
<cfoutput>#name#</cfoutput> _same as puts in ruby_

Some date functions

  • DateDiff("d", "1/1/2000", now()) will give you the difference between the two dates

  • DateAdd("d", now(), 42) add 42 days to the now() date

Arrays

Arrays don't start from 0 like in every other language instead start from 1

  • Create array literal
<cfset someThings = ["Boca", "Futbol", 42] />

constructor

<cfset sameArray = arrayNew(1) />
  • Adding elements in an specific index
<cfset someThings[4] = "Ruby on Rails" />

appending

<cfset ArrayAppend(someThings, "Destiny") />
  • looping over the array
<cfloop array = "#someThings#" index = "thing">
    <cfoutput>#thing#</cfoutput>
</cfloop>

Structures

This are like hashes in Ruby or dictionaries in python

  • Create structure literal
<cfset aGuy = {} />
<cfset batman = {
    "first_name" = "Bruno",
    "last_name" = "Diaz",
    "age" = 42
} />

constructor

<cfset aGuy = structNew() />
  • Adding elements with brackets
<cfset aGuy["first_name"] = "Ezequiel" />
<cfset aGuy["last_name"] = "Lopez" />

with dot notation

<cfset aGuy.age = 29 />
<cfset aGuy.height = "5' 11\"" />
  • looping over the structure
<cfloop collection = "aGuy" item = "data">
    <cfoutput>#aGuy[data]#: #data#</cfoutput>
</cfloop>

Queries

use SQL in coldfusion to retrieve data form a database or enter data in it

  • This query allocates the result of the query into the variable persons so then I can do this to display the info retrieved
<cfquery name="firstQ" datasource="tsdata.ts24">
    SELECT * FROM TestTable
</cfquery>
  • Looping over the Query
<cfoutput>
    <cfloop query="#firstQ#">
        <p><i>myDataAlfa: </i>#firstQ.myDataAlfa# <i>myDataInt: </i>#firstQ.myDataInt#</p>
    </cfloop>
    
    <!--- Extra data to get from the query --->
    <p>#firstQ.columnlist#</p>
    <p>#firstQ.recordcount#</p>
</cfoutput>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment