Handlebars
Knapsack for Handlebars works by scanning your specified Handlebars code locations and automatically discovering reusable components, making them instantly available in your design system.
Why This Approach:
- ⚡ Zero Manual Work - Knapsack finds and configures your components automatically
- 🔄 Always Current - Your documentation stays in sync with code changes
- 🎯 Intelligent - Understands different export patterns and component structures
- 🚀 Scale Effortlessly - Handle hundreds of components without individual setup
Requirements
- Handlebars templates - Templates must be in
.hbsor.handlebarsformat - JavaScript/TypeScript exports - For template data and helpers
Install
Configure
Minimal Setup The simplest configuration requires just specifying where your Handlebars components are located:
Full Configuration Example Here’s a comprehensive example showing all available options:
Code Sources
The codeSrcs array tells Knapsack where to find your Handlebars components and how to process them. The renderer will scan these paths, find Handlebars component exports, and make them available in the Knapsack UI for documentation and demos.
Local Components
Paths are relative to knapsack.config.js
External Libraries
Ensure that any external library is included in your package dependencies
Smart Filtering
Use filters to control exactly which components are discovered:
Alter Handlebars
This enables you to register custom partials, helpers, and other modifications to the Handlebars environment, enhancing the flexibility and power of your templates. In addition to registering partials, you can use the alterHandleBars function to register helpers, set custom delimiters, or perform other runtime customizations. Refer to the Handlebars Runtime API documentation for more details on the available methods and their usage.
By leveraging these customization capabilities, you can tailor the Handlebars runtime to meet the specific needs of your project, ensuring a powerful and flexible templating environment within Knapsack.
Example Configuration
The following example demonstrates how to configure Knapsack with a custom Handlebars renderer and register a partial named foo. In this example, the alterHandleBars function registers a partial named badge with the template <span class="badge">{{text}}</span>. This partial can then be used in other Handlebars templates within your Knapsack project.
Using Registered Partials
Once a partial is registered, you can use it in your templates by referencing its name with the partial syntax {{> partialName}}. For example, to use the badge partial: