2012-12-18 1 views
2

Meteor, Handlebars 및 Backbone을 사용하여 다중 페이지 응용 프로그램을 만듭니다. 세션 변수를 설정하는 백본을 사용하여 라우터를 설정했습니다 (currentPage). currentPage의 값에 따라 다른 템플릿을 어떻게 렌더링합니까?세션 변수를 기반으로 한 렌더링 템플릿

이 작업을 수행 할 수있는 템플릿 도우미 함수를 만들 수 있다고 들었지만이 문제에 어떻게 접근해야할지 모르겠습니다.

Handlebars.registerHelper('currentPageIs',function(page){ 
    return currentPage == page; 
}); 

// and in the html: 
{{#if currentPageIs '/posts'}} 
    {{> posts}} 
{{else}} 
    {{> homepage}} 
{{/if}} 

답변

1

, 그때 나는이 작업을 기대. 먼저 meteorite을 설치해야합니다.

또한 공식 라우팅 솔루션은 roadmap에 있습니다.

0

정말 간단한 해결책은 여기에 있습니다 : : https://gist.github.com/3221138

을하지만 난 강력하게 meteor-router를 설치하는 것이 좋습니다 currentPage 글로벌하고 페이지를 문자열로 저장하는 경우

0

최선의 선택은 철 라우터를 사용하는 것,하지만 당신은 템플릿을 변경할 수있는 좋은 패턴 여기 것을 원하지 않는다 : 어떻게 경로를 확인하기 위해

//HTML 
<body> 
    {{>mainTemplate}} 
</body> 

//JS Client Initially 
var current = Template.initialTemplate; 
var currentDep = new Deps.Dependency; 

Template.mainTemplate = function() 
{ 
    currentDep.depend(); 
    return current; 
}; 

function setTemplate(newTemplate) 
{ 
    current = newTemplate; 
    currentDep.changed(); 
}; 

//Later 
setTemplate(Template.someOtherTemplate); 

모르겠다을 하지만 가능하면 제안 된 setTemplate 함수를 사용하여 템플릿을 조건부로 변경할 수 있습니다.

+0

Meteor 0.9.4에서 이것을 시도했지만 오류가 발생했습니다 : Tracker recumpute 함수에서 예외 : 오류 : 해당 템플릿이 없습니다. mainTemplate –