2016-07-26 4 views
0

맞춤 블록 도우미에서 params를 전달하고 컴파일하고 싶습니다. params가 hash-object 안에 있다는 것을 알았지 만 어떻게 부분으로 컴파일 할 수 있습니까?핸들 바를 통한 변수 전달 blockhelper

flyoutClass 매개 변수를 부분적으로 컴파일해야합니다. ...

모든 것이 잘 작동하지만 내 PARAM의 출력이 있어야 할 곳은 빈칸으로 ...

핸들은 이미 몇 가지를 시도

module.exports.register = function (Handlebars, context) { 
    Handlebars.registerHelper('injectHtml', function(name, options) { 
     console.log(options.hash); //yeah my param 
     var partial = Handlebars.partials[name]; 
     var template = Handlebars.compile(partial); 
     //var template = Handlebars.compile(partial)(options.hash); * 
     var output = template({"body": options.fn(this)});  
     return new Handlebars.SafeString(output); 
     //return new Handlebars.SafeString(output(options.hash)); * 
     //return new Handlebars.SafeString(partial(output)); * 
    }) 
}; 

도우미,하지만 난 항상 경고가

경고 : 문자열 .hbs이 파일

기능하지

다른 부분에 내 blockhelper에서 PARAM을 통과
<div class="flyout {{flyoutClass}}"> 
    <button>flyout-button</button> 
    <div class="flyout__content"> 
     {{{body}}} 
    </div> 
</div> 

내 blockhelper

{{#injectHtml "flyout" flyoutClass='navigation__item'}} 
    <div class="wrapper"> 
     <h3>Headline</h3> 
     <p>some copy</p> 
     <button>CTA</button> 
    </div 
    <div class="wrapper"> 
     <h3>Headline</h3> 
     <p>some copy</p> 
     <button>CTA</button> 
    </div> 
{{/injectHtml}} 

UPTADE

전화 그리고 그것은 가능하다?

var output = template({ 
    "addClass": options.hash.addClass, 
    "id": options.hash.id, 
    "body": options.fn(this) 
}); 

는 I는 "ID"의

{{#injectHtml "flyout" flyoutClass='navigation__item'id='myUniqueID'}} 

및하지만 [1] PARAM 컴파일되지 나의 부분 버튼

<div class="flyout {{flyoutClass}}"> 
    {{>button btn="icon-text" id="{{id}}"/*[1]*/ icon="arrow-down"label="klick me"}} 
    <div class="flyout__content" aria-labelledby="{{id}}"/*[2]*/> 
     {{{body}}} 
    </div> 
</div> 

그것을 이용하여 부분이 연장하고자 , [2] 잘 작동합니다. 동적으로이 매개 변수를하려는 때문에

<div class="flyout navigation__item"> 
    <a href="#" id="{{id}}"/*[1]*/ aria-expanded="false"> 
    <div class="flyout__content" aria-labelledby="myUniqueID"/*[2]*/> 
     //html content 
    </div> 
</div> 

답변

1

당신은 부분적인 템플릿에 flyoutClass 매개 변수를 "컴파일"수 없습니다. 이것은 템플릿 데이터의 정말 또 다른 매개 변수를 의미합니다 :

var output = template({ 
    "flyoutClass": options.hash.flyoutClass, 
    "body": options.fn(this) 
}); 

UPDATE

...하지만이 동적으로 ?? [원문]

을 PARAMS를 추가 할 수있는 방법은 없습니다

도우미에 전달 된 모든 매개 변수를 전달하려면 options.hash :

을 사용하여 도우미 내에서 템플릿 데이터 개체를 확장하면됩니다.
var data = Handlebars.Utils.extend({ body: options.fn(this) }, options.hash); 
var output = template(data); 
+0

WOW 고맙습니다;) 작동하지만 params를 동적으로 추가 할 수있는 방법이 없습니까 ?? 왜냐하면 나는 또한이 블록 헬퍼를 내 fakescroller, 드롭 다운과 같은 다른 부분적인 부분을위한 generell 도우미로 사용하기를 원하기 때문이다. –