2015-02-04 3 views
3

handlebars.js NPM 모듈을 사용하여 미리 컴파일 된 템플릿을로드하기위한 프로토콜이 있습니까 아니면 특정 템플릿을로드하기 위해 자체 "getTemplate"기능을 제공해야합니까?노드 JS 서버 쪽에서 미리 컴파일 된 핸들 막대 템플릿로드

다음은 정의되지 않은 'hello'속성을 읽을 수 없습니다.

var compiledTemplate = handlebars.templates['hello']; 

"hello.handlebars"는 템플릿 파일의 이름입니다.

반면에 이것은 정상적으로 작동합니다.

var template = fs.readFileSync(“./hello.html", "utf8"); 
var uncompiledTemplate = handlebars.compile(template); 
var data = {message : "Hello world!"}; 
var finalPageHTML = uncompiledTemplate(data); 

그래서 난 그냥

compiledTemplate(data) 

을 실행하고 컴파일되지 않은 버전으로 내 최종 HTML의에 simmilar를 얻을 수 있도록 무엇을해야합니까?

감사합니다.

답변

0

나를 위해 적합한 해결책을 찾았습니다. 나는이 명령을 실행, 그리고

<ul> 
    <li>{{title}}</li> 
</ul> 

: 내 템플릿 디렉토리라는 list.handlebars에서 템플릿을

handlebars -c handlebars templates -f dist/templates.js && echo module.exports = templates; >> dist/templates.js 

'-c 핸들'플래그 앞에 추가

는 ("핸들")을 요구를; 출력합니다.

그런 다음 내 서버 코드에서, 내가 사용

var template = require('./dist/templates'); 
console.log(template['list']({title: 'winner'})); 
관련 문제