如何将画布插入Handlebars.js模板

时间:2016-02-04 03:14:05

标签: javascript html5 canvas handlebars.js processing.js

很简单:

<script id="write-tpl" type="text/x-handlebars-template">
    <canvas data-processing-sources="processing.pde"></canvas>
</script>

画布标签是否未正确转义或是什么?我也试过以下内容,但无济于事:

<canvas data-processing-sources="processing.pde"><{{!}}/canvas>

如何在我的handlebars.js模板中包含带有源的画布并将其渲染?

更新

标题中的所有脚本:

 <!-- Materialize Icons  -->
  <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">

  <!-- Typicons -->
  <link rel='stylesheet' href='typicons.font-master/src/font/typicons.min.css' />

  <!-- JQuery -->

  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
  <script type="text/javascript" src="http://builds.handlebarsjs.com.s3.amazonaws.com/handlebars.min-latest.js"></script>

  <!-- Materialize JS -->
  <script src="js/materialize.min.js"></script>

  <!-- Drop Down -->
  <script>
    $( document ).ready(function() {
      $('.dropdown-button').dropdown({
        inDuration: 300,
        outDuration: 225,
              constrain_width: false, // Does not change width of dropdown to that of the activator
              hover: false, // Activate on click
              alignment: 'left', // Aligns dropdown to left or right edge (works with constrain_width)
              gutter: 0, // Spacing from edge
              belowOrigin: false // Displays dropdown below the button
            });
    });
  </script>

  <script>
    var data = {
      width: 200,
      height: 100,
      background: "#000"
    };

    var template = Handlebars.compile($("#write-tpl").html());

    document.body.innerHTML = template(data);
  </script>

  <!-- Style -->
  <link href="css/materialize.css" rel="stylesheet"/>
  <link href="css/style.css" type="text/css" rel="stylesheet" media="screen,projection"/>

我的模板:

<script id="write-tpl" type="text/x-handlebars-template">

      <script id="write-tpl" type="text/x-handlebars-template">
        <canvas data-processing-sources="js/processing.pde" width="{{width}}" height="{{height}}" style="background:{{background}};"></canvas>
        <{{!}}/script>

    </script>

以下页面底部的脚本:

<!-- Javascript -->
    <script src="https://www.parsecdn.com/js/parse-1.2.19.min.js"></script>
    <script src="js/blog.js"></script>

    <!-- Popup -->
    <script src="view.min.js?auto"></script> 

    <!-- Processing -->
    <script src="js/processing.js"></script>

1 个答案:

答案 0 :(得分:1)

删除模板中的内部脚本:

<script id="write-tpl" type="text/x-handlebars-template">
      <script id="write-tpl" type="text/x-handlebars-template">
         <canvas data-processing-sources="processing.pde" width="{{width}}" height="{{height}}" style="background:{{background}};"></canvas>
      <{{!}}/script>
</script>

像这样:

<script id="write-tpl" type="text/x-handlebars-template">
    <canvas data-processing-sources="processing.pde" width="{{width}}" height="{{height}}" style="background:{{background}};"></canvas>
</script>

这是一个有效的例子:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>untitled</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <script type="text/javascript" src="http://builds.handlebarsjs.com.s3.amazonaws.com/handlebars.min-latest.js"></script>
    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/processing.js/1.4.16/processing.min.js"></script>
</head>
<body>

    <script id="write-tpl" type="text/x-handlebars-template">
        <canvas data-processing-sources="processing.pde" width="{{width}}" height="{{height}}" style="background:{{background}};"></canvas>
    </script>


    <div id="content"></div>

    <script>
        $(document).ready(function(){
            var data = {
              width: 200,
              height: 100,
              background: "#000"
            };

            var template = Handlebars.compile($("#write-tpl").html());

            $("#content").html(template(data));

        });
    </script>


</body>

输出:

http://i.imgur.com/D6JyKjr.png