본문 바로가기

기타

[Handlebar]Block 사용하기(조건에 따른 구문)

Ajax와 Handlebars를 이용했을 때, 조건에 따라서 만들어진 문장을 구별할 때 사용할 수 있는 Block

 

Basic Blocks

For demonstration purposes, let's define a block helper that invokes the block as though no helper existed.
<div class="entry">
  <h1>{{title}}</h1>
  <div class="body">
    {{#noop}}{{body}}{{/noop}}
  </div>
</div>

 

The noop helper (short for "no operation") will receive an options hash. This options hash contains a function (options.fn) that behaves like a normal compiled Handlebars template. Specifically, the function will take a context and return a String.
Handlebars.registerHelper('noop', function(options) {
  return options.fn(this);
});

 

 

Handlebars always invokes helpers with the current context as this, so you can invoke the block with this to evaluate the block in the current context.

 

 

출처: http://handlebarsjs.com/block_helpers.html 

{{#eqReplyer replyer}}
<div class="timeline-footer">
<a class="btn btn-primary btn-xs"
data-toggle="modal" data-target="#modifyModal">Modify</a>
</div>
{{/eqReplyer}}

<script>
Handlebars.registerHelper("epReplyer", function(replyer, block){
var accum = "";
if(replyer == "${login.uid}"){
accum += block.fn();
}

return accum;
});    
</script>

실수: 위에 보면 등록한 헬퍼의 명칭과 사용하려는 헬퍼의 이름이 일치하지 않음(eqReplyer, ep~~)

발생 오류:

 

handlebars.js:254

Uncaught Exception {description: undefined, fileName: undefined, lineNumber: undefined, message: "Missing helper: 'eqReplyer'", name: "Error", …}

 

코드로 배우는 웹프로젝트 내용