![]() | Welcome! |
This is an ASP.NET MVC ViewEngine that uses the Handlebars syntax for its view files.
For reference:
https://github.com/rexm/Handlebars.Net
- Blistering-fast Handlebars.js templates in your .NET application.
http://handlebarsjs.com/
- The syntax is described here (but note that is the client-side Javascript compiler/parser)
How to add the view engine (assuming the source file has "using HandlebarsDotNet.Mvc;" at the top):
protected void Application_Start() { // In case you want to remove the other view engines, uncomment this: //ViewEngines.Engines.Clear(); // Create the view engine. var hbsve = new HandlebarsViewEngine(); // The builtin helpers aren't added by default - you need to opt-in to have them available. hbsve.RegisterMvcHelpers(); hbsve.RegisterSectionsHelpers(); // Add the Handlebars view engine ViewEngines.Engines.Add(hbsve); // The rest of the method continues here. Here is what ASP.NET MVC has by default. AreaRegistration.RegisterAllAreas(); RouteConfig.RegisterRoutes(RouteTable.Routes); }
By default view (and layout) files have an extension of ".hbs". You can change that by setting the ViewExtension property.
The files used for rendering are found in the paths returned from an IPathsProvider implementation.
If you don't specify one in the constructor for HandlebarsViewEngine then DefaultPathsProvider is used.
In order to find partials the VirtualPathProvider must support GetDirectory. Also, if it supports GetCacheDependency that will be beneficial. If it doesn't then updates to the view files are not instantaneuos - updates to the files are checked every 5 seconds. If it does support GetCacheDependency there can still be some delay depending on the implementation.
How to send a model to the view file:
public class HomeController : Controller { public ActionResult Index() { var model = new { first = "John", last = "Doe" } return View(model); } }
Hello, {{first}} {{last}}!
Hello, John Doe!
The example above didn't use a layout file. This is how to do that:
{{!< default}}
Hello, {{first}} {{last}}!
<html> <body> {{{body}}} </body> </html>
<html> <body> Hello, John Doe! </body> </html>
The latest version of this help is located on the web at https://jiellse.github.io/Handlebars.Net.Mvc/
Sometimes it is useful to have documentation locally on your own computer. Maybe you are at a place with no internet connectivity, or flaky connection?
A downloadable version of this can be found at https://jiellse.github.io/Handlebars.Net.Mvc/HandlebarsDotNet.Mvc.chm. If you do download it, you may have to bring up the Properties for the file and press the Unlock button in order for it to show any content. (If there is no Unlock button this step has already been done or wasn't necessary, depending on the browser used when downloading.)
If you are currently using the help file and wish to go to the website instead, use the link above.