Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/462.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在javascript文件中编写把手模板_Javascript_Coding Style_Pug_Handlebars.js - Fatal编程技术网

如何在javascript文件中编写把手模板

如何在javascript文件中编写把手模板,javascript,coding-style,pug,handlebars.js,Javascript,Coding Style,Pug,Handlebars.js,javascript手柄模板通常会添加到HTML文件中,如果我们可以将所有模板都保存在javascript文件中,会更容易,有没有这样做的最佳实践 <script id="entry-template" type="text/x-handlebars-template"> <h1>{{title}}</h1> </script> {{title}} 编辑:我也可以使用纯Javascript字符串编写此模板,其他选项是什么 var temp

javascript手柄模板通常会添加到HTML文件中,如果我们可以将所有模板都保存在javascript文件中,会更容易,有没有这样做的最佳实践

<script id="entry-template" type="text/x-handlebars-template">
  <h1>{{title}}</h1>
</script>

{{title}}
编辑:我也可以使用纯Javascript字符串编写此模板,其他选项是什么

var template = '<h1>{{title}}</h1>';
var模板=“{{title}}”;

我认为你做不到

导致浏览器执行的javascript为
type=“text/javascript”
,且手柄模板不应存在

=======编辑=======

为了使代码更清晰,可以将Handlebar模板放在另一个文件中

以下是一个例子:

test.php

<?php

?>

<!DOCTYPE html>
<html>
<head>
    <title>test</title>
</head>
<body>

<script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/handlebars.js/2.0.0/handlebars.min.js" ></script>

<script>
    (function(){
        $('body').load('assets/template.js', function(){
            var template = Handlebars.compile( $('#template').html() );
            var data = {
                'name': 'jone doe',
                'email': 'xx@example.com'
            };

            $('body').append( template(data) ); 
        });


    })()
</script>
</body>
</html>

我认为你应该使用车把的预编译器。请看看这个。
<script type="text/x-handlebars-template" id="template">
    {{name}}
    {{email}}
</script>