CFLib.org – Common Function Library Project

ScopeCoalesce(strVariable, lstScope[, default])

Last updated February 17, 2004

author

Scott Jibben

Version: 1 | Requires: CF5 | Library: DataManipulationLib

Description:
This UDF can be used to get a value from a form, url, or other scope variable depending on its existence. It can also be useful to force a scope precedence that is different from the ColdFusion default. The optional 3rd parameter allows you to define the default return value if is desired.

Return Values:
Returns any possible value.

Example:

<cfset request.testing = "20">
<cfset form.testing = "10">
<cfset url.testing = "5">
<cfset variables.testing = ScopeCoalesce("testing", "client,form,url")>
<cfoutput>
  variables.testing = '#variables.testing#'
</cfoutput>
<!- the output will display '10' ->

Parameters:

Name Description Required
strVariable Variable to check for. Yes
lstScope List of scopes to check. Yes
default Default value to use if the variable does not exists. Defaults to an empty string. No

Full UDF Source:

/**
 * This UDF will find the first variable scope that exists for a variable in the list of variable scopes and return its value.
 * 
 * @param strVariable 	 Variable to check for. (Required)
 * @param lstScope 	 List of scopes to check. (Required)
 * @param default 	 Default value to use if the variable does not exists. Defaults to an empty string. (Optional)
 * @return Returns any possible value. 
 * @author Scott Jibben (scott@jibben.com) 
 * @version 1, February 17, 2004 
 */
function ScopeCoalesce(strVariable, lstScopes) {
  var vRet = "";
  var nIndex = 1;
  var nLstLen = ListLen(lstScopes);

  // assign the default return if passed in
  If (ArrayLen(Arguments) GTE 3)
    vRet = Arguments[3];

  // loop over the list
  while (nIndex LTE nLstLen) {
    if (IsDefined(ListGetAt(lstScopes, nIndex) & '.' & strVariable)) {
      vRet = Evaluate(ListGetAt(lstScopes, nIndex) & '.' & strVariable);
      break;
    }
    nIndex = nIndex + 1;
  }

  return vRet;
}
blog comments powered by Disqus

Search CFLib.org


Latest Additions

Kevin Cotton added
date2ExcelDate
May 5, 2016

Raymond Camden added
CapFirst
April 25, 2016

Chris Wigginton added
loremIpsum
January 18, 2016

Gary Stanton added
calculateArrival...
November 19, 2015

Sebastiaan Naafs - van Dijk added
getDaysInQuarter
November 13, 2015

Created by Raymond Camden / Design by Justin Johnson