Javascript 方形空间样把手模板法

Javascript 方形空间样把手模板法,javascript,templates,ember.js,handlebars.js,Javascript,Templates,Ember.js,Handlebars.js,我已经开始欣赏Handlebar js模板的简单性,但是有一件事我仍然感到有点兴奋,我想知道是否有更简单的解决方案 问题: 设置车把的简单方法 // Extract the text from the template .html() is the jquery helper method for that var raw_template = $('#simple-template').html(); // Compile that into an handlebars templat

我已经开始欣赏Handlebar js模板的简单性,但是有一件事我仍然感到有点兴奋,我想知道是否有更简单的解决方案

问题:

设置车把的简单方法

// Extract the text from the template .html() is the jquery helper method for that
  var raw_template = $('#simple-template').html();
  // Compile that into an handlebars template
  var template = Handlebars.compile(raw_template);
  // Retrieve the placeHolder where the Posts will be displayed 
  var placeHolder = $("#main");
  // Create a context to render the template
  var context = {posts: 
    [
      {title: "First Post", entry: "You can't stop me now!", author: "Sandy Duster"},
      {title: "2nd Post",   entry: "Alice you are going in the wrong way."},
      {title: "3rd Post",   entry: "Mate, Where is my burger?"},
      {title: "4th Post",   entry: "The rainmaker day"},
    ]
  }
  // Generate the HTML for the template
  var html = template(context);
  // Render the posts into the page
  placeHolder.append(html);
以上所有内容目前都是使用一个把手模板将内容添加到一个容器主id中所需的内容

如果我决定使用不同的模板向更多的容器中添加内容,我必须编译它,检索占位符容器,为模板生成html,并将其附加到占位符中。对于每个不同的模板文件,我都必须这样做

我的理想方案是调用上下文或JSON数据一次:

var context = {posts: 
    [
      {title: "First Post", entry: "You can't stop me now!", author: "Sandy Duster"},
      {title: "2nd Post",   entry: "Alice you are going in the wrong way."},
      {title: "3rd Post",   entry: "Mate, Where is my burger?"},
      {title: "4th Post",   entry: "The rainmaker day"},
    ]
  }
然后,只需遍历body中的内容,插入{{page[0].name}、{{imageURL},添加内容块等,而无需单独定义模板。很简单,比如wordpress如何查询文章和显示内容,查询数据库一次,然后在页面中添加查询内容

squarespace开发者平台的工作方式与此类似,您不必插入模板或编译和呈现,只需在{{.section title}中添加html即可

所以我的问题是,有没有一个简单的类似平方空间的方法,我可以采用?或者squarespace只是为了方便起见开发了大量后端代码

抱歉,如果这听起来像一个令人困惑的问题,我只想用最简单的方法:

 SCRIPT TO LOAD JSON CONTENT
<body>
    {INCLUDE HEADER BLOCK HERE}
    <h1>{ADD TITLE HERE}</h1>
    {ADD CONTENT HERE}
    </body>

无需重复编译、定义容器变量、渲染。

您可以将整个代码块放入如下函数中:render'main'、'simple_template',context所以您的建议是,我可以简单地执行render'main'、'simple_template',context;呈现“工作”、“工作模板”上下文;呈现“标题”、“标题模板”上下文;呈现“页脚”、“页脚模板”上下文;如果为了更好地管理模板,我将这些模板文件分开,有没有一种方法可以轻松地包含.handlebar模板?我是否必须依赖AJAX请求才能做到这一点?eg var template=导入模板的函数。把手内容呈现“页脚”、模板、上下文;