Coder Social home page Coder Social logo

codehead / jquery-handlebars Goto Github PK

View Code? Open in Web Editor NEW

This project forked from 71104/jquery-handlebars

0.0 2.0 0.0 493 KB

A jQuery plugin to render Handlebars.js templates into elements

Home Page: http://71104.github.io/jquery-handlebars

License: MIT License

JavaScript 100.00%

jquery-handlebars's Introduction

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:

<p>{{ field1 }}, {{ field2 }}</p>
// will fetch <template.handlebars> 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:

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

// now this will fetch <path/to/templates/content.hbs>
$('#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:

{{! I'm using a custom "salute" helper }}
<p>{{salute what }}</p>

{{! I'm using a custom "csv" block helper }}
{{#csv array }}{{ this }}{{/csv}}
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:

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

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

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

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

<li>{{ this }}</li>

you can access it in your templates normally:

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

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

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

License

MIT. Copyright 2013 Alberto La Rocca.

jquery-handlebars's People

Contributors

71104 avatar angelxmoreno avatar zigomir avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.