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:

Top 7 Worst WordPress Maintenance Mistakes - WPRiders Article
Top 7 Worst WordPress Maintenance Mistakes
At WPRiders, we have been working on both small and complex WordPress projects for over seven years. We also do maintenance work for WordPress websites, so we’ve heard plenty of horror stories about the top 7 worst WordPress maintenance mistakes made by customers who came to us for rescue. Without naming any names, below you have are […]
Categorize WordPress Comments in Genesis Framework - WPRiders Article
Categorize WordPress Comments in Genesis Framework
TL;DR: If You Want to Download the FREE Comments Categories Plugin for Your Own Use, Click Here We have created a new version from scratch that is publicly available as a contribution to the WordPress community. Have you ever needed to categorize comments added to your site? WordPress, our favorite Content Management System (CMS) allows […]
Translate Plugin From Child Theme
Translate Plugin From Child Theme
Ever needed to translate some strings from a plugin so as to be able to further upgrade the main plugin without losing your translations? I did. This is because there are some languages around for which there are no default translations in plugins like WooCommerce Social Login or WooCommerce Bookings. Unfortunately there’s no standard way […]