handlebars.js - How to use Handlebars.registerPartial using Javascript - Stack Overflow

I have a very basic question. How do you use Handlebars.registerPartial using Javascript only? I have s

I have a very basic question. How do you use Handlebars.registerPartial using Javascript only? I have seen on many sites how to use it with JQuery, but I unfortunately can't use JQuery.

This is example of what I have seen:

Handlebars.registerPartial("person", $("#person-partial").html());

But the $("#person-partial").html() is JQuery

I have a very basic question. How do you use Handlebars.registerPartial using Javascript only? I have seen on many sites how to use it with JQuery, but I unfortunately can't use JQuery.

This is example of what I have seen:

Handlebars.registerPartial("person", $("#person-partial").html());

But the $("#person-partial").html() is JQuery

Share edited Aug 21, 2014 at 16:06 user3499265 asked Aug 21, 2014 at 16:00 user3499265user3499265 972 silver badges10 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 5

Second argument Handlebars.registerPartial() is string containing the template. You dont need jquery for that. I know that you are refering to this tutorial and it uses jquery in its examples.

Using same example code you can do it using document.getElementById and .innerHTML like following

<script id="people-template" type="text/x-handlebars-template">
  {{#each people}}
    {{> person}}
  {{/each}}
</script>

<script id="person-partial" type="text/x-handlebars-template">
  <div class="person">
    <h2>{{first_name}} {{last_name}}</h2>
    <div class="phone">{{phone}}</div>
    <div class="email"><a href="mailto:{{email}}">{{email}}</a></div>
    <div class="since">User since {{member_since}}</div>
  </div>
</script>

<script type="text/javascript">
  $(document).ready(function() {
    var template = Handlebars.pile($("#people-template").html());
    Handlebars.registerPartial("person", document.getElementById("person-partial").innerHTML);

    template(yourData);
  }
</script>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744648944a4585784.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信