Skip to content

Instantly share code, notes, and snippets.

@seanvikoren
Created July 3, 2012 21:00
Show Gist options
  • Save seanvikoren/3043063 to your computer and use it in GitHub Desktop.
Save seanvikoren/3043063 to your computer and use it in GitHub Desktop.
ColdFusion Test Files
<!--- Hello.cfm =====================================--->
<html>
<head>
<title>Invoking Component Methods</title>
</head>
<body>
<h1>Invoking Component Methods</h1>
<!-- ====================================================================================== -->
<h2>Example 1: Transient Instantiation</h2>
<cfinvoke component = "Images" method="ImageFlipper" returnvariable="imgpath">
<cfinvokeargument name="directory" value="Images/Runners">
</cfinvoke>
<cfoutput><img src="#imgpath#"/></cfoutput>
<!-- ====================================================================================== -->
<h2>Example 2: Using attributes rather than cfinvokeargument</h2>
<cfinvoke
component = "Images"
method="ImageFlipper"
returnvariable="imgpath"
directory="Images/Runners">
<cfoutput><img src="#imgpath#"/></cfoutput>
<!-- ====================================================================================== -->
<h2>Example 3: Passing the arguments using argumentCollection</h2>
<cfset args = StructNew()>
<cfset args.directory = "Images/Runners">
<cfset args.extensions = "jpg">
<cfinvoke
component="Images"
method="ImageFlipper"
argumentCollection="#args#"
returnVariable="imgpath">
<cfoutput><img src="#imgpath#"/></cfoutput>
<!-- ====================================================================================== -->
<h2>Example 4: Separate instantiation and method invocation</h2>
<cfset args = StructNew()>
<cfset args.directory = "Images/Runners">
<cfset args.extensions = "jpg">
<cfobject name="objImage" component="Images2">
<cfloop index="i" from="1" to="7">
<cfinvoke
component="#objImage#"
method="ImageFlipper"
directory="Images/Runners"
extensions = "jpg"
returnVariable="imgpath">
<cfoutput><img src="#imgpath#"/></cfoutput>
</cfloop>
</body>
</html>
<!--- Util.cfc =====================================--->
<!---
Here we demonstrate a basic ColdFusion component with embedded cfScript, SQL, and JavaScript.
NOTE: This is not running code and therefore has not been tested and is intended as an example only.
--->
<cfcomponent>
<cffunction name="ImageFlipper" access="public" returntype="string">
<cfargument name="directory" type="string" required="true">
<cfargument name="extensions" type="string" default="jpg,gif">
<cfset aryImages = getFileList(arguments.directory,arguments.extensions)>
<cfparam name="SESSION.picture" default="0">
<cfset SESSION.picture = SESSION.picture + 1>
<cfif SESSION.picture GT ArrayLen(aryImages)>
<cfset SESSION.picture = 1>
<cfelse>
<cfset SESSION.picture = -1>
</cfif>
<cfset imagepath="#arguments.directory#/#aryImages[SESSION.picture]#">
<cfreturn imagepath>
</cffunction>
<!--- Here we drop into cfScript syntax --->
<cfscript>
public void function foo()
{
WriteOutput("Method foo() called<br>");
}
</cfscript>
<cffunction name="WriteList" access="public" returntype="string">
<!--- Here we drop into SQL syntax --->
<cfquery name="GetParks" datasource="cfdocexamples" cachedwithin="#CreateTimeSpan(0, 6, 0, 0)#">
SELECT PARKNAME, REGION, STATE
FROM Parks
ORDER BY ParkName, State
</cfquery>
<cfset thisString = "This is a string">
<cfoutput>
<b>The thisString variable in ColdFusion</b><br>
#thisString#<br>
<br>
<strong>
In a JavaScript script, convert thisString Variable to JavaScript
and output the resulting variable:
</strong>
<br>
<!--- Here we drop into JavaScript syntax --->
<script type="text/javascript" language="JavaScript">
var #ToScript(thisString, "jsVar")#;
document.write("jsVar in JavaScript is: " + jsVar);
</script>
</cfoutput>
<cfoutput query="GetParks" startrow="#StartRow#" maxrows="#MaxRows#">
<tr>
<td valign="top" bgcolor="ffffed">
<b>#GetParks.CurrentRow#</b>
</td>
<td valign="top">
<font size="-1">#ParkName#</font>
</td>
<td valign="top">
<font size="-1">#Region#</font>
</td>
<td valign="top">
<font size="-1">#State#</font>
</td>
</tr>
</cfoutput>
</cffunction>
</cfcomponent>
<!--- Util2.cfc =====================================--->
<cfcomponent extends="Util">
<cffunction name="CreateImageTable" access="public" returntype="string">
<cfargument name="directory" type="string" required="true">
<cfargument name="columns" type="numeric" default="2">
<cfargument name="extensions" type="string" default="jpg,gif">
<cfset aryImages = getFileList(arguments.directory,arguments.extensions)>
<cfset rows = Ceiling(ArrayLen(aryImages)/columns)>
<cfset picture = 1>
<cfsavecontent variable="ReturnTable">
<table border="1">
<cfloop index="i" from="1" to="#rows#">
<tr>
<cfloop index="j" from="1" to="#columns#">
<td>
<cfoutput>
<img src="#arguments.directory#/#aryImages[picture]#">
</cfoutput>
</td>
<cfset picture = picture + 1>
</cfloop>
</tr>
</cfloop>
</table>
</cfsavecontent>
<cfreturn ReturnTable>
</cffunction>
</cfcomponent>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment