English 中文(简体)
Node.js with Handlebars.js on服务器和客户
原标题:Node.js with Handlebars.js on server and client

我在Node.js用快车和手提车作为模板发动机。

Expressjs uses layouts and then renders views. The layout (layout.hbs) looks like this:

<!doctype html>
<html lang="en">
    <head>
    </head>
  <body>
    {{{body}}}
  </body>
</html>

<代码>{{{>>>>>>在节日内替换服务器边。 j 当你走一条路时。 例如:

app.get( / , function(req, res){
   res.render( index })
})

将以索引内容取代<代码>{{{{>}>。

现在,在客户一米处,使用背风.,希望使用手bar,供通过后台控制的观点使用。 问题在于,由于这些网页已经通过Handlebars提供,当我试图在其内部使用废物(或在废物内使用废物)时,它就没有工作。 没有任何错误,只是用数据取代标签。

是否有任何人在此之前遇到过这种情况,或有什么想法围绕工作?

Thank you!

最佳回答

Yup, 它是一个 stick问题——比如,在身为老鼠的手稿中引出的问题。

My solution is to use jade (a la haml) in expressjs (server-side) to output handlebars based templates for the client. This way, the server uses one syntax (jade), and the client uses another (handlebars). I am at the same crossroads as you, so I have the same challenge.

Of course, jade is not essential (though it s ready-made for expressjs). You could choose any (non-handlebars) template engine for the server, and/or you could use handlebars on the server with your non-handlebars templating on the client --- as long as the two syntaxes of your chosen templating engines do not collide. Since I m using emberjs on the client and it uses handlebars syntax (by default), I prefer using emberjs + handlebars syntax on the client. So expressjs + jade became a natural fit for the server.

问题回答

你们应使用事先编造的客户模板。 它们的执行速度加快,使你能够在服务器和客户中使用同样的模板语言。

  1. Install handlebars globally npm install handlebars -g
  2. Precompile your templates handlebars client-template1.handlebars -f templates.js
  3. Include templates.js <script src="templates.js"></script>
  4. Execute the template var html = Handlebars.templates["client-template1"](context);

https://stackoverflow.com/a/13884587/8360

这样做的简单办法是在“手法”档案中在<代码>{<<>>>/代码>之前附上<>代码>。 例如:

<script type="text/x-template" id="todo-item-template">
<div class="todo-view">
    <input type="checkbox" class="todo-checkbox" {{checked}}>
    <span class="todo-content" tabindex="0">{{text}}</span>
</div>

<div class="todo-edit">
    <input type="text" class="todo-input" value="{{text}}">
</div>

<a href="#" class="todo-remove" title="Remove this task">
    <span class="todo-remove-icon"></span>
</a>

上述法典将提供给客户,并保存在{>>> 标签上。

www.un.org/spanish/ecosoc 不能自食其力!

我也想做同样的客户/服务商,因此我写了几套单,帮助:

<>node-handlebars-precompiler

我在几个小时的时间里,根据在编织物的指挥线编篡人操作路障。 它不是世界上最伟大的法典,而是很好地为我做了工作。

www.un.org/spanish/ecosoc EDIT: 我不再坚持这一一揽子计划。 如果你要接下去,请通过Github与我联系。 我现在主要使用Jade模板,因此我继续担任维护者是毫无意义的。

围绕这一点,我通过服务器-侧模板通过用户方模板进行了工作。

因此,在服务器边,把所有用户方模板改为一个阵列,将其传送到服务器边的服务器上。

在您的路上,手工艺者做的是:

readTemplates(function(err, clientTemplates) {
  res.render("page", {
    clientTemplates: clientTemplates;   
  });
});

And then in layout.hbs:

{{#each clientTemplates}}
<script type="text/handlebars id="{{this.filename}}" >
{{{this.template}}}
</script>
{{/each}}

在此,我用没有延期的档案名称作为模板,以便从后台观点中参考。 Oh,并记住采用生产方式。

Yeah, this sucks.

我认为,我们应为此撰写一份手脚/奖/助手。

I didn t like the precompilation solution (because I want to define templates in the same file where I will use them) nor the naive {{ escape solution (because it needs the full Handlebars compiler and more javascript code) so I came up with an hybrid solution that uses Handlebars helpers:

(1) 在服务器组合中登记一个称为“模版”的新助手

var hbs = require( hbs );
hbs.registerHelper("template", function(key, options){
    var source = options.fn().replace("\{{", "{{");
    var ret =
     <script>
  + 
        key +   = function(opt){
  +
             return Handlebars.template(  + hbs.handlebars.precompile(source) +  )(opt);
  +
         }
  + 
     </script> ;
    return ret;
});


2) Use it anywhere in your client-side webpage (with {{ escape for client-side parameters)

{{#template "myTemplate"}}
    <div>
        <p>Hello {{this.name}}!</p>
    </div>
{{/template}}

(the server will precompile it in something like this)

<script>
    myTemplate = function(opt){
        return Handlebars.template(/* HBS PRECOMPILATED FUNCTION */)(opt);
    }
</script>


3) Simply call the function where you need it in client-side javascript

var generatedHtml = myTemplate("world");   // = <div><p>Hello world!</p></div>
$("#myDiv").html(generatedHtml);           // or whatever

You have 2 options. The second is THE best way to go:

1) Escape the mustaches

<script type="text/x-handlebars" data-hbs="example">
  <p>{{name}}</p>
</script>

2) Precompile

This will compile the template on the server before it goes to the client. This will make the template ready to use and reduces the burden on the browser.





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签