Brand New Default Blazor Templates


 I have made the decision to switch to using TailwindCss for all my new projects because I really like TailwindCss. It became a pain to have to clean out the Bootstrap and add the styling for the default pages each time I started a new project. My solution, create a new default Blazor default template that uses TailwindCss.

Ways to create templates


After some investigation, I discovered there are now two ways to create project templates for .Net. The original one where you use Visual Studio to token your project and then export it out. The second method is to use the template engine.

Using Visual Studio is error-prone. Once you tokenize your project, it will not build anymore. You will have to keep two versions of the project and update each one moving forward. It does generate a zip file which makes it easier to share the new template.

The new and better way, in my opinion, is to use the new template engine. Channel 9 has a good video series on how it works. You use the dotnet commands to create your template once you define it in a JSON file.

The steps to follow to create your project template are:
    1. Create a folder to work in
        a. File structure
                i. Src => Content => Project solution folders
                ii. Under the project solution folder add a new folder called .template.config. This folder will hold the template definition files
        b. .template.config files
                i. Place your image files for the icon to use for your new template. This is the image you will see in Visual Studio when creating your new project
                ii. Template.json file: this is a JSON file that contains your definitions
                    1. What values go into the file and what values are required are discussed in the Channel9  series.
                    2. There is also a tool to analyze your template.json file to ensure it is valid.
                iii. Ide.host.json: This is an additional file that provides additional information to Visual Studio. This is the information you see when creating a new project in Visual Studio.
    2. Create the template
        a. From PowerShell or the Command line run:

                        dotnet new –install <path to the solution>

        b. You can run dotnet new –list and see the list of install templates and your new template should be in the list.
    3. Create a project with the new  template
        a. Run dotnet new <name of your new template> -o <name of project to create>
    4. Run your new project
        a. Go to the folder you created your new project in
        b. Run dotnet new run
        c. If all works, your application should be built and executed.
        d. If your project is a web application, you will need to see what port it is running on. You can define this in your template file as well.

That is, you now have a new template you can use in VS code, through dotnet new and Visual Studio, through the new project wizard. Overall, it seems quick and easy to generate a new template.

Share your new template

Now if you want to share your new project template, you have a couple of ways.

    1. Create a NuGet package
    2. Create a Visual Studio extension

For me, I am only interested in using NuGet. A NuGet package can be used with Visual Studio or Visual Code. Visual Studio users can only use the Visual Studio extension option.

To generate the NuGet package you need to define a NuGet spec file. This is where you defined the properties of the NuGet package you are building. There is a sample in the git repository listed below.

Once you define your package file you must run:

    Nuget.exe pack <path to spec file> -OutputDirectory <path to writing the package out to>

For my template, I created a new git hub repro just for my project templates, and I plan on making many more. I do not think I am ready to publish to NuGet.org yet.


Create a new Project from the NuGet Package Template

To be able to create new projects from the NuGet package template, you must install the template into your system.

To do this run:

    dotnet new --install <path to NuGet package>

This will install the template. You can now use dotnet new or go to Visual Studio and create your new project.

run:

    dotnet –install -list to validate that the template is installed.

 

Summary

This post is not meant to be a step-by-step tutorial on how to create project templates.  The Channel 9 video series does a much better job of that. It is meant to provide you with resources and insight into what it takes to create a new project template. It also explains how I created my new Blazor Tailwindcss default project templates. Creating this new template was one of the coolest things I have done in a while.

I will be using this template moving forward and I plan to create more, such as SQLite, Auth, the basic application that was logged in, 404 pages, logging, etc. I am extremely excited about what this will allow me to create.

Comments

Popular posts from this blog

Yes, Blazor Server can scale!

Blazor new and improved Search Box

Blazor Wizard Step Component