What are Live templates?
Live templates are predefined functionalities in almost all JetBrains IDE programs.
When can we use them?
They are recommended if you want to increase your productivity by placing the code line that you would probably use the most into the Live template. It will save your time in further coding. Most people that are working in IT are looking for ways to cut time spent in development so they could do other stuff. Like working on some personal projects, or, in my case, playing darts with my colleagues. This is one example of achieving that.
How can it be implemented?
My advice is, whenever you come across a block of code that most likely will be used occasionally in a short period of time, take a few minutes to add it inside Live templates in order to save time by calling it with a small change.
Dialogue with live templates can be found inside Preferences -> Editor -> Live Templates and it looks like this:
Even when you install PHPStorm, you can find some predefined templates which can be a good base for you to learn how to create a structure for new ones.
You can also choose the way you want to call templates. It can be ‘Tab’, ‘Space’, or any other key combination.
Let’s go through an example of adding one template for Laravel Framework. One of the ideas is to use it as relations inside models. The first step is to click on `+` and choose `Live template`.
Next, you need to fill out a new template.
- Abbreviations (text that you will type in order to call the template)
- Description (description popup)
- Template text (template body)
- Context (the context in which the template applies)
- Edit variables (dynamic part of the template)
- Expand (button to call the template)
After adding the template, next thing is to continue with editing the dynamic part of the template by clicking on `Edit Variables`. Save all progress and go back to the Editor.
Let’s see what happens if you open Post model class and you add-relation with User class.
By typing template abbreviation that you`ve just created, you will get a popup with a suggestion.
Use the `Tab` button to fill out the block code on places where the cursor is and places you have chosen to enter manually. Every next Tab press will lead you to the next place until they are all filled out.
The final look of block code would look like this.
When using `Live template` is not recommended?
If you are a beginner and want to go through the complete learning process step by step, then it would be better not to use this functionality. Later, when you need to be faster, use Live templates and save your time.