简体   繁体   English

如何使用车把渲染HTML

[英]how to render html using handlebars

I downloaded handlebars from GitHub 我从GitHub下载了车把

and it contained handlebars.js and plugin.js in the source folder.As for told in the documentation the way to use the render function to fetch template from a file is as follow: 并在源文件夹中包含handlebars.js和plugin.js。如文档中所述,使用render函数从文件中获取模板的方法如下:

$.handlebars({
templatePath: 'path/to/templates',
templateExtension: 'hbs'
});

// now this will fetch <path/to/templates/content.hbs>
$('#some-element').render('content', {
// ...
});

and what i did did to use it is: 我使用它所做的是:

<script type="text/javascript" src="js/jquery.js"></script>
<script src="js/handlebars.js" type="text/javascript"></script>
<script src="js/plugin.js" type="text/javascript"></script>
$.handlebars({
    templatePath: 'lib/assets',
    templateExtension: 'html'
});
var data = {"username":uname};
$('.container').render('lockscreen', data);

lib/assets/lockscreen.html contains code like this: lib / assets / lockscreen.html包含如下代码:

<div class="center">
                <div class="headline text-center" id="time"></div>
                <div class="lockscreen-name" style="font-weight:600;font-size:16px;"><b>{{username}}</b></div>
                <div class="lockscreen-item">
                    <div class="lockscreen-image"><img src="images/avatar5.png" alt="user image"/></div>
                    <div class="lockscreen-credentials">
                        <div class="input-group">
                            <input type="password" class="form-control" placeholder="password" id="pa_asd"/>
                            <div class="input-group-btn">
                                <button class="btn btn-flat"><i class="fa fa-arrow-right text-muted"></i></button>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="lockscreen-link"><a href="login.html">Or sign in as a different user</a></div>
            </div>

And I still can't get it done.I tried it using otherway...using a static function created by koorchik 我仍然无法完成它。我尝试了另一种方式...使用了由koorchik创建的静态函数

function render(tmpl_name, tmpl_data) {
    if ( !render.tmpl_cache ) { 
        render.tmpl_cache = {};
    }

    if ( ! render.tmpl_cache[tmpl_name] ) {
        var tmpl_dir = '/lib/assets';
        var tmpl_url = tmpl_dir + '/' + tmpl_name + '.html';

        var tmpl_string;
        $.ajax({
            url: tmpl_url,
            method: 'GET',
            async: false,
            dataType: 'html',
            success: function(data) {
                tmpl_string = data;
            }
        });

        render.tmpl_cache[tmpl_name] = _.template(tmpl_string);
    }

    return render.tmpl_cache[tmpl_name](tmpl_data);
}
var rendered_html = render('lockscreen', data);

but got some sort of error like 但是出现了类似的错误

_ is undefined _未定义

Can anybody help me???? 有谁能够帮助我????

That's because your code uses underscore.js , which is an external utility library, and _.template() is actually a function from that library. 这是因为您的代码使用underscore.js (这是一个外部实用程序库),而_.template()实际上是该库中的函数。 Your code doesn't find it because you haven't included the underscore.js script. 您的代码找不到,因为您尚未包含underscore.js脚本。

Here is a link to the latest minified version of underscore.js : http://underscorejs.org/underscore-min.js 这是underscore.js的最新最小版本的链接: http : //underscorejs.org/underscore-min.js

You should include it before loading your resources, like this: 您应该在加载资源之前包括它,如下所示:

<script src="http://underscorejs.org/underscore-min.js"></script>
<script type="text/javascript" src="js/jquery.js"></script>
<script src="js/handlebars.js" type="text/javascript"></script>
<script src="js/plugin.js" type="text/javascript"></script>

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

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