[thelist] CF Techniques?

Seth Bienek - Web Consultant sbienek at acep.org
Mon Aug 21 17:29:15 CDT 2000


Hi Frank,

> Can you tell me more about the mechanics of this? 

Yes.  Basically, you are checking for the existence of a form value, then
setting a URL scoped variable if it exists..  Like this:

<cfif IsDefined("Form.UserName")>
  <cfset URL.UserName = Form.UserName>
</cfif>

This will work with all scopes, as far as I know.

> Is there a way of 
> making it so that I don't have to pre-write a list, but have 
> something process the values that are passed anyway?

Yes.  CF exposes a list of all formfields passed in a "post" operation, as a
list named "form.fieldnames".  How convenient!  You can re-scope an entire
form's worth of data very easily (in this case, to the URL Scope):

<cfloop list = "#Form.Fieldnames#" index="ThisFormField">
   <cfset "URL.#ThisFormField#" = "#evaluate("Form.#ThisFormField#")#">
</cfloop>

You can also re-scope query results.  I wrote a custom-tag to do this awhile
back, for a Fusebox app I was working on.  It's very simple code (20 lines,
including comments), so I've pasted it in here as well.  The idea was to
re-scope query results into the "Attributes" scope, and auto-fill a form if
a record existed.  If not, then defaults were set for the form fields'
corresponding "Attributes" variables...  This custom tag takes two values,
"Query" which is required, and is the name of the query you want to
re-scope, and "Scope" (optional, defaults to "attributes"), which is the
name of the scope you want the values translated into.  It was for personal
use, so it's not documented, but here's the code anyway, for those
interested:

<!--- START CF_RESCOPE --->
<cfsetting enablecfoutputonly="Yes">

<!--- Set default scope to "Attributes." --->
<cfparam name="Attributes.Scope" default="Attributes">

<!--- Wrangle the query name from the calling template --->
<cfset QueryName = "caller." & Attributes.Query>

<!--- Define the list of columns in the query --->
<cfset ColumnList = "#evaluate(QueryName & ".ColumnList")#">

<!--- Re-scope Query Columns to 'Attributes' Scope.. BAM! --->
<cfloop list = "#ColumnList#" index="fieldname">
	<cfset "Caller.#Attributes.Scope#.#trim(fieldname)#" =
"#evaluate("#QueryName#.#trim(fieldname)#")#">
</cfloop>

<cfsetting enablecfoutputonly="No">
<!--- END CF_RESCOPE --->

Re-scoping is also part of the FuseBox spec.  If you're doing advanced level
CF developing, I highly recommend having a look at:
http://fusebox.org

There is a custom tag there that also does re-scoping, called
<cf_FormURL2Attributes>

A winded, but hopefully helpful response.

Take Care,

Seth 

------------------------------------
Seth Bienek
Independent ColdFusion Developer









More information about the thelist mailing list