Using Snippets in CF Builder

As a coder you will find yourself writing the same code over and over, or maybe copying an pasting blocks of code that is required in several files of your application; so you open the file that contains the code you want to replicate, then select and copy the text to finally open the destination document and paste the code.
What if I could save that block somewhere in my favorite editor (CF Builder) and then invoke it whenever I need it and even add an extra custom text at the moment of the insertion, well Snippets can help you with this problem allowing you to save time in your coding processes:

Defining a snippet

  1. Open the snippets panel/view, you can do it from the tool bar or the Window menu Window/Show View/Snippets
    Snippets_Access
  2. Select the package in which the new snippet will be created and click on the “Create new snippet” button on the Snippets panel (#3 on image), the snippet will be saved inside the package selected on the snippets list view.Think of Packages as Folders to organize your snippets.
    Snippets panel and tools

    Figure 2

    Snippet details

    Figure 3

  3.  Next you will need to provide some fields to complete the snippet definition:
    • *Name. This is a required field used to identify your snippet in the Snippets view.
    • Trigger Text. There are 2 ways of inserting a snippet, by selecting the snippet on the list and click on “insert selected snippet” button  (see Figure 2 – #2), or typing a word or “trigger text” that CF Builder will recognize and then press simultaneously the keys [CTRL] + [ J ]  Note: Trigger text is case sensitive so “Component” is not the same as “component” to keep in mind when naming your snippets.
    • Description. Some text that gives you a hint of what this snippet does.
    • *Start block. Here you will define the body of your snippet, if you need to wrap some text between to predefined blocks, the the first part goes here and the second part on the End Block field.
    • End Block. See Start Block field
  4. Click Ok

Inserting a snippet

Method No. 1 Trigger Text

  1. Type the trigger text and press simultaneously [CTRL] + [ J ]

Method No. 2 Snippets toolbar

  1. On the Snippets view, select the desired snippet and click on the “Insert selected snippet” button, the block of code will be inserted at the current cursor position in your document.
  2. If you defined dynamic fields (variables) inside your snippet, a window prompt you to complete the values of those variables

Dynamic text on snippets

To actually create a boilerplate that will contain some dynamic text that can be provided by the user at the time of insertion, you need to create variables.

Defining a variable

Inside your Start Block and/or End Block fields type: $${[name of the field]:[default values]}, to explain this take a look on Figure 3 – Snippet Details Window and observe how the snippet is defined.
When inserting a snippet, if some variables were defined a window asking for the values of these variables will show.

Author has a default value “Angel Chrystian Torres”. (Singular value)
Accessors has a list of values in this case two, but it can be more true|false (List of values), to separate the items on the list use “|” (pipe symbol), the first item on the list will be the default selected item. Variables declared in this way will be shown as a dropdown list on the window prompt.

Figure 4

Figure 4


Sharing your snippets

With all the different cloud services been offered nowadays a simple solution of sharing your snippets or keeping them always available as long as you have an internet connection or your synchronization configuration, is to use whatever of your “Cloud drives” as Dropbox, Google Drive, One Drive etc. Normally in windows, all these applications create a folder inside your user directory for example X:UserschrysDropbox.
Go to Window -> Preferencies -> ColdFusion -> Snippets

Snippet_path
Type the path to the snippet folder and press OK.
Now you can start creating snippets, or copy some files that your coworker sent you in to the Folder you just configured as the path to the snippets.

Click here to download some useful snippets

Leave a reply

Your email address will not be published. Required fields are marked *

By submitting this form, you accept the Mollom privacy policy.

Related