New Feature: Playlist Center! Pick a topic and let our playlists guide the way.

Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

Using snippets

From: ColdFusion Builder Essential Training

Video: Using snippets

Little has affected my day-to-day productivity in last few years quite as much as snippets. First off, what is a snippet? A snippet is that one block of code you find yourself typing or copying and pasting over and over again, every single day. So, what makes for a good snippet? A good snippet is usually a short block of code. Let's create a couple of snippets to see just how useful these can be. In the login.cfm file, I have my Login Form halfway done. I have my form down here at the bottom. I have one row with my username. You obviously need another row for the password.

Using snippets

Little has affected my day-to-day productivity in last few years quite as much as snippets. First off, what is a snippet? A snippet is that one block of code you find yourself typing or copying and pasting over and over again, every single day. So, what makes for a good snippet? A good snippet is usually a short block of code. Let's create a couple of snippets to see just how useful these can be. In the login.cfm file, I have my Login Form halfway done. I have my form down here at the bottom. I have one row with my username. You obviously need another row for the password.

And at the top, I have a cfparam that sets a default value for my username. So, these are the kinds of things that I do every single day, adding cfparams, adding table rows with labels and inputs. So, we are going to turn these into snippets. The first thing you need to do to create a snippet is make sure that the Snippets view is showing. By default, in CFBuilder, the Snippets view is down here in the lower right-hand corner. If you don't see your Snippets view, choose Window > Show View and Snippets. Let's first go over the icons at the top of the Snippets view.

The first one is a Refresh icon. Your snippets are actually stored on your hard drive, and I will show you where those are at. Open up your Preferences and type the word snippets. Click on ColdFusion and Snippets. You can see exactly where your snippets are stored. They are in your Documents folder, inside of your workspace, in a hidden metadata folder, and then snippets. You can choose a different location if you would like your snippets to be easier to get to, i.e. not inside of a hidden folder.

Or if you are sharing snippets with multiple people, perhaps on a network drive, you could also point this to a shared folder. Let's click Cancel and go back to the Snippets view. The next button actually inserts a snippet. We will use that one so we have created one. The next one is to create a new snippet, edit a snippet, delete one, and create and delete packages. And packages in this sense are folders to store your snippets. So, you can have a folder for layout snippets or form based snippets, however you would like to organize them.

But once you have 10 or 15 items in here, sometimes it gets a little bit difficult to find exactly what you want. Let's create that cfparam snippet by selecting our code. So, I don't like to type if I can avoid it. We will copy it, click Plus, and here's the Snippet Details dialog. We will give our snippet a name. I think CFParam make sense. We will leave Trigger Text blank for now. We will add that back in a bit. And then I can give it a description. Add a simple cfparam tag. And then I have a Start and an End Block.

A cfparam tag does not have an end tag, so we are not going to use the End Block, but I am going to paste in my Start code and then remove FORM.username. So, now I have a cfparam tag with empty name and value attributes. Click OK. And I can now see the CFParam snippet inside of the Snippets view. Down here in the Preview, it will show me exactly what will be inserted when I insert the snippet. Go back to the code editor and I want to add a new cfparam on Line 3. So, I place my cursor where I want the snippet to be, and I can either double-click or click the Insert Snippet button.

So, I double-click. It adds my cfparam. And then I have to go back up here and type in FORM.password to finish up my cfparam tag. So, how can I make this snippet more useful? First, I don't like to take my hands off the keyboard if I can avoid it, reaching back and forth for the mouse. So, I am going to undo this and use some trigger text so I can insert the snippet directly from my code. I will select the snippet and click Edit. I will go to the Trigger Text field and I am going to put cfp. So, when I type cfp in my code editor, I can press the keyboard shortcut to have that snippet inserted for me. Click OK.

Now go back to the code editor, type cfp, and then Command+J on the Mac or Ctrl+J on Windows, and that inserts the snippet directly. Now, I still have to use the arrow keys or my mouse to go back and insert the name of the variable I want to param, which is also annoying. So, I will undo that one more time, and we will go back to our snippet and use some data placeholders, so that I can actually create a dialog when I insert the snippet.

Inside of my name attribute, I will type $${} and then I am going to give my attribute a name. In this case, we will call it Variable Name. Then I am going to do the same thing for the value. $${Default Value}. Now when I insert the snippet, a dialog will pop up and prompt me for the variable name and the default value. Click OK. Go back to Line 3, type cfp, press Command+J or Ctrl+J, and there is my Replace variables dialog for the snippet.

So, I want this to be FORM.password. I will leave the default value blank. If I wanted to, I could click this little arrow here to the right and it will expand the text field if I need to enter in more than one line. Click OK. And there's my next cfparam. I can press Enter and do another one, FORM.login, Default Value, false, press Enter, and I can continue on a line at a time to add cfparam tags. Now that's handy, but you can do more with the variables inside of a snippet than just have simple prompts.

