简体   繁体   English

把手模板未渲染-可以在Firefox Inspector中看到HTML

[英]Handlebars Template Not Rendering - Can See HTML In Firefox Inspector

I have a handlebars template in an external file (user-listing-template.html) which I am dynamically loading via a JQuery Ajax call to retrieve the file, compiling via Handlebars.compile and then displaying in a div. 我在外部文件(user-listing-template.html)中有一个车把模板,该文件是通过JQuery Ajax调用动态加载以检索文件,通过Handlebars.compile进行编译然后显示在div中的。 See example code below: 请参见下面的示例代码:

Template file: 模板文件:

<script id="user-listing-template" type="text/x-handlebars-template">
  <h2 class="ui header">User Maintenance</h2>
  <hr>
  <h4 class="text-align-left">Total Users: </h4>
  <br><br>
</script>

JS function to load Template: JS函数加载模板:

function userListroute() {
var source;
    var template;

    $.ajax({
        url: "/templates/user-listing-template.html",
        cache: true,
        success: function(data) {
            console.log('Template Data From File: ' + data);
            source = data;
            console.log('Template HTML ' + source);
            template = Handlebars.compile(source);
            console.log('Compiled Template: ' + template);

            $('#app').html(template);

        }               
    });  
}

In index.html, I have a div where I would hope to see the template: 在index.html中,我有一个div,希望能看到模板:

<div id="app"></div>

However, when I run this application the template is not rendered. 但是,当我运行此应用程序时,不会呈现模板。 I can see the template's HTML code in the Inspector in Firefox dev tools: 我可以在Firefox开发工具的“检查器”中看到模板的HTML代码:

Firefox Inspector Output Firefox检查器输出

All of the console.log() statements show the valid HTML code is there, however it will not display, I only see the blank page. 所有console.log()语句都显示有效的HTML代码,但是不会显示,我只看到空白页。 The console output is shown here: 控制台输出如下所示:

Firefox Console Output Firefox控制台输出

I am clearly doing something wrong, just not sure what - it all appears to be working except for the rendering bit. 我显然做错了什么,只是不确定是什么-除了渲染位,其他所有东西似乎都在工作。

Any assistance greatly appreciated. 任何帮助,不胜感激。 I'm fairly new to Handlebars, and am just trying a proof-of-concept application. 我对Handlebars相当陌生,并且正在尝试概念验证应用程序。

According to official site http://handlebarsjs.com/ . 根据官方网站http://handlebarsjs.com/的说法。 You just compile your template without generating html. 您只编译模板而不生成html。

template = Handlebars.compile(source);
var html = template(yourdata);     // yourdata is the object rendered to your template
$('#app').html(html);   // can not be template variable, variable html is final html content

When you retrieve your template file you are leaving it encapsulated with <script> tags. 检索模板文件时,将其保留为<script>标记封装。 Handlebars needs the content only. 把手仅需要内容。 Thus (as you can see in Firefox inspector) the compiled output is not displayable HTML as it is still within the original script tags. 因此(如您在Firefox检查器中所看到的),编译后的输出仍不可显示,因为它仍在原始script标签中。

You need to extract the HTML from your data when you assign it to the source variable. 将数据分配给source变量时,需要从数据中提取HTML。

success: function( data ) {
    source = $( data ).html();
    ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM