Pragmatic Snippets

Previously, I showed my top seven snippets. Let us use some of the snippets together to make a new component. To follow along, you should install the snippets.

Next, we will consider our output. We will design a form bean component for a user management form consisting of the following values:

  • Email
  • FullName
  • Status
  • Username

Create a new component called snippet.cfc. Paste the four attributes listed above three times in snippet.cfc . Your file should look like the one below:

<cfcomponent>
         
         Email
         Fullname
         Status
         Username

         Email
         Fullname
         Status
         Username

         Email
         Fullname
         Status
         Username
      
      
      </cfcomponent>

Begin at the last block. This will be as block of getters and setters. To be as efficient as possible, put your cursor at the beginning of Email, select the word, cut it, type getset and your snippet key combo. (Either CTRL+SHIFT+. or CTRL+J). When the prompt appears, paste the text Email, then type an s character in each box to select 'string'. Repeat this for each of the four attributes.

When complete, and with some judicious spacing, you end up with this:

<cfcomponent>
   
         
         Email
         Fullname
         Status
         Username

         Email
         Fullname
         Status
         Username

         <!--- Usage: GetEmail / SetEmail methods for Email value --->
         <cffunction name="getEmail" access="public" output="false" returntype="string">
            <cfreturn variables.instance.Email />
         </cffunction>
         
         <cffunction name="setEmail" access="public" output="false" returntype="void">
            <cfargument name="Email" type="string" required="true" />
            <cfset variables.instance.Email = arguments.Email />
         </cffunction>
         
         <!--- Usage: GetFullname / SetFullname methods for Fullname value --->
         <cffunction name="getFullname" access="public" output="false" returntype="string">
            <cfreturn variables.instance.Fullname />
         </cffunction>
         
         <cffunction name="setFullname" access="public" output="false" returntype="void">
            <cfargument name="Fullname" type="string" required="true" />
            <cfset variables.instance.Fullname = arguments.Fullname />
         </cffunction>
         
         <!--- Usage: GetStatus / SetStatus methods for Status value --->
         <cffunction name="getStatus" access="public" output="false" returntype="string">
            <cfreturn variables.instance.Status />
         </cffunction>
         
         <cffunction name="setStatus" access="public" output="false" returntype="void">
            <cfargument name="Status" type="string" required="true" />
            <cfset variables.instance.Status = arguments.Status />
         </cffunction>
         
         <!--- Usage: GetUsername / SetUsername methods for Username value --->
         <cffunction name="getUsername" access="public" output="false" returntype="string">
            <cfreturn variables.instance.Username />
         </cffunction>
         
         <cffunction name="setUsername" access="public" output="false" returntype="void">
            <cfargument name="Username" type="string" required="true" />
            <cfset variables.instance.Username = arguments.Username />
         </cffunction>
      


</cfcomponent>

Not too shabby eh? Now we need an init function. Cut both sets of remaining attributes into the clipboard. Type fun and your snippet trigger text. When the dialogue box appears, fill in the following information:

Hint: I perform initialization for this component
   Name: init
   Access: public
   Return Type: snippet
Then paste the attributes. Now for the first four attributes we will work similar to our getset block by cutting the attribute, then fill out the information as so:
Type: String
   Required: False
After you close the dialogue box, the cursor should be inside the name attribute. Paste the attribute then wash, rinse and repeat. After you finish each of the four, type default="" and copy paste it into the argument tags. Your file should look like such:

<cfcomponent>
   

      <!---    Date: 7/9/2007 Usage: I perform initialization --->
      <cffunction name="init" output="false" access="public" returntype="snippet" hint="I perform initialization for this component">
         <cfargument name="Email" type="string" required="false" default="" />
         <cfargument name="Fullname" type="string" required="false" default="" />
         <cfargument name="Status" type="string" required="false" default="" />
         <cfargument name="Username" type="string" required="false" default="" />      
         
         Email
         Fullname
         Status
         Username
         
      </cffunction>         


         <!--- Usage: GetEmail / SetEmail methods for Email value --->
         <cffunction name="getEmail" access="public" output="false" returntype="string">
            <cfreturn variables.instance.Email />
         </cffunction>
         
         <cffunction name="setEmail" access="public" output="false" returntype="void">
            <cfargument name="Email" type="string" required="true" />
            <cfset variables.instance.Email = arguments.Email />
         </cffunction>
         
         <!--- Usage: GetFullname / SetFullname methods for Fullname value --->
         <cffunction name="getFullname" access="public" output="false" returntype="string">
            <cfreturn variables.instance.Fullname />
         </cffunction>
         
         <cffunction name="setFullname" access="public" output="false" returntype="void">
            <cfargument name="Fullname" type="string" required="true" />
            <cfset variables.instance.Fullname = arguments.Fullname />
         </cffunction>
         
         <!--- Usage: GetStatus / SetStatus methods for Status value --->
         <cffunction name="getStatus" access="public" output="false" returntype="string">
            <cfreturn variables.instance.Status />
         </cffunction>
         
         <cffunction name="setStatus" access="public" output="false" returntype="void">
            <cfargument name="Status" type="string" required="true" />
            <cfset variables.instance.Status = arguments.Status />
         </cffunction>
         
         <!--- Usage: GetUsername / SetUsername methods for Username value --->
         <cffunction name="getUsername" access="public" output="false" returntype="string">
            <cfreturn variables.instance.Username />
         </cffunction>
         
         <cffunction name="setUsername" access="public" output="false" returntype="void">
            <cfargument name="Username" type="string" required="true" />
            <cfset variables.instance.Username = arguments.Username />
         </cffunction>
      
