项目作者: iStuffs

项目描述 :
:wavy_dash: Handelbars external file handler.
高级语言: JavaScript
项目地址: git://github.com/iStuffs/hbs-tiny-loader.git
创建时间: 2017-11-01T22:20:25Z
项目社区:https://github.com/iStuffs/hbs-tiny-loader

开源协议:

下载


.hbs tiny loader

.hbs tiny loader

Here is a little work around to load your handelbars template files (.hbs).

Handelbars is great javascript template engine to work with. But to keep things organized I like to put my templates in separate files. So you need to gather them with a little AJAX call. There is where .hbs tiny loader will make your life easier.

.hbs tiny loader comes in two flavors, asynchronous and synchronous. You can pick one by choosing the right branch of this repo.

Requirements

  • handelbars
  • jQuery
  • .hbs tiny loader

So you need to include those scripts at the end of your HTML body tag.

  1. [...]
  2. <script type="text/javascript" charset="utf-8" src="js/jquery.3.2.1.js"></script>
  3. <script type="text/javascript" charset="utf-8" src="js/handlebars.4.0.11.js"></script>
  4. <script type="text/javascript" charset="utf-8" src="js/jquery.handlebars.hbs-tiny-loader.min.js"></script>
  5. <script type="text/javascript" charset="utf-8" src="js/script.js"></script>
  6. </body>

Exemples

Considering this file structure:

  1. root/
  2. ├── js/
  3. ├── handlebars.4.0.11.js
  4. ├── jquery.3.2.1.js
  5. ├── jquery.handlebars.hbs-tiny-loader.min.js
  6. └── script.js
  7. ├── templates/
  8. └── template.hbs
  9. └── index.html

Asynchronous example

  1. var path = 'templates/template.hbs';
  2. var data = {
  3. message: 'Hello hbs file!'
  4. };
  5. var deferred = $.getHbs(path);
  6. deferred.then(function(template){
  7. var html = template(data);
  8. $('body').prepend(html);
  9. });

Synchronous example

  1. var path = 'templates/template.hbs';
  2. var template = $.getHbs(path);
  3. var data = {
  4. message: 'Hello hbs file!'
  5. };
  6. var html = template(data);
  7. $('body').prepend(html);

Have fun with handlebars and keep things organized. :wink: