Schedule a Free Consultation

Live Templates Provide Code Snippets Library in PhpStorm

Read Article

Using Live Templates in PhpStorm, developers insert code snippets and provide values for parameters.

For quite some time, PhpStorm has been one of the leading development tools, as it allows developers to apply their ideas into code with impressive speed. Among the features that stand out, PhpStorm’s ability to navigate between files and auto-complete keywords will likely impress professional developers. Furthermore, PhpStorm is unique in that it deals exclusively with PHP, but it is also a team player because it shares the extensibility of other multi-language purposed IDEs.

We Feel the Need: the Need for Speed

We are all familiar with this scenario: jQuery library is already available on our development machine and we want to use a standard jQuery construct. Tired or under pressure, we do not remember the syntax, and the last thing in the world is to go look for it. Do you remember what you wished for? “I wish there was a simpler way to do this so I can simply insert the code and move on!” PhpStorm users rejoice! Now you can!

In this article, we describe a method to create AJAX code templates, insert each code item in your code file, and easily replace template parameters with actual values. As we looked at another code development tool, we noticed a plugin that offered such functionality, and we realized we could implement it for PhpStorm, without resorting to plugins.

You can think of it as a template library at your disposal. In addition to increased productivity, using template libraries will bring developers additional benefits:

  • Sharing their template library with fellow
  • Importing and start using template libraries
  • Contributing to a shared template library by adding items
  • Promoting company-wide coding styles

Inside PhpStorm, reside Live Templates – a collection of folders and files that assist developers in creating software components from already defined items. The template library is saved here, and it becomes readily available to use.

Adding Template Snippet

In our example, we have created a library we intend to have available only when working on a .js file.

Live Templates Provide Code Snippets Library in PhpStorm - WPRiders Article

By default, a template placed in Live Templates is available in any file context, but we used the ability to specify which file type the library is associated with. In short, inserting a jQuery template works as follows:

  1. In your .js file, as you type “jq” the list of available library snippets shows up.
  2. Choose the snippet of your choice, say jqAjax.
  3. As the snippet is inserted, the cursor is located before $METHOD$
  4. Type the method value, press Enter, and the cursor jumps to $URL$
  5. Type the url value, press Enter, and the cursor jumps to $END$ — a special placeholder indicating the end of placeholder area.

To create a placeholder, such as $METHOD$, simply place the words between a heading $ and a trailing $, avoiding any spaces. Also, you must remember the two placeholders that have a predefined used: $END$, $SELECTION$.

Live Templates Provide Code Snippets Library in PhpStorm - WPRiders Article

Snippet Elements

When creating a snippet and placing it in a library, you have to fill out the following elements so you can locate each snippet:

  • Abbreviation – the keyword used to locate the snippet
  • Description – text providing a quick and clear description of snippets role
  • Template text – the area used to type the snippet content
  • Change – press it to select which file types the snippet is associated with

We have found it useful to provide a prefix to your snippet for a fast and easy identification.

Sharing Live Templates

To share a specific group of Live Templates, you will use an XML file that PHPStorm will generate when you create new Live Template Group. On OS X El Capitan, the XML files are located at /Users/<your user>/Library/Preferences/PhpStorm<version_number>/templates

Live Templates Provide Code Snippets Library in PhpStorm - WPRiders Article

The video clip below shows how you can use code snippets.

Live Templates Provide Code Snippets Library in PhpStorm - WPRiders Article

We are sharing with you our jQuery Live Templates. Let us know if you find them helpful: https://github.com/WPRiders/PhpStorm_Live_Templates

Navigate to

You Might Also Enjoy These Digital Marketing Articles:

How to Choose a WordPress Ongoing Development Agency - WPRiders Article
How to Choose a WordPress Ongoing Development Agency
Only someone that hasn’t run a business website seriously still considers that the work is done when the website is finished.Most of the time, that’s actually when the complex work starts. Always there is something that needs to be done. You have to add new pages, update the old ones, add new products or remove […]
How To Build Long-Lasting Relationships With Your Clients When You Have A WordPress Website
For any business, losing customers can really hurt your bottom line. That’s why smart companies know customer retention is just as important as acquisition. In fact, according to Neil Patel, “Acquiring a new customer can cost up to 7 times more than selling to an existing customer.” As a business owner or marketing decision-maker, you […]
Test If Flexslider Loaded
Test If Flexslider Loaded
Recently I had a mini project for a client and I had to test if Flexslider has been shown on the screen in order to further modify it. Subscribing to the startcallback didn’t work because the slider was shown by the theme and not by my code. jQuery(document).ready(…) didn’t work either. The slider was there, […]