Intro to Live Templates0:00
All right, let's keep going. So, next up, why don't we take just a few minutes, it's all we need, to discuss live templates or, effectively, code snippets. All right, here we go. So, I'm going to open up my routes file, and, yeah, let's register a new route that loads a view, and how about, if you visit the homepage, we load the view called Welcome. And notice, as I start typing, because we're using the Laravel IDEA plugin, we have auto-completion for our views, which is really cool. All right.
we have auto-completion for our views, which is really cool. All right. So, now, I can command-click to go directly to that view. And, yeah, at the very top, this is the default welcome page that you get with Laravel. And, actually, let's have a look in the browser, real quick. And, yep, here we go. You've probably seen it a hundred times. Okay. So, anyways, if I switch back, you can see that the Laravel team is inlining the Tailwind CSS library.
Creating Tailwind Snippet1:22
but for all intents and purposes, we are now loading Tailwind, which is cool. But if I switch back, I'm always forgetting what this source path is. So, this is a prime candidate for a live template, and I'll show you how to do it. First up, I'm going to select the whole thing, and then I will hit command-comma to open up my settings, and I'm just going to look for live template. All right, here we go. And, yeah, of course, out of the box, we have a bunch that we can reach for. Things like give me the fully qualified class name for a file, or if I want to echo something or create a public method, and we've already used this many times.
or if I want to echo something or create a public method, and we've already used this many times. So, for example, before I continue, if I want to use this snippet or this live template, the abbreviation is pubf. So, if I were to quickly open up a file, and if I type pubf, there we go. I hit return, and it works. Now, another option is to use a keyboard shortcut, so that I can filter down all of the live templates that I have. And that action, I believe, is called insert live template. Yeah, and you can see on my machine, that is bound to command-J.
And that action, I believe, is called insert live template. Yeah, and you can see on my machine, that is bound to command-J. So, if we give that a shot, this will show me all of the snippets that are relevant for the current file. So, again, if I want the fully qualified class name, notice it's dynamic in this case, so it knew I was in a User class, so it referenced the appropriate path, which is cool. But yeah, in our case, I could hit command-J, pubf, and there we go. Anyways, back on track, command-comma. And yeah, within the PHP section, because we do have a Blade file here, why don't we add a new template?
And yeah, within the php section, because we do have a Blade file here, why don't we add a new template? All right, what should the abbreviation be? Whatever you want, Tailwind, CDN, anything like that. And I'm going to paste in that script reference. Now, if I were to simply apply this and not declare a context, let's see how this works. All right, let's go back to welcome. I'll get rid of this and hit command-J. And now if I start typing Tailwind, I don't see it at all. And further, if I do the full snippet and I try to use tab completion like this,
Setting Template Context3:26
And now if I start typing Tailwind, I don't see it at all. And further, if I do the full snippet and I try to use tab completion like this, it doesn't work. And that's because I did not set a context. So, I can do that right down here. Let's define. Where is this snippet relevant? Well, I could say it's relevant within php and HTML. All right, let's hit apply and try it one more time. Command-J, Tailwind, and now it shows up.
Pest Test Snippet3:48
All right, let's hit apply and try it one more time. Command-J, Tailwind, and now it shows up. Very easy. But now, what about tab stops and things like that? Well, let's do another example. Let's see. This is a fresh install of Laravel, but you can see that I've already pulled in pest behind the scenes. So, why don't we, let's go into the feature section. We have some dummy tests here. Yeah, here we have one.
We have some dummy tests here. Yeah, here we have one. It returns a successful response. It might be nice if I had a snippet to quickly scaffold this basic structure here. So, maybe I could have a live template called it. All right, let's give it a shot. I'm going to copy all of this, go back, and let's create a brand new one here. All right, the abbreviation is it. I will paste all of this in. But, of course, this should be dynamic, and then the body should be dynamic.
Adding Tab Stops4:59
It's right at the bottom, which, again, just takes me out of the flow a little bit. Really, I want the cursor to stop here, and then maybe have a second tab stop that goes to the body of the closure here. All right, so once again, command , and yeah, let's see what we can do here. If we review any of these others, yeah, how about this one? Notice that I can declare dynamic tab stops that are names by using this syntax here. $ and then a name, and then a closing $. Okay, so here, I could say, well, this represents the name of the test, and then here is where I will end. All right, so once again, apply, try it again, it, and notice where the cursor is now.
and then here is where I will end. All right, so once again, apply, try it again, it, and notice where the cursor is now. So I can do something, hit tab, and now it takes me to the next tab stop, and I can begin my test here. Very cool. All right, one more. How about this? One thing I often do with PEST is I will declare tests that I need to do, almost like I'm gathering my thoughts. So for example, I might say, it can fetch all recent posts or something like that.
Todo Snippet and Variables6:01
almost like I'm gathering my thoughts. So for example, I might say, it can fetch all recent posts or something like that. And if I'm not ready to work on this, I will add a call to to-do. And yeah, that ends up being really cool because, again, it's a helpful way to gather my thoughts. So why don't we turn this into a snippet as well called to-do. All right, once again, command comma, or this time I will select everything. I will bring up my control bar with command P or shift shift. And why don't we just look for live template and see what actions are available to me. In our case, we want something like extract or save as live template.
It ends up being a nice workflow. And yeah, really, that's going to take you 95% of the way there. In situations where you have more complicated snippets, just keep in mind that there are dynamic variables that you can reference. So in this case, because I have a name variable within my template here, I can actually bind that, so to speak, to an expression. So for example, if I want to capitalize something or reference a part of the file, like the name of the file, or the name of the class, or the current date, you can do some pretty funky and wild things here using these expressions. And yeah, I would encourage you to have a look here when you get to that point.