Let's undo this last cfparam, and go down and take a look at my table row here for my username. When I am building data entry tables, almost every single one of my rows is formatted just like this. The only thing that changes is the ID for the text field, the label for that field, and the value that gets placed inside of that field. So, this is another perfect candidate for a snippet. I will select this entire row and copy it, and we will create a new snippet. We just need to complete the dialog the same way we did with out cfparam.

We will give it a name. I will call this Form Input Row. Now I need to decide on some trigger text. I will name it f for form, inp for input, and then row, finprow, and this Inserts a new form input row. Then for my Start Block, I will paste in the code that I copied out of the code editor. The first thing I will do is remove all the leading tabs, because CFBuilder is actually smart enough to know that if you insert a snippet with two tabs in front of it, it will indent the entire snippet block by those two tabs.

So, I have removed all the tabs. Now I just need to go through and place all of my variables into the code. So, for the ID, I will do $${ID}. And every place I need to use that ID, I will just copy it and paste. I will only have to enter in the ID once, but CFBuilder will replace the ID in every instance inside my snippet. The next thing I need is a label, so $${Label}.

Then I need to specify the type for the cfinput, but I don't actually want this to be a text field that I type in a value for. I only have five valid choices for the type. So, I am going to type in Type: and then a pipe separated list of the valid values. These are text, pipe, hidden, radio, check box and password. Then I will close my brace. And the last thing I need to do is specify my scope for the variable.

$${Scope:Arguments|Session|FORM|URL}. Be careful that all your braces are properly matched. If your snippet syntax is incorrect, when you try to insert it into the code editor, you are not likely to get any error at all. It just simply won't insert. So, double check that you have all of your braces matching. And once you know everything is right, click OK.

Now, back in the code editor, I will press Enter, type my trigger text. And case does matter, so make sure it's all lowercase, if your trigger text was all lowercase. Press Command+J or Ctrl+J, and our Replace variables dialog comes up. For my ID, it will be password. My label will be Password. For the type, I can press the little dropdown list and choose the type, but I could also type in a value here. So, if I wanted it to be a submit, I could type in Submit. I will choose Password for now.

Then choose my scope. Again, I could type in a custom scope. If this was coming from a record set, I could type the name of my record set in here, but I am going to choose FORM, and click OK. Notice that each of my lines in my snippet was indented by two tabs. So, I didn't have to actually add all of those tabs into the snippet. I hope the two snippets we created here will save you hours of typing over your development career, and that you can see just how powerful snippets can be in your day-to-day coding.

Stop and take a look at what you spend most of your day doing and spend a few moments to automate those tedious tasks with a few well-defined snippets.

Show transcript

This video is part of

Image for ColdFusion Builder Essential Training
ColdFusion Builder Essential Training

40 video lessons · 3837 viewers

Daniel Short
Author

 
Expand all | Collapse all
  1. 7m 48s
    1. Welcome
      59s
    2. Installing ColdFusion Builder
      2m 20s
    3. Installing ColdFusion Builder as a plug-in
      3m 18s
    4. Using the exercise files
      1m 11s
  2. 17m 42s
    1. Workspace overview
      2m 25s
    2. Organizing your workspace
      6m 19s
    3. Adding and selecting workspaces
      2m 53s
    4. Installing and managing plug-ins
      6m 5s
  3. 12m 12s
    1. Creating projects
      2m 11s
    2. Importing projects
      2m 48s
    3. Creating project working sets
      2m 23s
    4. Managing projects
      2m 22s
    5. Linking folders
      2m 28s
  4. 23m 2s
    1. Configuring and managing ColdFusion servers
      6m 26s
    2. Using the Services Browser
      4m 15s
    3. Using RDS Dataview and Fileview
      4m 52s
    4. Using FTP
      7m 29s
  5. 18m 10s
    1. Creating editor profiles
      2m 6s
    2. Using Code Assist
      5m 43s
    3. Setting code coloring
      4m 5s
    4. Using shortcut keys
      3m 5s
    5. Working with outlines and syntax checking
      3m 11s
  6. 37m 40s
    1. Using code completion
      7m 25s
    2. Using snippets
      9m 23s
    3. Using SQL editing and the Query Viewer
      5m 55s
    4. Working with components
      7m 27s
    5. Working with variable mapping
      2m 14s
    6. Navigating code and documents
      5m 16s
  7. 13m 12s
    1. Getting set up
      3m 50s
    2. Using the ColdFusion debugger
      5m 37s
    3. Watching logs with Tailview
      3m 45s
  8. 13m 8s
    1. Installing extensions
      4m 5s
    2. Using the CFC Generator extension for ColdFusion 8
      4m 52s
    3. Using the CFC Generator extension for ColdFusion ORM
      4m 11s
  9. 34m 5s
    1. Building a simple contact form extension
      3m 42s
    2. Creating user interfaces
      8m 55s
    3. Packaging the extension
      3m 55s
    4. Building the handler
      8m 10s
    5. Using the Builder extension
      9m 23s
  10. 25s
    1. Goodbye
      25s

Start learning today

Get unlimited access to all courses for just $25/month.

Become a member
Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold

Are you sure you want to delete this note?

No

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.