While working on an on-going CFWheels project for OurayClimbing.com, I have been spending a ton of time on the CMS. As is always the case for me, I have several form fields in a CMS that require select lists for US States and Countries. In the past, I've thrown this data into a DB, queried it as needed, etc.
Since I am using CFWheels and a bit plugin crazed, I decided to create a basic plugin that would allow me to quickly and easily get this data in my controllers and views. The plugin is available at either of the following locations:
- CFWheels—http://cfwheels.org/plugins/listing/52
- gihub—https://github.com/imageaid/StatesAndCountries
In the interest of less typing, here are the details from my plugin's index.cfm file.
StatesAndCountries for Wheels v1.0
A very basic plugin to access queries for US states, Canadian provinces, and Countries. I created this because I cannot recall an application that I've built over the years that did not need a State or Country drop-down in some form somewhere in the application.
The plugin comes with three asset files: us_states.xml, canadian_provinces.xml, and countries.xml. When one of the four methods (below) are called for the first time, the relevant XML file is read and a query is created, stored in the application scope and returned. Subsequent calls to the same method will pull the query from the application scope rather than recreating it time and again.
Usage
This plugin provides four methods for use in your controllers: getUSStates(), getCanadianProvinces(), getUSStatesAndCanadianProvinces() getCountries(). All methods do not accept parameters and all return a query. Each row in the returned query has two columns: name and abbreviation.
Examples
Once installed, the plugin is quite easy to use. In the desired controller(s), call the plugin method you want to access.
<cfscript> // this is an excerpt from a controller called Users function new(){ usStates = getUSStates(); countries = getCountries(); user = model("User").new(); renderPage(layout="admin"); } </cfscript>
In the relevant view, you could loop over the queries and output the data or use the queries in a select() call along the lines of the following:
<cfoutput> <div class="form_field"> #select(objectName='user', property='state', label='State', options=usStates, valueField="abbreviation", textField="name")# </div> <div class="form_field"> #select(objectName='user', property='state', label='State', options=countries, valueField="abbreviation", textField="name")# </div> </cfoutput>