 Last time on our template mini-series, we discussed creating a template and getting it to work in Visual Studio. Now we're going to talk about how you can continue expanding on that by adding parameters. So learn more on this episode of Visual Studio Toolbox. Hey everybody, welcome back to another episode of Visual Studio Toolbox. I'm your host Leslie Richardson and I'm joined once again by senior PM Syed Hashimi. Welcome back. Hello. Great to be here. Thank you. Yeah. Long time to see. So in the last episode, we talked about creating a template and then getting it to work within Visual Studio. So what are we going to talk about today? Yeah, that's right. So today, we're going to talk about how can I take an existing template and add one or two parameters to that, and then we'll see how that works in Visual Studio as well. All right. Well, I'd love to just dive right in, because I'm sure everyone wants templates that go beyond just the hello world type setup, right? There's no doubt about it. All right. So let's go ahead and jump right into it here. So I've got my GitHub open here and a reminder for how to get to this repository is aka.ms-netcore-templates. We basically just show what we're going to do here. So let me go into the my command here, and inside the template.json file is where we're going to start adding these parameters. So everything that I'm going to do here today is basically just right here inside this inside these files here. I'm just going to snap it here. Let me open up the demo solution here. That's got the templates and all that sort of stuff. All right. I'm going to go ahead and open up my template.json file here, and here's where we're going to go in and start adding some parameters here. By the way, the first parameter, there's a very special parameter here, and that's for framework. So what's the target framework for this project? It's important to add this for a number of reasons. One, whenever you have parameters inside of a project, and if you want those parameters to appear in Visual Studio, the framework parameter always has to be, it always has to be declared there. And it's also good for users just so that way they know what target framework is going to be used here to create, is going to be used when they create a project there. So let me just go ahead and copy this content from here, and then we'll kind of just describe more about this. So symbols, I'm going to paste that in. Let me double check my project here real quick. So if I go take a look at my CS project, I can see the target framework is actually net five. Gonna take that, and then we'll go ahead and fix this. So instead of the choice being net core up 3.1, it should be net five, and target net five, and then the same for these replacements here. All right, so this is basically, it's called the choice parameter that has one choice. So it's like a dropdown with one particular value there. All right, so now let's also go back and take a look at what can we do for other parameters here. So in the code, the demo kind of project had an author name somewhere inside the code, and then there was a replacement that was happening for that. So let's go ahead and add something like that to the code that we have here. So let me just go and grab author name here. It's a created by author name, period. All right, so now what's gonna happen is I'm gonna create a parameter that users can specify a new value by who this has been created by, right? And what I'll do is let me take this, and maybe we'll put it into, you know what I'll do is I'm actually just gonna put this inside my project file as well. Maybe like as a comment or something. All right, there we go, great. All right, so now what I'll do is let's go back to the template.json. We're gonna go ahead and add the new parameter here, author name, the type here is parameter. And you notice I'm just kind of IntelliSensing my way through all of this stuff, right? Yeah, must be super helpful. Not having to remember every single piece of syntax. Yeah, it's very difficult to remember all these things, you know? Absolutely. So I just specified a default value. So when this template gets instantiated, you know, either from, or actually when this template gets used from .NET new, if that parameter is not specified, this default value will be used. And then that will be the default value in Visual Studio that gets kind of plugged in there as well. Cool. So if you put author name anywhere else within this template, it would also pick up on the schema or this role would be applied to every instance. That's right, that's right, that's right. Also file names as well. So if you've got a file that's called, you know, author name, blah, blah, blah, then author name, part of that would get modified to whatever the user specified here. And there's also cleanup that happens to let's say, for example, let's say for example, the user specifies some value that can't be used in a file name. The template engine will basically take care of fixing that up to where it would be a valid file name there. Right, sorry. So now I've added two parameters here. We've got the framework parameter that's a choice and it's just got one choice for .NET 5. We also got the author name parameter and the default is author demo and it's a text. Let's go ahead and reinstall. We'll go ahead and do pretty much everything here. So I'll say reset templates and then I'll do a semicolon and I'm just gonna copy the additional commands here. So we can just do everything in kind of one go here and also with the correct order here. All right. I did was I just combined those three commands with a semicolon so this will run and then this will run and then this will run. All right, we can see it reset the template and then it created the NuGet package and then it installed the NuGet package and then we can see it's here. So if I were to do .NET Nu, sci-ed, con, dash-h, I should see those new parameters here. I see I've got author name here. Framework doesn't automatically appear here because it's like there's just one value there I think and I'm really not sure why framework doesn't show up here. I think it probably should, this is probably a bug. I'll kind of talk to the team about that. So now I just reinstalled that template. So I have to close Visual Studio and then reopen it so that way Visual Studio gets kind of the latest definition of that. Let me go into create a new project and if all goes well, we should see in the additional info page those parameters that we have defined here. All right, so now I see, oh yeah, that's right, that's right, that's right. Okay, so yeah, I see the target framework but I don't see my author name parameter. So there's actually an additional step that needs to happen here. Let me open up that solution. So demo.solution. So that's another thing that I forgot to mention here. Framework parameter, that one's kind of special case then if you've got the framework parameter in your template.json, it will just appear in Visual Studio itself. But for the other parameters, in Visual Studio we're not sure what parameters should and shouldn't be shown. So what we do is we have to actually put an indication in the ide.host.json. And let me show you where that is in the samples here. So all you have to do is basically define this simple symbol info property here and that's an array of values here. So let's go ahead and do that. So we'll say symbol, this is not in the, wow, it's not in the intelligence for some reason. Okay, so this is another kind of bug here. Definitely want to get that one fixed. So I'll be bringing that up with the team, of course. All right, so this is just an array of objects here. So we give the id and the id here is author name and they'll check this real quick. So id, author name, and then the name is author name and then is visible to true. So yeah, that looks all good to me. The scheme of declaration looks good too. So I'm not sure what's going on with the intelligence here, but that's okay. Let me go ahead and close out a visual studio and then we'll go ahead and rerun that command to get it reset and then reinstalled. Yep, author name, okay. So we'll say demo author here, okay. And we can see that there's just one choice in the target framework and I don't have the ability to change that. But if you wanted to, you can definitely go through and create a template that supports multiple target frameworks, but it's definitely some additional work for those types of templates. Let's take a look at what we get out of here. Let's go ahead and just run this with control F5. Let me just run it here. All right, so we can see hello from console one created by demo author. It's gonna take a look at this, make sure the replacements happen too. We saw that the namespace and then this string here got replaced with the project name as expected. See demo author has been replaced as expected. And then inside the CS Proj, that string was replaced also with demo author here. So that is really about it for how to create parameters here and then get them to appear, not only in .NET new, but also in Visual Studio. And one thing that I wanna kind of point out that I did point out in the previous video, but let me go ahead and point it out one more time is, let's say if you do wanna support something that's more kind of complex, right? Like if you wanna create a template that supports more than one target framework, or if you wanna create a template that has different types of parameters, right? Like maybe you wanna checkbox instead of a text or stuff like that. The best thing to do is to go take a look at the existing ASP.NET core templates that have been created by Microsoft here. So this is the repository where all our kind of source templates appear. And most of them that you'll probably be interested in are under project templates. So that's definitely a great resource for how to create kind of a more kind of complex template. But yeah, we'll also kind of cover some additional kind of info in the upcoming video as well. That is exciting. And yeah, that's great. Once again, it seems super easy to be able to continue expanding your template like that. Yeah, definitely. Yeah, exactly. So it's kind of a grow up story here. You know, I wanted to make it very easy to kind of get started with a basic template, but then also make it kind of easy to grow that template as well. And that's kind of what we're doing through these series of videos here, yeah. Yeah, so what's next? What we'll be next is, I think what we're gonna cover next is kind of troubleshooting and analyzing templates here. So what are the kind of common pitfalls? And then what can we do to avoid those? And what's available? So I created a .NET Core tool that template authors can use to analyze their templates. So I'll show how to install and use that and then go through just kind of the, some of the gotchas there, yeah. Yeah, that sounds like some good stuff to know because I don't think anyone ever gets it right on the first try, you know? Yeah, definitely, definitely. Sweet. So until then, happy coding. That's it. Thank you.