简体   繁体   English

从模板中的脚本获取价值-Handlebars.js

[英]Getting value from script in template - Handlebars.js

I am writing a {{#if}} block in Handlebars template, but I want this if to work on the flag value which I am getting from script switch statement. 我正在Handlebars模板中编写一个{{#if}}块,但是如果要处理从脚本切换语句获取的标志值,我希望这样做。 How can be this achieved in Handlebars.js 如何在Handlebars.js中实现

I want to use the flag value in the if block here, how can i do that. 我想在这里的if块中使用标志值,我该怎么做。

Script.js Script.js

$(document).ready(function(){

var source   = $("#forgotPasswordPage-template").html();
var template = Handlebars.compile(source);
var dataForgotPasswordFormElements = {forgotPasswordFormElement:[
  {formElement: "emailId" , dataType:"text" , label :"Email Id", errorBlank:"Please enter Email Id"},
  {formElement: "oldPassword" , dataType:"password" , label:"Old Password" , errorBlank:"Please enter old password"},
  {formElement: "newPassword" , dataType:"password" , label:"New Password", errorBlank:"Please enter new password"},
  {formElement: "confirmPassword" , dataType:"password" , label:"Confirm Password", errorBlank:"Please confirm new password"},
  {formElement: "hintQuestion" , dataType:"text" , label:"Hint Question", errorBlank:"Please enter hint question"},
  {formElement: "hintAnswer" , dataType:"text" , label:"Hint Answer",errorBlank:"Please enter hint answer"},
]};
 $("#forgotPasswordPage-placeholder").html(template(dataForgotPasswordFormElements));
 $( "#forgotPassword .newField input " ).each(function( index ) {
  var newPasswordValue="";
  $(this).focusout(function(){
        var flag=0;
        var value = $(this).val();
        var emailPattern = /^\b[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b$/i ;
        if(value!=""){
                        if (value.match(emailPattern)) 
                        {console.log('correct');
                         flag=1;}
                        else
                        {console.log('incorrect');
                         flag=2;}
                     }

Template.html Template.html

<script id="forgotPasswordPage-template" type="text/x-handlebars-template">
                    {{#forgotPasswordFormElement}}
                    <div class="newField">
                        <label for="{{formElement}}">{{label}}</label>
                         <br/>
                        <input type="{{dataType}}" data-role="none" id="{{formElement}}"></input>
                       {{#if}}
                        <div class="hideError">{{errorBlank}}</div>
                       {{/if}}
                    </div>
                    {{/forgotPasswordFormElement}}
            </script>

Please share some example, would be very helpful. 请分享一些例子,将非常有帮助。

This is the way Handlebars works: 这是车把的工作方式:

template 模板

<a href="{{url}}">
    {{~#if test}}
      {{~title}}
    {{~^~}}
      Empty
    {{~/if~}}
</a>

yourScript yourScript

var template = Handlebars.compile(source, { test: true });

for this example I need to pass the test value before to compile the template to get the correct template. 对于此示例,在编译模板以获取正确的模板之前,我需要通过测试值。 In your code, you should call the compile function at the end when you have set the flag variable. 在代码中,设置了flag变量后,应在最后调用compile函数。 Something like this: 像这样:

if (value.match(emailPattern)) {
   console.log('correct');
   flag=1;
} else {
   console.log('incorrect');
   flag=2;
}
var template = Handlebars.compile(source, { error: flag });

in your template: 在您的模板中:

{{~#if error}}
 <div class="hideError">{{~errorBlank}}</div>
{{~/if~}}

You could see more examples in the documentation: 您可以在文档中看到更多示例:

http://handlebarsjs.com/expressions.html http://handlebarsjs.com/expressions.html

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

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