Changes between Initial Version and Version 1 of HowTos/HowToUseHelpers

Show
Ignore:
Timestamp:
11/16/09 23:25:46 (16 years ago)
Author:
cfgrok (IP: 64.30.223.5)
Comment:

Renamed page for alpha sorting on home page

Legend:

Unmodified
Added
Removed
Modified
  • HowTos/HowToUseHelpers

    v1 v1  
     1= How To Use Helpers = 
     2 
     3Model-Glue provides a great separation of concerns when it comes to your web application. It helps you clearly differentiate between views, controllers, and model files. However, sometimes there are utilities that don't quite fit in the MVC design. Consider a utility that formats strings such that URLs and email addresses are automatically linked. Where would you put that? It isn't a controller concern. It isn't a model. You could simply put it in a view file, but then you end up (possibly) repeating the same function in multiple places. 
     4 
     5Model-Glue 3 solves this problem by creating support for what it calls "helpers". Helpers are libraries of user defined functions or CFCs of methods. By placing these files in the proper location. Model-Glue 3 will automatically load them and make them accessible via the new helpers scope. This scope is available to both controller and views file. Let's take a look at a simple example. 
     6 
     7First, you have to know where Model-Glue will look for helper files. Open !ColdSpring.xml and look for the helperMappings setting. Most likely it will look something like this: 
     8 
     9{{{ 
     10<property name="helperMappings"><value>/myapplication/helpers</value></property> 
     11}}} 
     12 
     13If you used the default Model-Glue skeleton application, you should see a helpers folder. Create a new file called udf.cfm and place this UDF in it: 
     14 
     15{{{ 
     16<cfscript> 
     17function formatDateTime(dt) { 
     18    return dateFormat(dt) & " " & timeFormat(dt);     
     19} 
     20</cfscript> 
     21}}} 
     22 
     23Open up one of the views from your site and add this line: 
     24 
     25{{{ 
     26<cfoutput>#helpers.udf.formatDateTime(now())#</cfoutput> 
     27}}} 
     28 
     29Run the site and you should see something like this: 
     30 
     31{{{ 
     3230-Apr-09 09:32 PM 
     33}}} 
     34 
     35Ok, so what happened here? First off, pay attention to how I called the script: helpers.udf.formatDateTime. "helpers" comes from Model-Glue 3 itself. This is the root scope created by Model-Glue 3 to hold all your utilities. "udf" comes from the file name. If I had called the file udfs.cfm, I'd have to use helpers.udfs.formatDateTime. Finally, "formatDateTime" is simply the UDF itself. This exact same call can be made in any controller as well. 
     36 
     37As another example, create a new file in the helpers folder called utils.cfc. Drop the !GoldenBirthay UDF in it: 
     38 
     39{{{ 
     40<cfcomponent output="false"> 
     41 
     42<!--- 
     43Returns a 'golden birthday', or the date when your birthday day of month equals your age. 
     44 
     45@param birthday      The date of birth. (Required) 
     46@return Returns a date. 
     47@author Joshua Siok (jsiok@smp.org) 
     48@version 0, April 30, 2009 
     49---> 
     50<cffunction name="goldenBirthday" access="public" returntype="date"> 
     51    <cfargument name="birthDate" type="date" required="yes"> 
     52    <cfreturn dateAdd("yyyy", day(birthDate),birthDate)> 
     53</cffunction> 
     54 
     55</cfcomponent> 
     56}}} 
     57 
     58Based on the rules discussed before, we know that we can call this method by using the helpers scope, the file name, and the method name: 
     59 
     60{{{ 
     61<cfoutput>#helpers.utils.goldenBirthday(createDate(1973,4,8))#</cfoutput> 
     62}}} 
     63 
     64One last note. The files in the helpers folder (or folders if you have defined more than one) are cached. If your Model-Glue application is not set to reload on every request, you will have to force a reload with the proper URL variable.