Chapter 8. File Templates and Snippets

 

"Are we there yet?"

 
 --Everyone, including you

Coding is a ton of fun, but sometimes, it can be rather repetitive. Snippets (live templates) in PyCharm help you do away with a lot of the receptive parts. However, PyCharm goes further; it even allows you to make file templates so that once you make a new file, for example, a .py file or a .js file, you can have some boilerplate code written for you. You're already using file templates in PyCharm if you haven't changed its default settings. This chapter is all about coding speed, so hold on to your hats!

In this chapter, we are going to take a look at:

  • File templates
  • Snippets (live templates)
  • Surround templates

File templates

File templates

Let's make a new file in PyCharm. The two options that you have, Python file and Python unit test, are file templates. You might have more than what is shown here, but in essence, when you make a new file template, it shows up as an option when you make a new file. So, let's take a look at what's inside these two templates:

File templates

Understanding variables

As you can see in the template (in the preceding screenshot) there is some code to initiate the file. In this case, it's Python Unit Test. You can also see that (indicated by a red arrow); there are some special variables that you can use inside the template. The file templating engine PyCharm uses is called Velocity Template Language (VTL).

Some of the variables are given to you by default. So, $USER is derived from the username that the user has registered to PyCharm with. If you registered with an abbreviation of your name, you might want to change the $USER variable, and you can do it like this:

#set ( $USER = "THE AMAZING SPIDER MAN" )

You can change preconfigured variables and make new variables using #set. The documentation on File and Code Templates provides a good account of the default variables available, but if you want to explore more of VTL, which is an Apache project with tons of documentation, refer to https://velocity.apache.org/engine/releases/velocity-1.5/user-guide.html.

Making new templates

You can make new templates with the (+) icon. You can also copy the existing ones with the copy icon if you want to make slight modifications. Now, it is fairly simple to create new templates with variables in them. But, say, you registered with the username CHEEZIE; although this is a great name for the Internet, a pseudonym is often inappropriate in more formal circumstances.

Making new templates

We saw that we can set $USER with #set, but we don't want to be doing this for every script that has a reusable variable in it. We can use this by using the Includes tab.

We can set the include template here and simply include it at the top of our file in our Python script using #parse:

Making new templates

However, we cannot create your own group. Some other templates such as Setup Script and Flask Main are only available under certain conditions. For example, Flask Main is used as a template when you first create a Flask project using PyCharm.

In most cases, the basic tools that file templates provide you are often more than adequate and can speed up a lot of repetitive tasks for you. You probably won't use this feature very much, but you will certainly use snippets, or as PyCharm likes to call them, live templates, which we will be discussing next.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset