百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
J

jquery-handlebars

> 编程语言
开源

一个 jQuery 插件,用于将 Handlebars.js 模板渲染为元素

100 stars0 点赞2 次浏览
访问官网GitHub

工具介绍

一个 jQuery 插件,用于将 Handlebars.js 模板渲染为元素

jquery-handlebars

A jQuery plugin to render Handlebars.js templates into elements.

Template scripts are retrieved through AJAX, precompiled and cached.

Download

Version 1.1 available.

Getting Started

Each jQuery object has a render method that:

  • retrieves the specified template (either from cache or by fetching through AJAX and compiling with Handlebars),
  • renders it through Handlebars using the specified context object,
  • passes the output string to the jQuery object's html method.

Example:

handlebars

{{ field1 }}, {{ field2 }}
javascript
// will fetch  and render to the DOM element whose id is "content"
$('#content').render('template', {
	field1: 'Hello',
	field2: 'world!'
});

The first argument to the render method is a template name. The plugin builds the template path to fetch from this name by prepending a base path and appending a file name extension. The default base path is the empty string, while the default extension is .handlebars. To configure them differently use the jQuery.handlebars method:

javascript
$.handlebars({
	templatePath: 'path/to/templates',
	templateExtension: 'hbs'
});

// now this will fetch 
$('#some-element').render('content', {
	// ...
});

The second argument to the render method is of course the context to use in Handlebars to render the template.

Helpers and partials

When using this plugin you can use the Handlebars namespace normally if you want; this allows you to register helpers and partials.

For example:

handlebars
{{! I'm using a custom "salute" helper }}

{{salute what }}

{{! I'm using a custom "csv" block helper }}
{{#csv array }}{{ this }}{{/csv}}
javascript
Handlebars.registerHelper('salute', function (what) {
	return 'Hello, ' + what + '!';
});

Handlebars.registerHelper('csv', function (array, options) {
	return array.map(function (element) {
		return options.fn(element);
	}).join(', ');
});

$('#content').render('content', {
	what: 'world',
	array: [1, 2, 3]
});

The plugin also supports fetching and registering partials. You only need to configure the base path and filename extension for the partial files in the jQuery.handlebars method:

javascript
$.handlebars({
	partialPath: 'partials',
	partialExtension: 'partial'
});

Then you can register a partial using the partial action of the jQuery.handlebars method:

javascript
/* based on configured settings, this will fetch the 
	file and register it as a partial */
$.handlebars('partial', 'element');

At this point, assuming you have the following partials/element.partial file:

xml

{{ this }}

you can access it in your templates normally:

handlebars

{{#each array }}
	{{> element }}
{{/each}}
javascript
$('#content').render('template', {
	array: ['first', 'second', 'last']
});

As a shorthand you can register all the partials at initialization time using the partials configuration setting:

javascript
$.handlebars({
	templatePath: 'templates',
	partialPath: 'partials',
	partials: ['header', 'footer', 'user', 'another-partial', 'another-one']
});

License

MIT. Copyright 2013 Alberto La Rocca.

Issues· 4 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

JavaScript

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言