</cfcomponent>

We will finish off the component by adding the cfset tags that execute the setters. Paste this block 4 times:

<cfset set( arguments. ) />
Then put the attribute in between set and () as well as after the arguments. Finally add the return statement. The specific block should look like this:

<cfset setEmail( arguments.Email ) />
<cfset setFullname( arguments.Fullname ) />
<cfset setStatus( arguments.Status ) />
<cfset setUsername( arguments.Username ) />
         
<cfreturn this />

Congratulations! Wasn't that fun? When complete, you should have a nice Form Bean ready to use. For posterity, the complete code looks like this:

<cfcomponent>
   

      <!---    Date: 7/9/2007 Usage: I perform initialization --->
      <cffunction name="init" output="false" access="public" returntype="snippet" hint="I perform initialization for this component">
         <cfargument name="Email" type="string" required="false" default="" />
         <cfargument name="Fullname" type="string" required="false" default="" />
         <cfargument name="Status" type="string" required="false" default="" />
         <cfargument name="Username" type="string" required="false" default="" />      
         
         <cfset setEmail( arguments.Email ) />
         <cfset setFullname( arguments.Fullname ) />
         <cfset setStatus( arguments.Status ) />
         <cfset setUsername( arguments.Username ) />
                  
         <cfreturn this />
                     
      </cffunction>         


         <!--- Usage: GetEmail / SetEmail methods for Email value --->
         <cffunction name="getEmail" access="public" output="false" returntype="string">
            <cfreturn variables.instance.Email />
         </cffunction>
         
         <cffunction name="setEmail" access="public" output="false" returntype="void">
            <cfargument name="Email" type="string" required="true" />
            <cfset variables.instance.Email = arguments.Email />
         </cffunction>
         
         <!--- Usage: GetFullname / SetFullname methods for Fullname value --->
         <cffunction name="getFullname" access="public" output="false" returntype="string">
            <cfreturn variables.instance.Fullname />
         </cffunction>
         
         <cffunction name="setFullname" access="public" output="false" returntype="void">
            <cfargument name="Fullname" type="string" required="true" />
            <cfset variables.instance.Fullname = arguments.Fullname />
         </cffunction>
         
         <!--- Usage: GetStatus / SetStatus methods for Status value --->
         <cffunction name="getStatus" access="public" output="false" returntype="string">
            <cfreturn variables.instance.Status />
         </cffunction>
         
         <cffunction name="setStatus" access="public" output="false" returntype="void">
            <cfargument name="Status" type="string" required="true" />
            <cfset variables.instance.Status = arguments.Status />
         </cffunction>
         
         <!--- Usage: GetUsername / SetUsername methods for Username value --->
         <cffunction name="getUsername" access="public" output="false" returntype="string">
            <cfreturn variables.instance.Username />
         </cffunction>
         
         <cffunction name="setUsername" access="public" output="false" returntype="void">
            <cfargument name="Username" type="string" required="true" />
            <cfset variables.instance.Username = arguments.Username />
         </cffunction>
      


</cfcomponent>

Related Blog Entries

Comments
Hi Dan,

I have followed your tips and unzipped the files to my Eclipse, then restart my Eclipse. However, after I press the CTRL+J, I got Error message:
"Insert Snippet" did not complete normally. Please see the log for more information.
Reason: java.lang.NullPointerException.

Could you let me know if I miss anything?

Thanks,
Jerry
# Posted By Jerry | 10/29/07 10:38 AM
Hi Jerry,

You will have to manually edit each snippet to put in the trigger text and the name/description. If you have not done this, navigate to the snip tree view, and use the icons to create a new snip for each snippet defined in the tutorial.

Hope this helps,

DW
# Posted By Dan Wilson | 10/29/07 10:47 AM
Thanks for your help, Dan. Actually, I found my problem is caused by the setting of File Paths on my Eclipse. It was some other directory other than your given one, I think it was set as default by CFEclipse. I just copied everything and modified keycombos.properties file, now it's working fine.

Thanks again for this helpful stuff,

Jerry
# Posted By Jerry Wang | 11/1/07 11:52 AM
I know this post is old, but it is one of the top results for cfeclipse snippets searches in google.

<cfset setX() = arguments.x />

should be

<cfset setX(arguments.x) />
# Posted By justin treher | 7/18/08 2:57 PM
@justin,
Thanks for pointing this out. I've updated the post.

Happy Snippeting!

DW
# Posted By Dan Wilson | 7/18/08 3:05 PM
BlogCFC was created by Raymond Camden. This blog is running version 5.9.001. Contact Blog Owner