如何在使用RequireJS的Handlebars中使用预编译模板? [英] How to use precompiled templates in Handlebars with RequireJS?

查看:229
本文介绍了如何在使用RequireJS的Handlebars中使用预编译模板?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想预编译我的Handlebars模板,但我不确定这是如何在开发模式下工作的。

Guard 将持续监控对Handlebars模板文件的更改吗?



我使用RequireJS来拉入模板;例如:

  define(['jquery','handlebars','text!templates / my_template'],function($, Handlebars,myTemplate){

// ...

var data = {some:data,some_more:data};
var templateFn = Handlebars.compile(myTemplate);
$('#target')。append(templateFn(data));

// ...

});

所以我理解一旦模板被预编译,就可以这样做:

  define(['jquery','handlebars'],function($,Handlebars){

// ...

var data = {some:data,some_more:data};
var template = Handlebars.templates ['my_template'];
$(' #target')。append(template(data));

// ...

});

请注意以下有关第二个代码段的内容:


  1. RequireJS模块不再提取模板。
  2. 不再使用Handlebars.compile()。

因此,通常我会让Guard运行,以便在模板文件发生文件系统级修改时对模板进行编译?



基本上我的问题是,开发者是否打算这样做?

  if (开发){
编译模板
}
else {
使用预编译模板
}

我也使用Rails,所以也许有一些像sass-rails这样的黑魔法。

/ div>

你看看Require.js Handlebars Plugin( https:// github .com / SlexAxton / require-handlebars-plugin )或epeli's requirejs-hbs( https://github.com/epeli/requirejs-hbs )?


I'd like to precompile my Handlebars templates, but I'm not sure how this works in development mode.

Is it common practice have some background process like Guard running to constantly monitor changes to Handlebars template files?

I'm using RequireJS to pull in templates; e.g.:

define(['jquery', 'handlebars', 'text!templates/my_template'], function($, Handlebars, myTemplate) {

  // ...

  var data = {"some": "data", "some_more": "data"};
  var templateFn = Handlebars.compile(myTemplate);
  $('#target').append(templateFn(data));

  // ...

});

So I understand once templates are precompiled, one would do this:

define(['jquery', 'handlebars'], function($, Handlebars) {

  // ...

  var data = {"some": "data", "some_more": "data"};
  var template = Handlebars.templates['my_template'];
  $('#target').append(template(data));

  // ...

});

Note the following about the in the second code snippet:

  1. The RequireJS module no longer pulls in the template.
  2. Handlebars.compile() is no longer used.

So typically would I have Guard running to keep my templates compiled whenever file system-level modifications happen to template files?

Basically my question is, is the intention for developers to do this?

if (development) {
  compile templates
}
else {
  use precompiled templates
}

I'm also using Rails, so maybe there is some black magic like sass-rails.

解决方案

Have you take a look at Require.js Handlebars Plugin (https://github.com/SlexAxton/require-handlebars-plugin) or epeli's requirejs-hbs (https://github.com/epeli/requirejs-hbs) ?

这篇关于如何在使用RequireJS的Handlebars中使用预编译模板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