Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
251 views
in Technique[技术] by (71.8m points)

javascript - 访问把手文件中的数据(Accessing data in handlebars file)

How to access a variable in handlebars file that is declared in a JavaScript file.(如何访问在JavaScript文件中声明的句柄文件中的变量。)

Accessing as below doesn't work.(如下访问不起作用。) In JavaScript file(script.js):(在JavaScript文件(script.js)中:) (function() { var dataForCreate = "Create"; })(); In Handlebar file(sample.hbs):(在把手文件(sample.hbs)中:) <!doctype html> <html> <head> <script src="script.js"></script> </head> <body> <button id="createBtn">{{dataForCreate}}</button> </body> </html>   ask by Hari Prasath translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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为sampleContainerdiv容器,并且正在使用jQuery然后-) $('#sampleContainer').html(htmlTemplate);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...