2014-02-07 3 views
1

정신 이상 검사는 mainLocation이 "예"를 반환해야 함을 누군가 확인할 수 있습니까? 어떤 이유로 그것은 "아니오"를 반환합니다.Handlebars.js 예상대로 작동하지 않는 경우

여기 내 코드입니다.

<script id="Templates" type="text/x-handlebars-template"> 
<div id="RowExpand{{id}}" class="collapse"> 
    {{#if mainLocation}} 
    yes - Some content here... 
    {{else}} 
    no - Some content here... 
    {{/if}}  
</div> 
</script> 

파이어 버그의 json 데이터입니다.

address  "5862 Vincent Blvd." 
city  "Santa Monica" 
state  "CA" 
zipcode  "90452" 
mainlocation true 

답변

1

와의 섹션을 포장 수,이 일을합니다. 이게 내 문제를 해결 한 방법입니다. 나는 이렇게 사용자 정의 핸들바 도우미를 만들었습니다.

Handlebars.registerHelper("ismainlocation", function(mainlocation) { 
if(mainlocation){ 
    return "Yes"; 
} 
else{ 
    return "No"; 
} 
}); 

그리고 내 스크립트 템플릿을 변경했습니다.

<script id="Templates" type="text/x-handlebars-template"> 
<div id="RowExpand{{id}}" class="collapse"> 
{{ismainlocation mainlocation}}  
</div> 
</script> 
1

이 템플릿의 데이터 컨텍스트는 무엇입니까?

당신이 같은 문제가 발생하는 다른 경우 블록 도우미

In your html file: 
<head> 
    ... 
</head> 
<body> 
    {{> example}} 
<body> 

<template name="example"> 
    {{#with data}} 
    {{#if mainLocation}} 
     html code 
    {{else}} 
     other html code 
    {{/if}} 
    {{/with}} 
</template> 


In your js file: 
Template.example.helpers({ 
    data: function() { 
    return {mainLocation: true}; 
    } 
}); 
+0

감사합니다. 나는 핸들 바가 상당히 새롭고 스크립트를 올바른 위치에 두는 것이 아닙니다. 이것은 내가 시도한 것입니다 : 그리고 작동하지 않았습니다. 내가 뭘 잘못하고 있니? 감사합니다 – user752746

+0

유성 프로젝트로 작업하는 경우. 스크립트 태그는 필요 없습니다. 나는 대답을 수정하려고합니다. – Bozhao

+0

고마워, 유성을 사용하지 않아. – user752746

관련 문제