You must be having an index.html or some main html where you want to render/attach this template.(您必须在要呈现/附加此模板的位置具有index.html或一些主要html。)
Sample.hbs
is just an template file, you first need to compile the hbs template to pass data to it.(Sample.hbs
只是一个模板文件,您首先需要编译hbs模板以将数据传递给它。)
Step 1:(步骤1:)
In your script file access the template file say sampleTemplate
and compile it using Handlebars helper.(在脚本文件中,访问模板文件,说出sampleTemplate
并使用Handlebars helper对其进行编译。)
var compiledTemplate = Handlebars.compile( sampleTemplate );
Step 2:(第2步:)
Now you can pass the data to this compiled template as below -(现在,您可以按以下方式将数据传递到此编译模板中-)
var htmlTemplate = compiledTemplate({dataForCreate : "Create"});
Step 3:(第三步:)
Now you have the raw html partial/template ready to attach it to main html.(现在,您已经准备好将原始html局部/模板附加到主html。) Say you have a div
container with id sampleContainer
and you are using jQuery
then -(假设您有一个ID为sampleContainer
的div
容器,并且正在使用jQuery
然后-)
$('#sampleContainer').html(htmlTemplate);
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…