Skip to content

Instantly share code, notes, and snippets.

@learncfinaweek
learncfinaweek / gist:4121127
Created November 20, 2012 21:07
Data Handling - XML

Dealing with XML in ColdFusion is fairly straightforward. ColdFusion provides many built-in functions for doing all sorts of XML based operations. Some of those functions provide streamlined access to create, read, and search an xml document. You can also validate XML data against a DTD or schema and transform XML using XSLT.

Reading an XML Document

@learncfinaweek
learncfinaweek / gist:4121129
Created November 20, 2012 21:07
Data Handling - json

JSON or, JavaScript Object Notation, is a simple way for applications to share data. It is commonly used to send data from a server to a browser client, but it can be used for much more. It closely resembles XML without the markup or, more accurately, a struct of structs. Creating JSON data in ColdFusion is just about as easy as setting a variable.

Create JSON String

To create JSON, you first need to start with a structure. For example:

@learncfinaweek
learncfinaweek / gist:4121143
Created November 20, 2012 21:09
Data Handling - Hands On 9

In this hands on, we will do a simple database call and output the data.

Tags Used: <cfset>, <cfquery>, <cfloop>

  1. Open up the /www/resume.cfm file in your code editor.
@learncfinaweek
learncfinaweek / gist:4121145
Created November 20, 2012 21:09
Data Handling - Hands On 10
@learncfinaweek
learncfinaweek / gist:4121146
Created November 20, 2012 21:10
Data Handling - Hands On 11

In this hands on, we are going to generate an XML sitemap that is formatted for use with Google Sitemaps.

Tags Used: <cfxml>, <cfoutput>, <cffile>

Functions Used: toString

@learncfinaweek
learncfinaweek / gist:4121210
Created November 20, 2012 21:19
Code Reuse - Functions

One acronym that is thrown around in programming circles is DRY: Don't Repeat Yourself. Does anyone enjoy reading a repetitive book? Listening to the same five songs on the radio station? How about writing the same piece of code, over and over again? Performing the same task over and over again is mind-numbing and soul-crushing work. When working in a large system, it's much more fun to write a piece of code in such a way that it can be used multiple times.

Now that some of the basics of programming with ColdFusion have been covered, we can take a moment to discuss code reuse. Writing code that can be used again is an important skill for developers to master; not only does it save time, but it forces the developer to structure their code such that individual routines are isolated and labelled properly. Doing this makes the code easier to read and interpret. In this chapter, simple examples will be used; you can extrapolate from these examples and imagine the effects on a larger scale.

@learncfinaweek
learncfinaweek / gist:4121221
Created November 20, 2012 21:20
Code Reuse - Includes

You can also make a new .cfm file and use it as a function library. You can then use the cfinclude tag to include it on any pages that might need it.

cfinclude only has one attribute, template, that takes the path to the function library file. If the function is placed in a file called greetingCustomizer.cfm, to give our page access to the function we just include our greeting customizer on the page like so:

<cfinclude template="path/to/libraries/greetingCustomizer.cfm" />
@learncfinaweek
learncfinaweek / gist:4121227
Created November 20, 2012 21:20
Code Reuse - Custom Tags

For displaying code on the front end, another option is to use a custom tag. Custom tags live in their own directory and are prefixed with "cf_". Putting a custom tag on a cfml page will execute the code contained in the tag as if it had been included on the page itself. Start by creating a new .cfm file to house our custom tag. Let's call it greeting.cfm. First, check the executionMode of the tag. If the executionMode is "start", that means that the tag is being opened. If executionMode is "end", that means that the tag is being closed. This can be checked by setting up an if statement like so:

<cfif thisTag.executionMode EQ "start">

<!--- Code to execute when the tag is being opened --->

<cfelse>

@learncfinaweek
learncfinaweek / gist:4121233
Created November 20, 2012 21:21
Code Reuse - Components

A more modern alternative to cfinclude is to create components. Components in ColdFusion behave similarly to objects in many other programming languages. First, start by creating a new file, called Greeting.cfc. ColdFusion uses .cfc to denote files that are components.

Inside of Greeting.cfc, add a cfcomponent tag:

<cfcomponent>
@learncfinaweek
learncfinaweek / gist:4121237
Created November 20, 2012 21:22
Code Reuse - Hands On 12

In this hands on, let's create a function that will take a string and output a string of ASCII characters. Some people think that this will stop email addresses from being spidered. This may or may not be the case, but the logic for creating a function is still the same.

Tags Used: <cfscript>, <cffunction>, <cfargument>, <cfset>, <cfloop>,