After a template is expanded, variables appear in the editor as input fields. Template variables in live templates enable user input. For parameterized and surround templates things are a little bit more complicated so we are going to create parameterized template below.įor example, we want to have custom table with class and id and one row and three columns inside. If we are writing Simple Template then just enter text here. After that you need to add Template text (you can also have Template text set for you in case you select a block of text and invoke menu Tools | Save as Live Template). Enter abbreviation (name by which we are going to invoke this template) and Description. In order to create a new Custom Live Template go to the Live Templates Settings dialog and click on add ( +) button in order to add new template. It would be quite useful to examine predefined Live Templates in order to understand how is it created. We can see a list of predefined Live Templates by groups.
Just go to Settings | IDE Settings | Live Templates. Let’s check how can we create a custom template. You can also type template abbreviation and use TAB to make it do coding for you. Use hotkeys in order to get list of templates or use it.
#Phpstorm live templates code#
Simple template (some fixed code that expands into plain text) CTRL+J / META+J.We’ve got complete and valid JS code and the only thing you need to do is to change variables. Enter itar abbreviation in JS file and press TAB. Just to understand how does it work let’s check predefined example “Iterate elements of array”. This functionality is available in IntelliJ Idea, P圜harm, WebStorm, PhpStorm, RubyMine, AppCode.Įach live template is identified by a template abbreviation.
#Phpstorm live templates how to#
Today we are going to learn how to create custom live templates in order to type less. There are numerous pre-defined templates for CSS, HTML, XSL, JS including Zen Coding and some others, however, sometimes it’s not enough. Live templates contain predefined code fragments so that you can use them to insert frequently-used or custom code constructs into source code quickly, efficiently, and accurately. Live template is a feature helping you to type less and be more productive easily creating and using pattern structures in your code. One of the most important things we should think about while coding is performance – these code patterns are 100% reusable, we don’t use our brain in order to employ it – just mechanically type and this typing requires some time, so that the key factor in making your coding a high-speed one is to spend as little time on routine coding as possible. This is about the process, rather than the code.Usually in our code we have a lot of pattern structures such as conditionals (if, else), loops (for, while, foreach), tag structures (HTML tables, ul listings) and others. It's not so important to understand what this code does, so if you are unsure then don't worry about it. If you have ever worked with Symfony's Form Collection then you will likely have had to type this out at least once.
This example comes directly from the video. This frees up valuable brain space, allowing me to remember more important things like old Simpsons' quotes (true), and relatives birthdays (no true). I'm particularly bad at remembering how to correctly define a Symfony controller from scratch, or the exact syntax and layout for a Symfony Form Type.įor these, I use custom live templates. Read some docs / blog post / StackOverflow article, copy the interesting bits, paste into your code and see if it works. If you work like I do, copy/paste is a common occurrence. Live Templates are the secret sauce to increasing your development productivity inside PHPStorm. What you may not know is that you are not only limited to the code generators that PHPStorm comes with out of the box. The code generation feature is particularly useful.
#Phpstorm live templates full#
Ok, I am aware not everyone uses (or loves) PHPStorm, but those of us that do recognise that it's full of time saving awesomeness, from simple things like highlighting unused variables through to more interesting things like code generation (easy getters/setters), and being able to determine whether we have implemented all the required interface methods, etc. I think that by now 100% of the entire community has concluded that PHPStorm is the best thing sliced Vim )