Debug your code faster using custom live templates with PhpStorm - React to WordPress
PhpStorm Live Templates

Debug your code faster using custom live templates with PhpStorm

As a programmer and a team leader, I don’t have time to waste on debugging broken pieces of code. I need to do it fast with little to no mess. To do so, I’m using the help of PhpStorm live templates system

What are Live Templates?

Live templates are shortcodes that connecting to certain code patterns that you can re-use inside of your code with a click of a TAB. That allows you to easily create huge blocks of code by typing only a couple of characters and clicking TAB.

A live template could be as simple as just an echo statement:

echo "$END$";
phpstorm live template eco
Default “eco” Live Template

Or more complex like a foreach loop with key and value:

foreach ($ITERABLE$ as $VAR_KEY$ => $VAR_VALUE$) {
   $END$
}
Foreach loop - phpstorm livetemplate
Default “forek” Live temeplate. Notice at how the value name is changed automatically to single form ($term)

As you can see, PhpStorm live templates, if used wisely, can save you loads of time. Now let’s move to the core of this article –  custom templates.

Debug your code with custom live templates

By now, you should understand: what are live templates and how they work. This part of the article will give you a couple of tools on how to create your own live templates and use them to debug your code.

How to create Live Templates?

It is just like 1 2 3, Just go to your PhpStorm and File > Settings > Live Templates

Creating custom live template in phpstorm
1. Select the programming language and add a template

2. Select live template

3. Set the abbreviation

4. Insert the template, including the preset keywords phpstorm allows.

The “epre” Live template

How it looks in PhpStorm

Without exaggerating, I’m using this template at least 10 times a day while trying to test an array,  debug a failed if statement, or just output a response of a CURL call.

echo '<pre style="direction: ltr; text-align: left">';
print_r($NAME$);
echo '</pre>';
“epre” custom live template
"epre" in action
“epre” in action

The “epret” template

The “epret” template is almost the same as the original “epre” I showed you earlier. As a fan of $_GET page manipulations, this template will allow you to do all the debugging and testing you need on a live website without ruining any user’s experience.

Obviously – use it as little as you can, it is still a bad practice to debug production website. Only users that will go to www.domain.com?debug=1 will see the annoying debugging text.

if ( $_GET['debug'] === 1 ) {
   echo '<pre style="direction: ltr; text-align: left">';
   print_r($NAME$);
    echo '</pre>';
}

epret live template

Now you have the tools to create your OWN live templates

I showed you only 2 of the most common templates I’m using, but you can create thousands of useful, smart, time saving live templates and enhance your everyday experience with PhpStorm.

More about: Best WordPress web hosting in 2018  

Leave a Reply

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