versx.Handlebars.Net.ViewEngine 1.0.2

dotnet add package versx.Handlebars.Net.ViewEngine --version 1.0.2
                    
NuGet\Install-Package versx.Handlebars.Net.ViewEngine -Version 1.0.2
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="versx.Handlebars.Net.ViewEngine" Version="1.0.2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="versx.Handlebars.Net.ViewEngine" Version="1.0.2" />
                    
Directory.Packages.props
<PackageReference Include="versx.Handlebars.Net.ViewEngine" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add versx.Handlebars.Net.ViewEngine --version 1.0.2
                    
#r "nuget: versx.Handlebars.Net.ViewEngine, 1.0.2"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package versx.Handlebars.Net.ViewEngine@1.0.2
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=versx.Handlebars.Net.ViewEngine&version=1.0.2
                    
Install as a Cake Addin
#tool nuget:?package=versx.Handlebars.Net.ViewEngine&version=1.0.2
                    
Install as a Cake Tool

Handlebars.Net.ViewEngine

Usage

A Nuget package has not been created yet - this is planned

  • Reference the HandlebarsViewEngine library from your ASP.NET core project

  • Add the following using statement to the top of Startup.cs:

using Handlebars.Net.ViewEngine;
  • Modify Startup.cs as follows:
public class Startup
{
    // This method gets called by the runtime. Use this method to add services to the container.
    // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc()
                .AddHandlebars();

        // If you want to override the default layout location
        //services.Configure<HandlebarsViewEngineOptions>(options =>
        //{
        //    options.DefaultLayout = "Views/Shared/layout.hbs";
        //    options.RegisterHelpers = new Dictionary<string, HandlebarsHelper> { }
        //});
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseBrowserLink();
            app.UseDeveloperExceptionPage();
        }

        // To allow access static files, such as .css and .js file
        app.UseStaticFiles();

        app.UseMvc(routes =>
        {
            routes.MapRoute(
                name: "default",
                template: "{controller=Home}/{action=Index}/{id?}");
        });

        app.Run(async (context) =>
        {
            await context.Response.WriteAsync("Could not match route");
        });
    }
}
  • Create the following folder structure in your project (files will be created in later steps, this just shows what the folders will look like when populated with files)
Controllers
-- {controller-name}Controller.cs
Views
-- {controller-name}
---- {action-name}.hbs
---- Partials [optional]
------ {controller-name}-{partial-name}.hbs [optional]
-- Shared
---- layout.hbs
---- Partials
------ {partial-name}.hbs
  • Create a layout template - layout.hbs. For example:
<!DOCTYPE html>
<html>

<head>
    <title>Handlebars Example</title>   
    <link href="./style.css" rel="stylesheet">
</head>

<body class="app">
    <div>
        <div id="headerContent">
            {{> header}} 
        </div>
        <div id="mainContent">
            {{{body}}} 
        </div>
    </div>
</body>

</html>
  • Create controllers as you would in vanilla ASP.NET Core, passing a model to the view if it is required in the template. For example:
public class PeopleController : Controller
{
    public IActionResult Index()
    {
        var people = new List<Person>();

        people.Add(new Person
        {
            Name = "Amit Saluja",
            Position = "Business Analyst",
            Office = "Bangalore"
        });
        people.Add(new Person
        {
            Name = "Sofia Souza",
            Position = "Marketing Manager",
            Office = "São Paulo"
        });

        return View(people);
    }
}
  • Create Views for your actions, for example:
<table cellpadding="10">
    <thead>
        <tr>
            <th>Name</th>
            <th>Position</th>
            <th>Office</th>
        </tr>
    </thead>
    <tbody>
        {{#each this}}
        <tr>
            <td>{{Name}}</td>
            <td>{{Position}}</td>
            <td>{{Office}}</td>
        </tr>
        {{/each}}
    </tbody>
</table>

Limitations

  • All partial views must be uniquely named, regardless of which folder they are in. It is recommended that shared partials are stored in Views/Shared/Partials. More specific partials can be stored in Views/{Controller-Name}/Partials but should be named {controller-name}-{partial-name}.hbs. This is due to the fact that handlebars.net loads all partials globally, keyed by a name. Therefore, the view engine loads all partials at startup and throws an exception if there are any duplicates.

  • Only data passed to a view as a model is currently supported. Therefore, items added to the ViewBag are not accessible.

Product Compatible and additional computed target framework versions.
.NET net5.0 is compatible.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.2 1,325 2/1/2022
1.0.1 1,195 1/24/2022
1.0.0 1,138 1/24/2022